forked from Rust-for-Linux/rust-for-linux.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
executable file
·168 lines (140 loc) · 5.45 KB
/
post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
"""Postprocesses the "book".
"""
import json
import os
import pathlib
import re
import shutil
def handle_markdown(string):
if string.count("`") % 2 != 0:
raise RuntimeError("Unbalanced backticks.")
while True:
if "`" not in string:
return string
# Should use `code`, but it requires fixing the general CSS.
string = string.replace("`", '<span class="rfl-mono-font">', 1)
string = string.replace("`", "</span>", 1)
def process(path, content, html_menu, html_index_toc):
# Fix index title (`index.html` and `Rust-for-Linux.html`).
content = content.replace("<title>Rust for Linux - Rust for Linux</title>", "<title>Rust for Linux</title>")
# Fix header.
content = content.replace('<link rel="icon" href="favicon.svg">', "")
content = content.replace('<link rel="shortcut icon" href="favicon.png">', """
<link rel="icon" href="Rust-for-Linux.svg">
<meta property="og:url" content="https://rust-for-linux.com">
<meta property="og:type" content="website">
<meta property="og:title" content="Rust for Linux">
<meta property="og:description" content="Adding support for the Rust language to the Linux kernel">
<meta property="og:image" content="https://rust-for-linux.com/Rust-for-Linux.svg">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "CreativeWork",
"name": "Rust for Linux",
"description": "Adding support for the Rust language to the Linux kernel",
"url": "https://rust-for-linux.com",
"image": "https://rust-for-linux.com/Rust-for-Linux.svg"
}
</script>
<style>
#logo {
display: block;
margin-left: auto;
margin-right: auto;
}
.rfl-menu-block {
text-align: center;
font-size: 1.8rem;
font-weight: bold;
}
.rfl-mono-font {
font-family: var(--mono-font);
}
.rfl-mobile-links {
display: none;
}
@media only screen and (max-width: 1080px) {
.rfl-mobile-links {
display: block;
}
}
.chapter li.chapter-item {
margin-top: 0.2em !important;
margin-left: 1.5em !important;
}
.chapter li.part-title {
margin-bottom: -0.3em !important;
margin-top: 1em !important;
}
.quote-highlight:target {
/*
* `--table-header-bg` is used as an existing color in the themes
* that is good enough for highlighting the `blockquote`s.
*/
background-color: var(--table-header-bg);
}
</style>
""")
# Provide custom menu.
content = re.sub(r'<div class="sidebar-scrollbox">.*?</div>', html_menu, content, count=1, flags=re.DOTALL)
# Append Table of Contents in index (`index.html` and `Rust-for-Linux.html`), as well as
# the links for mobile.
content = content.replace("<!-- Generated TOC -->", html_index_toc)
return content
def main():
# Read TOC generated by `pre.py`.
with open("summary.json", "r", encoding="utf-8") as f:
json_summary = json.load(f)
# Generate menu.
html_menu = ""
for block_name in json_summary:
html_block = ""
for section_name in json_summary[block_name]:
if section_name != "":
html_block += f'<li class="part-title">{section_name}</li>'
for (text, link) in json_summary[block_name][section_name]:
html_block += f'<li class="chapter-item"><a href="{link}" tabindex="0">{handle_markdown(text)}</a></li>'
html_menu += f"""
<p class="rfl-menu-block">{block_name}</p>
<ol class="chapter">{html_block}</ol>
"""
html_menu = f"""
<div class="sidebar-scrollbox">
<a href="/"><img id="logo" src="Rust-for-Linux.svg" alt="Rust for Linux Logo"></a>
{html_menu}
</div>
"""
# Generate TOC for index pages.
html_index_toc = {}
for block_name in json_summary:
html_index_toc[block_name] = ""
for section_name in json_summary[block_name]:
section_links = ""
for (text, link) in json_summary[block_name][section_name]:
section_links += f'<li><a href="{link}" tabindex="0">{handle_markdown(text)}</a></li>'
if section_name != "":
html_index_toc[block_name] += f"<h3>{section_name}</h3>\n"
if section_links != "":
html_index_toc[block_name] += f"<ul>{section_links}</ul>\n"
html_index_toc = f"""
<h2><a class="header" href="#the-project">The project</a></h2>
{html_index_toc["The project"]}
<div class="rfl-mobile-links">
<h2><a class="header" href="#links">Links</a></h2>
{html_index_toc["Links"]}
</div>
"""
# Post-process each `mdbook`-generated file.
for path in pathlib.Path().glob("book/**/*.html"):
with path.open("r+", encoding="utf-8") as f:
new_content = process(path, f.read(), html_menu, html_index_toc)
f.seek(0)
f.truncate()
f.write(new_content)
# Remove unneeded files.
os.remove("book/favicon.svg")
os.remove("book/favicon.png")
if __name__ == "__main__":
main()