Skip to content

Commit 57d2132

Browse files
committed
Add functionality for including hints at the end of the pdf book
1 parent cf2776d commit 57d2132

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/pdf/hints.tex
77
/pdf/book.*
88
/pdf/book_mobile.*
9+
/pdf/*.log
910
/book.pdf
1011
/book_mobile.pdf
1112
/html/[012]*.html

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ test: html
3535
@node src/check_links.js
3636
@echo Done.
3737

38-
book.pdf: $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/book.tex $(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
38+
book.pdf: $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex pdf/book.tex $(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
3939
#cd pdf && sh build.sh book > /dev/null
4040
mv pdf/book.pdf .
4141

42+
pdf/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).md) src/extract_hints.js
43+
node src/extract_hints.js | node src/render_latex.js - > $@
44+
4245
img/generated/%.pdf: img/%.svg
4346
inkscape --export-pdf=$@ $<
4447

pdf/book.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137

138138
\input{21_skillsharing.tex}
139139

140-
%\input{hints.tex}
140+
\input{hints.tex}
141141

142142
\backmatter
143143

src/extract_hints.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const {readdirSync, readFileSync} = require("fs")
2+
3+
process.stdout.write("# Exercise Hints\n\nThe hints below might help when you are stuck with one of the exercises in this book. They don't give away the entire solution, but rather try to help you find it yourself.\n\n");
4+
5+
for (let name of readdirSync(".")) {
6+
if (!/^\d\d.*\.md$/.test(name)) continue
7+
8+
let file = readFileSync(name, "utf8")
9+
let title = file.match(/(?:\n|^)# (.*?)\n/)[1], titleWritten = false
10+
11+
let curSubsection
12+
let re = /\n### (.*?)\n|\{\{hint\n([^]+?)\nhint\}\}/g
13+
while (m = re.exec(file)) {
14+
if (m[1]) {
15+
curSubsection = m[1]
16+
} else {
17+
if (!titleWritten) {
18+
process.stdout.write(`## ${title}\n\n`)
19+
titleWritten = true
20+
}
21+
process.stdout.write(`### ${curSubsection}\n\n${m[2].trim()}\n\n`)
22+
}
23+
}
24+
}

src/render_latex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ let file, noStarch = false
55
for (let arg of process.argv.slice(2)) {
66
if (arg == "--nostarch") noStarch = true
77
else if (file) throw new Error("Multiple input files")
8-
else file = arg
8+
else file = arg == "-" ? "/dev/stdin" : arg
99
}
1010
if (!file) throw new Error("No input file")
11-
let chapter = /^\d{2}_([^\.]+)/.exec(file)
11+
let chapter = /^\d{2}_([^\.]+)/.exec(file) || [null, "hints"]
1212

1313
let {tokens, metadata} = transformTokens(require("./markdown").parse(fs.readFileSync(file, "utf8"), {}), {
1414
defined: ["book", "tex"],

0 commit comments

Comments
 (0)