Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Commit 51a0169

Browse files
committed
Update link checking utility
1 parent 5894772 commit 51a0169

File tree

3 files changed

+33
-112
lines changed

3 files changed

+33
-112
lines changed

Makefile

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ html/%.html: %.md
1212
node src/render_html.js $< > $@
1313
node src/build_code.js $<
1414

15-
html/js/chapter_info.js: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) code/solutions/* bin/chapter_info.js
15+
html/js/chapter_info.js: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) code/solutions/* src/chapter_info.js
1616
node src/chapter_info.js > html/js/chapter_info.js
1717

1818
html/js/acorn_codemirror.js: node_modules/codemirror/lib/codemirror.js \
@@ -43,81 +43,5 @@ test: html
4343
@for F in $(CHAPTERS); do echo Testing $$F:; node bin/run_tests.js $$F.txt; done
4444
@! grep '[a-zA-Z0-9]_[—“”‘’]\|[—“”‘’]_[a-zA-Z0-9]\|[a-zA-Z0-9]`—\|[a-zA-Z0-9]`[a-zA-Z0-9]' *.txt
4545
@! grep '(!book\|(!html|(!interactive|(!tex' html/*.html nostarch/*.tex
46-
@node bin/check_links.js
46+
@node src/check_links.js
4747
@echo Done.
48-
49-
nostarch: $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex
50-
51-
nostarch/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js
52-
node bin/extract_hints.js | node bin/pre_latex.js --nostarch - | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
53-
54-
nostarch/%.tex: %.txt asciidoc_nostarch.conf bin/pre_latex.js bin/clean_latex.js
55-
node bin/pre_latex.js --nostarch $< | asciidoc -f asciidoc_nostarch.conf --backend=latex -o - - | node bin/clean_latex.js > $@
56-
57-
nostarch.pdf: nostarch/book.tex $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex \
58-
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
59-
cd nostarch && sh build.sh
60-
mv nostarch/book.pdf nostarch.pdf
61-
62-
pdf: $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex
63-
64-
pdf/hints.tex: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js asciidoc_pdf.conf bin/pre_latex.js bin/clean_latex.js
65-
node bin/extract_hints.js | node bin/pre_latex.js - | asciidoc -f asciidoc_pdf.conf --backend=latex -o - - | node bin/clean_latex.js > $@
66-
67-
pdf/%.tex: %.txt asciidoc_pdf.conf bin/pre_latex.js bin/clean_latex.js
68-
node bin/pre_latex.js $< | asciidoc -f asciidoc_pdf.conf --backend=latex -o - - | node bin/clean_latex.js > $@
69-
70-
book.pdf: pdf/book.tex $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex \
71-
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
72-
cd pdf && sh build.sh book > /dev/null
73-
mv pdf/book.pdf .
74-
75-
pdf/book_mobile.tex: pdf/book.tex
76-
cat pdf/book.tex | sed -e 's/makeidx}/makeidx}\n\\usepackage[a5paper, left=5mm, right=5mm]{geometry}/' | sed -e 's/setmonofont.Scale=0.8./setmonofont[Scale=0.75]/' > pdf/book_mobile.tex
77-
78-
book_mobile.pdf: pdf/book_mobile.tex $(foreach CHAP,$(CHAPTERS),pdf/$(CHAP).tex) pdf/hints.tex \
79-
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
80-
cd pdf && sh build.sh book_mobile > /dev/null
81-
mv pdf/book_mobile.pdf .
82-
83-
pdfonce:
84-
cd pdf && xelatex book.tex
85-
mv pdf/book.pdf book.pdf
86-
87-
texclean:
88-
rm -f nostarch/book.aux nostarch/book.idx nostarch/book.log nostarch/book.out nostarch/book.tbc nostarch/book.toc
89-
rm -f pdf/book.aux pdf/book.idx pdf/book.log pdf/book.out pdf/book.tbc pdf/book.toc
90-
91-
TMPDIR=/tmp/ejs_tex
92-
93-
ejs_tex.zip: nostarch/book.tex $(foreach CHAP,$(CHAPTERS),nostarch/$(CHAP).tex) nostarch/hints.tex \
94-
$(patsubst img/%.svg,img/generated/%.pdf,$(SVGS))
95-
rm -rf $@ $(TMPDIR)
96-
mkdir -p $(TMPDIR)
97-
cp nostarch/*.tex $(TMPDIR)
98-
cp nostarch/book.tex $(TMPDIR)
99-
cp nostarch/build.sh nostarch/nostarch.cls nostarch/nshyper.sty nostarch/nostarch.ins $(TMPDIR)
100-
grep includegraphics nostarch/*.tex | sed -e 's/.*{\(.*\)}/\1/' | xargs -I{} cp --parents "{}" $(TMPDIR)
101-
cd /tmp; zip -r ejs_tex.zip ejs_tex
102-
mv /tmp/ejs_tex.zip $@
103-
rm -rf $(TMPDIR)
104-
105-
book.epub: epub/titlepage.xhtml epub/toc.xhtml epub/hints.xhtml $(foreach CHAP,$(CHAPTERS),epub/$(CHAP).xhtml) \
106-
epub/content.opf.src epub/style.css bin/add_images_to_epub.js
107-
rm -f $@
108-
grep '<img' epub/*.xhtml | sed -e 's/.*src="\([^"]*\)".*/\1/' | xargs -I{} cp --parents "{}" epub
109-
node bin/add_images_to_epub.js
110-
cd epub; zip -X ../$@ mimetype
111-
cd epub; zip -X ../$@ -r * -x mimetype -x content.opf.src
112-
113-
epub/%.xhtml: %.txt asciidoc_epub.conf bin/pre_epub.js
114-
node bin/pre_epub.js $< | asciidoc -f asciidoc_epub.conf --backend=xhtml11 -o $@ -
115-
116-
epub/hints.xhtml: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) bin/extract_hints.js
117-
node bin/extract_hints.js | node bin/pre_epub.js - | asciidoc -f asciidoc_epub.conf --backend=xhtml11 -o $@ -
118-
119-
epubcheck: book.epub
120-
epubcheck book.epub 2>&1 | grep -v 'img/.*\.svg'
121-
122-
book.mobi: book.epub img/cover.png
123-
ebook-convert book.epub book.mobi --output-profile=kindle --cover=img/cover.png --remove-first-image

bin/check_links.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/check_links.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var fs = require("fs");
2+
3+
var files = Object.create(null);
4+
fs.readdirSync(".").forEach(function(name) {
5+
var m = /^(\d\d.*)\.md$/.exec(name);
6+
if (m) files[m[1]] = fs.readFileSync(name, "utf8");
7+
});
8+
files["22_fast"] = "{{id fast}}"; // Kludge to recognize bonus chapter
9+
files["hints"] = "{{id hints}}";
10+
11+
var fail = 0;
12+
function error(file, msg) {
13+
console.error(file + ": " + msg);
14+
fail = 1;
15+
}
16+
17+
var link = /\]\((([^\.\s]+)\.(\w{2,5})#([^\s\[]+?))\)/g, m;
18+
for (var file in files) {
19+
while (m = link.exec(files[file])) {
20+
let [_, link, file, ext, anchor] = m
21+
if (ext != "html")
22+
error(file, "Bad extension: " + link);
23+
var target = files[file];
24+
if (!target)
25+
error(file, "Unknown target file: " + link);
26+
else if (anchor != file.slice(3) && target.indexOf("{{id " + anchor + "}}") == -1)
27+
error(file, "Non-existing anchor: " + link);
28+
}
29+
}
30+
31+
process.exit(fail);

0 commit comments

Comments
 (0)