Skip to content

Commit d029eb7

Browse files
committed
Generate a two-level TOC for the epub file
Issue marijnh#381
1 parent a64859d commit d029eb7

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
/epub/hints.xhtml
2828
/epub/img/*
2929
/epub/content.opf
30+
/epub/toc.xhtml
3031
/book.epub
3132
/book.mobi

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ book.epub: epub/titlepage.xhtml epub/toc.xhtml epub/hints.xhtml $(foreach CHAP,$
6565
grep '<img' epub/*.xhtml | sed -e 's/.*src="\([^"]*\)".*/\1/' | xargs -I{} cp --parents "{}" epub
6666
node src/add_images_to_epub.js
6767
cd epub; zip -X ../$@ mimetype
68-
cd epub; zip -X ../$@ -r * -x mimetype -x content.opf.src
68+
cd epub; zip -X ../$@ -r * -x mimetype -x *.src
69+
70+
epub/toc.xhtml: epub/toc.xhtml.src $(foreach CHAP,$(CHAPTERS),epub/$(CHAP).xhtml) epub/hints.xhtml
71+
node src/generate_epub_toc.js $^ > $@
6972

7073
epub/%.xhtml: %.md src/render_html.js
7174
node src/render_html.js --epub $< > $@

epub/toc.xhtml renamed to epub/toc.xhtml.src

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,7 @@
7575
<!-- machine-readable TOC -->
7676
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
7777
<ol>
78-
<li><a href="00_intro.xhtml">Introduction</a></li>
79-
<li><a href="01_values.xhtml">Values, Types, and Operators</a></li>
80-
<li><a href="02_program_structure.xhtml">Program Structure</a></li>
81-
<li><a href="03_functions.xhtml">Functions</a></li>
82-
<li><a href="04_data.xhtml">Data Structures: Objects and Arrays</a></li>
83-
<li><a href="05_higher_order.xhtml">Higher-order Functions</a></li>
84-
<li><a href="06_object.xhtml">The Secret Life of Objects</a></li>
85-
<li><a href="07_robot.xhtml">Project: A Robot</a></li>
86-
<li><a href="08_error.xhtml">Bugs and Errors</a></li>
87-
<li><a href="09_regexp.xhtml">Regular Expressions</a></li>
88-
<li><a href="10_modules.xhtml">Modules</a></li>
89-
<li><a href="11_async.xhtml">Asynchronous Programming</a></li>
90-
<li><a href="12_language.xhtml">Project: A Programming Language</a></li>
91-
<li><a href="13_browser.xhtml">JavaScript and the Browser</a></li>
92-
<li><a href="14_dom.xhtml">The Document Object Model</a></li>
93-
<li><a href="15_event.xhtml">Handling Events</a></li>
94-
<li><a href="16_game.xhtml">Project: A Platform Game</a></li>
95-
<li><a href="17_canvas.xhtml">Drawing on Canvas</a></li>
96-
<li><a href="18_http.xhtml">HTTP and Forms</a></li>
97-
<li><a href="19_paint.xhtml">Project: A Pixel Art Editor</a></li>
98-
<li><a href="20_node.xhtml">Node.js</a></li>
99-
<li><a href="21_skillsharing.xhtml">Project: Skill-Sharing Website</a></li>
100-
<li><a href="hints.xhtml">Hints to the exercises</a></li>
78+
{{full_toc}}
10179
</ol>
10280
</nav>
10381
</body>

src/generate_epub_toc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {readFileSync} = require("fs")
2+
const {basename} = require("path")
3+
4+
let [template, ...chapters] = process.argv.slice(2)
5+
6+
function esc(str) {
7+
return str.replace(/[<>&"]/g, ch => ch == "<" ? "&lt;" : ch == ">" ? "&gt;" : ch == "&" ? "&amp;" : "&quot;")
8+
}
9+
10+
let toc = ""
11+
const section = /<h2\b[^>]*><a [^>]*?id="(h_.*?)".*?><\/a>(.*?)<\/h2>/g
12+
13+
for (let chapter of chapters) {
14+
let text = readFileSync(chapter, "utf8")
15+
let base = basename(chapter)
16+
let title = /<h1.*?>(.*?)<\/h1>/.exec(text)[1]
17+
toc += ` <li><a href="${base}">${esc(title)}</a>
18+
<ol>\n`
19+
for (let match; match = section.exec(text);)
20+
toc += ` <li><a href="${base}#${match[1]}">${esc(match[2])}</a></li>\n`
21+
toc += ` </ol>
22+
</li>\n`
23+
}
24+
25+
console.log(readFileSync(template, "utf8").replace("{{full_toc}}", toc))

0 commit comments

Comments
 (0)