Skip to content

Commit 38cc0cc

Browse files
Consolatisjohanmalm
authored andcommitted
Add further postprocessing for man pages
This adds: - List-of-contents on top, consisting of all `<h2>` headers - A on-hover perma link to the given header / entry
1 parent 515ef56 commit 38cc0cc

4 files changed

+87
-21
lines changed

build-site

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ build_man_pages () {
2626
sed 's/sans-serif;/monospace;/' src/template_pre.html > "$html"
2727
scdoc < "$scd" | pandoc --from man >> "$html"
2828
cat src/template_post.html >> "$html"
29-
./fix-bullet-formatting.py "$html"
29+
./man_post_processing.py "$html"
3030
done
3131
}
3232

fix-bullet-formatting.py

-20
This file was deleted.

man_post_processing.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import re
5+
6+
headings = re.compile('<(h[12])>([^<]*)</h[12]>')
7+
strong = re.compile('<p><strong>([^<]*)</strong>')
8+
HEADING_LEVELS = { 'h1': 0, 'h2': 1 }
9+
PERMA_LINK_CHAR='🔗'
10+
11+
def build_loc(sections):
12+
# Build index on top of the man page
13+
out = list()
14+
yield '<div class="LOC">'
15+
for lvl, text, link in sections:
16+
if lvl == 0:
17+
continue
18+
out.append(f'<a href="#{link}">{text.capitalize()}</a>')
19+
yield ' | '.join(out)
20+
yield '</div>'
21+
22+
def add_header_ids(buf):
23+
# Add on-hover perma links for headers
24+
sections = list()
25+
for match in headings.finditer(buf):
26+
all, el, value = (match.group(x) for x in range(3))
27+
target = value.lower().replace(' ', '_')
28+
sections.append((HEADING_LEVELS[el], value, target))
29+
buf = buf.replace(all, f'<{el} id="{target}">' +
30+
f'<a class="perma" href="#{target}">{PERMA_LINK_CHAR}</a>{value}</{el}>')
31+
buf = buf.replace('<!--%%%TOC%%%-->', '\n'.join(build_loc(sections)))
32+
return buf
33+
34+
def add_item_ids(buf):
35+
# Add on-hover perma links for entries other than headers
36+
for match in strong.finditer(buf):
37+
all, value = (match.group(x) for x in range(2))
38+
target = f'entry_{value.lower()}'
39+
for repl in (' ', '&gt;'):
40+
target = target.replace(repl, '_')
41+
for repl in ('"', "'", '&lt;', '\n', '/'):
42+
target = target.replace(repl, '')
43+
target = target.rstrip('_')
44+
buf = buf.replace(all, f'<p><strong id="{target}">' +
45+
f'<a class="perma" href="#{target}">{PERMA_LINK_CHAR}</a>{value}</strong>')
46+
return buf
47+
48+
def fix_bullet_formatting(buf):
49+
# Really ought to try to understand+fix this upstream,
50+
# but for now, let's just get it working
51+
return buf.replace("·</p>\n<p>", "· ")
52+
53+
def main(argv):
54+
with open(argv[0], 'r') as file:
55+
buf = file.read()
56+
57+
buf = fix_bullet_formatting(buf)
58+
buf = add_header_ids(buf)
59+
buf = add_item_ids(buf)
60+
61+
with open(argv[0], 'w') as file:
62+
file.write(buf)
63+
64+
if __name__ == "__main__":
65+
main(sys.argv[1:])
66+

src/template_pre.html

+20
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@
114114
text-decoration: underline;
115115
}
116116

117+
a.perma {
118+
display: none;
119+
}
120+
a.perma:hover {
121+
text-decoration: none;
122+
}
123+
:hover > a.perma {
124+
width: 1.5em;
125+
margin-left: -1.5em;
126+
display: inline-block;
127+
}
128+
129+
:target {
130+
background-color: rgba(128, 128, 128, 0.2);
131+
}
132+
133+
.LOC {
134+
margin: 2em 0;
135+
}
117136
</style>
118137
</head>
119138
<body>
@@ -127,3 +146,4 @@
127146
[<a href="manual.html">manual</a>]
128147
[<a href="tips-and-tricks.html">tips&nbsp;&amp;&nbsp;tricks</a>]
129148
</center>
149+
<!--%%%TOC%%%-->

0 commit comments

Comments
 (0)