Skip to content

Commit bf741e9

Browse files
committed
Add a (crude, ugly) working tex-to-pdf build step
1 parent 65f48dc commit bf741e9

13 files changed

Lines changed: 296 additions & 24 deletions

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/nostarch/[012]*.tex
22
/nostarch/hints.tex
33
/nostarch/book.*
4+
/nostarch.pdf
5+
/pdf/[012]*.tex
6+
/pdf/hints.tex
7+
/pdf/book.*
8+
/book.pdf
49
/html/[012]*.html
510
/html/js/chapter_info.js
611
/code/chapter/*
@@ -13,4 +18,3 @@
1318
/toc.txt
1419
/img/generated/*
1520
/img/cover.xcf
16-
/nostarch.pdf

00_intro.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Most importantly, there is no need to specify the way we want the
232232
program to jump back and forth any more. The `while` language
233233
construct takes care of that. It continues executing the block
234234
(wrapped in braces) below it as long as the condition it was given
235-
holds: `count <= 10`, which means “`count` is less than or equal to
235+
holds: `count <= 10`, which means “++count++ is less than or equal to
236236
10”. We no longer have to create a temporary value and compare that
237237
to zero. This was an uninteresting detail, and the power of
238238
programming languages is that they take care of uninteresting details

01_values.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ And this is the second
267267
There are, of course, situations where you want a backslash in a
268268
string to be just a backslash, not a special code. If two backslashes
269269
follow each other, they will collapse together, and only one will be
270-
left in the resulting string value. This is how the string “`A newline
271-
character is written like "\n".`” can be expressed:
270+
left in the resulting string value. This is how the string “++A newline
271+
character is written like "\n".++” can be expressed:
272272

273273
[source,javascript]
274274
----

03_functions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ other functions.
268268

269269
(((syntax)))(((square example)))(((function
270270
keyword)))(((function,definition)))(((function,declaration)))There is
271-
a slightly shorter way to say “`var square = function…`”. The
271+
a slightly shorter way to say “++var square = function…++”. The
272272
`function` keyword can also be used at the start of a statement, as in
273273
the following:
274274

04_data.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ not pass any arguments, the function somehow has access to the string
204204
described in link:06_object.html#obj_methods[Chapter 6].
205205

206206
Properties that contain functions are generally called _methods_ of
207-
the value they belong to. As in, “`toUpperCase` is a method of a
207+
the value they belong to. As in, “++toUpperCase++ is a method of a
208208
string”.
209209

210210
[[array_methods]]

11_language.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ values, we also create a form called `define`. It expects a word as
410410
its first argument, and an expression producing the value to assign to
411411
that word as its second argument. Since `define`, like everything, is
412412
an expression, it must return a value. We'll make it return the value
413-
that was assigned (just like JavaScript's `=` operator).
413+
that was assigned (just like JavaScript's `=` operator).
414414

415415
// include_code
416416

@@ -762,7 +762,7 @@ more work).
762762
(((hash character)))(((Egg language)))It would be nice if we could
763763
write ((comment))s in Egg. For example, whenever we find a hash sign
764764
(“#”), we could treat the rest of the line as a comment and ignore it,
765-
similar to “`//`” in JavaScript.
765+
similar to “++//++” in JavaScript.
766766

767767
(((skipSpace function)))We do not have to make any big changes to the
768768
parser to support this. We can simply change `skipSpace` to skip

13_dom.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ endif::book_target[]
669669
style attribute may contain one or more _((declaration))s_, which are
670670
a property (such as `color`) followed by a colon and a value (such as
671671
`green`). When there are more than one declaration, they must be
672-
separated by ((semicolon))s. For example, “`color: red; border:
673-
none`”.
672+
separated by ((semicolon))s. For example, “++color: red; border:
673+
none++”.
674674

675675
(((display (CSS))))(((layout)))There are a lot aspects that can be
676676
influenced with styling. For example, the `display` property controls

Makefile

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
all: html nostarch
2-
31
CHAPTERS := 00_intro 01_values 02_program_structure 03_functions 04_data 05_higher_order 06_object \
42
07_elife 08_error 09_regexp 10_modules 11_language 12_browser 13_dom 14_event 15_game 16_canvas \
53
17_http 18_forms 19_paint 20_node 21_skillsharing
@@ -31,32 +29,46 @@ code/solutions/20_4_a_public_space_on_the_web.zip: $(wildcard code/solutions/20_
3129
rm -f $@
3230
cd code/solutions; zip 20_4_a_public_space_on_the_web.zip 20_4_a_public_space_on_the_web/*
3331

34-
nostarch: $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex
35-
36-
nostarch/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js
37-
node bin/extract_hints.js | node bin/pre_latex.js - | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
38-
39-
nostarch/%.tex: %.txt asciidoc_nostarch.conf bin/pre_latex.js bin/clean_latex.js
40-
node bin/pre_latex.js $< | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
41-
4232
test: html
4333
@for F in $(CHAPTERS); do echo Testing $$F:; node bin/run_tests.js $$F.txt; done
4434
@! grep '[a-zA-Z0-9]_[—“”‘’]\|[—“”‘’]_[a-zA-Z0-9]\|[a-zA-Z0-9]`—\|[a-zA-Z0-9]`[a-zA-Z0-9]' *.txt
4535
@! grep '(!book\|(!html' html/*.html nostarch/*.tex
4636
@node bin/check_links.js
4737
@echo Done.
4838

39+
nostarch: $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex
40+
41+
nostarch/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js
42+
node bin/extract_hints.js | node bin/pre_latex.js --nostarch - | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
43+
44+
nostarch/%.tex: %.txt asciidoc_nostarch.conf bin/pre_latex.js bin/clean_latex.js
45+
node bin/pre_latex.js --nostarch $< | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
46+
4947
nostarch.pdf: nostarch/book.tex $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex \
5048
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
5149
cd nostarch && sh build.sh
5250
mv nostarch/book.pdf nostarch.pdf
5351

52+
pdf: $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex
53+
54+
pdf/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js asciidoc_pdf.conf bin/pre_latex.js bin/clean_latex.js
55+
node bin/extract_hints.js | node bin/pre_latex.js - | asciidoc -f asciidoc_pdf.conf --backend=latex -o - - | node bin/clean_latex.js > $@
56+
57+
pdf/%.tex: %.txt asciidoc_pdf.conf bin/pre_latex.js bin/clean_latex.js
58+
node bin/pre_latex.js $< | asciidoc -f asciidoc_pdf.conf --backend=latex -o - - | node bin/clean_latex.js > $@
59+
60+
book.pdf: pdf/book.tex $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex \
61+
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
62+
cd pdf && sh build.sh
63+
mv pdf/book.pdf .
64+
5465
pdfonce:
55-
cd nostarch && xelatex book.tex
56-
mv nostarch/book.pdf nostarch.pdf
66+
cd pdf && xelatex book.tex
67+
mv pdf/book.pdf book.pdf
5768

5869
texclean:
5970
rm -f nostarch/book.aux nostarch/book.idx nostarch/book.log nostarch/book.out nostarch/book.tbc nostarch/book.toc
71+
rm -f pdf/book.aux pdf/book.idx pdf/book.log pdf/book.out pdf/book.tbc pdf/book.toc
6072

6173
TMPDIR=/tmp/ejs_tex
6274

asciidoc_pdf.conf

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
[miscellaneous]
2+
newline=\n
3+
4+
[attributes]
5+
sectids!
6+
highlight=False
7+
book_target=True
8+
lsquo=‘
9+
rsquo=’
10+
ldquo=“
11+
rdquo=”
12+
13+
[replacements]
14+
“=``
15+
”=''
16+
‘=`
17+
’='
18+
—=---
19+
20+
[macros]
21+
(?su)(?<!\w)indexsee:\[(?P<attrlist>[^\]]*?)\]=indexsee
22+
\(!html (?P<text>(?:.|\n)*?)!\)=htmlonly
23+
\(!book (?P<text>(?:.|\n)*?)!\)=texonly
24+
(?su)(?<!\()[\\]?\(\(\((?P<attrlist>[^(](?:[^()]|\n)*?)\)\)\)(?!\))=indexterm
25+
(?<!\()[\\]?\(\((?P<attrlist>[^\s\(][^(](?:[^()]|\n)*?)\)\)(?!\))=indexterm2
26+
27+
[tags]
28+
superscript=^!..braceleft..!|!..braceright..!
29+
subscript=_!..braceleft..!|!..braceright..!
30+
monospaced=!..backslash..!lstinline:::|:::
31+
32+
[literal-inlinemacro]
33+
!..backslash..!lstinline:::{passtext}:::
34+
35+
[monospacedwords]
36+
!..backslash..!lstinline:::{words}:::
37+
38+
[quotes]
39+
`=#monospaced
40+
41+
[link-inlinemacro]
42+
!..backslash..!hyperref[{target}]!..braceleft..!{0}!..braceright..!
43+
44+
[indexsee-inlinemacro]
45+
!..backslash..!index!..braceleft..!{1}|see!..braceleft..!{2}!..braceright..!!..braceright..!
46+
47+
[http-inlinemacro]
48+
!..backslash..!href!..braceleft..!http:{target}!..braceright..!!..braceleft..!{0}!..braceright..!
49+
50+
[blockdef-listing]
51+
source-style=template="codeblock"
52+
53+
[htmlonly-inlinemacro]
54+
{empty}
55+
56+
[texonly-inlinemacro]
57+
{text}
58+
59+
[image-blockmacro]
60+
\vskip 1.5ex
61+
\includegraphics[width={width=10cm}]\{{target}\}
62+
\vskip 1.5ex
63+
{newline}
64+
\noindent
65+
66+
[partintroblock]
67+
template::[openblock]
68+
69+
[openblock]
70+
|
71+
72+
[quoteblock]
73+
{chapterquote?\epigraphhead[30]\{ }
74+
{chapterquote!\begin\{quote\}}
75+
{chapterquote?\epigraph\{\hspace*\{-.1cm\}\itshape``}|{chapterquote?''\}%}
76+
{attribution?\{---{attribution}{citetitle?, \emph\{{citetitle}\}}\}}
77+
{chapterquote?\}}
78+
{chapterquote!\end\{quote\}}
79+
{newline}
80+
81+
[codeblock]
82+
\label\{{id}\}
83+
\begin\{lstlisting\}{newline}
84+
|{newline}
85+
\end\{lstlisting\}
86+
{newline}
87+
88+
[listingblock]
89+
\begin\{lstlisting\}{newline}
90+
|{newline}
91+
\end\{lstlisting\}
92+
{newline}
93+
94+
[paragraph]
95+
\label\{{id}\}
96+
|
97+
{newline}
98+
99+
[header]
100+
\chapter\{{doctitle}\}
101+
{docid?\label\{{docid}\}}
102+
103+
[sect1]
104+
\section\{{title}\}
105+
\label\{{id}\}
106+
{newline}
107+
|
108+
109+
[sect2]
110+
\subsection\{{title}\}
111+
\label\{{id}\}
112+
{newline}
113+
|
114+
115+
[sect3]
116+
\subsubsection\{{title}\}
117+
\label\{{id}\}
118+
{newline}
119+
|
120+
121+
[footer]
122+
{empty}
123+
124+
[table]
125+
\noindent\begin\{tabular\}\{ll\}
126+
{headrows}
127+
{bodyrows}
128+
{footrows}
129+
\end\{tabular\}
130+
{newline}
131+
\noindent

bin/clean_latex.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,38 @@ function checkUnicode(str) {
1212
}
1313
}
1414

15+
var escaped = {"\\{{}": "{",
16+
"\\}{}": "}",
17+
"\\textbackslash{}": "\\",
18+
"\\${}": "$",
19+
"\\textless{}": "<",
20+
"\\textgreater{}": ">",
21+
"\\&{}": "&",
22+
"\\_{}": "_",
23+
"\\%{}": "%",
24+
"\\#{}": "\\#",
25+
"\\textasciicircum{}": "^",
26+
"\\textasciitilde{}": "~",
27+
"\\textbar{}": "|",
28+
"\\textquotedbl{}": "\""};
29+
var specials = Object.keys(escaped).map(function(k) {return k.replace(/[^\w\s]/g, "\\$&");}).join("|") + "|[^]";
30+
var deEscapeRE = new RegExp("\\\\lstinline:::((?:" + specials + ")+?):::", "g");
31+
var specialRE = new RegExp(specials, "g");
32+
function cleanLstInline(str) {
33+
return str.replace(deEscapeRE, function(m, content) {
34+
return "\\lstinline`" + content.replace(specialRE, function(f) {
35+
if (f.length > 1) return escaped[f];
36+
else if (f == "\n") return " ";
37+
else return f;
38+
}) + "`";
39+
});
40+
}
41+
1542
var input = "";
1643
process.stdin.on("data", function(chunk) {
1744
input += chunk;
1845
});
1946
process.stdin.on("end", function() {
20-
checkUnicode(input);
2147
input = input.replace(/(\n\n\\end{Code})|(\n{3,})|(_why,)|\\chapter\{(Introduction|Solutions to the Exercises)\}|\\hyperref\[((?:[^\]]|\\_\{\})+)\]|\\index\{([^|}]+?)\\textbar\{\}see\{([^}]+)}}|\\textasciicircum\{\}\{([^\}]+?)\}/g,
2248
function(all, codeSpace, manyBlanks, why, simplechapter, link, seeFrom, seeTo, superscript) {
2349
if (codeSpace)
@@ -35,7 +61,9 @@ process.stdin.on("end", function() {
3561
if (superscript)
3662
return "\\textsuperscript{" + superscript + "}";
3763
});
64+
input = cleanLstInline(input);
3865
input = input.replace(/({\\hspace\*\{.+?\}\\itshape``)\s*([^]+?)\s*('')/g, "$1$2$3");
66+
checkUnicode(input);
3967

4068
process.stdout.write(input);
4169
});

0 commit comments

Comments
 (0)