Skip to content

Commit 207ca93

Browse files
committed
Use a post-processing pass to add bookmarks
Also add them to h2 and h3 tags
1 parent 9eda6ad commit 207ca93

5 files changed

Lines changed: 60 additions & 41 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ html: $(foreach CHAP,$(CHAPTERS),html/$(CHAP).html) html/js/chapter_info.js html
99
$(patsubst img/%.svg,img/generated/%.png,$(SVGS))
1010

1111
html/%.html: %.txt asciidoc_html.conf
12-
PATH=node_modules/codemirror/bin:$(PATH) asciidoc -f asciidoc_html.conf --backend=html5 -o $@ $<
12+
PATH=node_modules/codemirror/bin:$(PATH) asciidoc -f asciidoc_html.conf --backend=html5 -o - $< | node bin/addmarks > $@
1313
node bin/build_code.js $<
1414

1515
html/js/chapter_info.js: $(foreach CHAP,$(CHAPTERS),$(CHAP).txt) code/solutions/* bin/chapter_info.js

asciidoc_html.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ options=sectionbody
4747
</pre>
4848

4949
[source-filter-style]
50-
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="./bin/blockfilter --bookmark -s {language}"
50+
source-style=template="source-highlight-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="./bin/blockfilter -s {language}"
5151

5252
[blockdef-listing]
5353
template::[source-filter-style]
@@ -100,7 +100,7 @@ template::[source-filter-style]
100100
|
101101

102102
[paradef-default]
103-
normal-style=template="paragraph",filter="./bin/parafilter"
103+
normal-style=template="paragraph"
104104

105105
[paragraph]
106106
<p{id? id="{id}"}>

bin/addmarks

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env node
2+
3+
var input = ""
4+
process.stdin.resume()
5+
process.stdin.on("data", function(chunk) {
6+
input += chunk.toString("utf8")
7+
})
8+
9+
process.stdin.on("end", function() {
10+
var re = /<(p|pre\ data-language|h[23])\b[^>]*>/g, m
11+
var pos = 0
12+
while (m = re.exec(input)) {
13+
var tag = m[1].match(/^\w+/)[0]
14+
var start = m.index + m[0].length
15+
var end = input.indexOf("</" + tag + ">", start)
16+
re.lastIndex = end + 3 + tag.length
17+
18+
var type, toHash = toPlainText(input.slice(start, end))
19+
if (tag == "pre") {
20+
type = "c"
21+
} else if (tag == "p") {
22+
type = "p"
23+
toHash = startAndEnd(toHash)
24+
} else {
25+
type = "h"
26+
}
27+
var mark = type + "_" + hash(toHash)
28+
var bookmark = "<a class=" + type + "_ident id=\"" + mark + "\" href=\"#" + mark + "\"></a>"
29+
process.stdout.write(input.slice(pos, start) + bookmark)
30+
pos = start
31+
}
32+
process.stdout.write(input.slice(pos))
33+
})
34+
35+
function hash(string) {
36+
var sum = require("crypto").createHash("sha1");
37+
sum.update(string);
38+
return sum.digest("base64").slice(0, 10);
39+
}
40+
41+
function toPlainText(text) {
42+
return text.replace(/<[^>]+>|&[^;]+;/g, "");
43+
}
44+
45+
function startAndEnd(text) {
46+
var words = text.split(/\W+/);
47+
if (!words[0]) words.shift();
48+
if (!words[words.length - 1]) words.pop();
49+
if (words.length <= 6) return words.join(" ");
50+
return words.slice(0, 3).join(" ") + " " + words.slice(words.length - 3).join(" ");
51+
}

bin/parafilter

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

html/css/ejs.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ pre[data-language=javascript] {
5959
cursor: pointer;
6060
}
6161

62-
pre.cm-s-default, p {
63-
margin-right: -30px;
64-
padding-right: 30px;
65-
}
66-
67-
p:hover a.p_ident:after, pre:hover a.c_ident:after {
62+
p:hover a.p_ident:after, pre:hover a.c_ident:after, h2:hover a.h_ident:after, h3:hover a.h_ident:after {
6863
content: "¶";
6964
color: #888;
7065
font-size: 80%;
@@ -99,6 +94,11 @@ h3 {
9994
font-size: 100%;
10095
}
10196

97+
pre.cm-s-default, p, h2, h3 {
98+
margin-right: -30px;
99+
padding-right: 30px;
100+
}
101+
102102
a, a:visited, a:active {
103103
text-decoration: none;
104104
color: #467;

0 commit comments

Comments
 (0)