This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_markdown.py
65 lines (51 loc) · 1.85 KB
/
setup_markdown.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
# Convert Markdown files to HTML, then embed in Rust functions that return
# raw strings.
# We use this script and pandoc instead of built-in markdown functionality
# due to the need for syntax highlighting. Rust's Syntec library doesn't
# work on the wasm32-unknown-unknown target.
# Rererence https://pandoc.org/MANUAL.html
import os
import re
# ./pandoc --list-highlight-styles
# pygments tango espresso zenburn kate monochrome breezedark haddock
STYLE = "tango"
VERSION = "0.4.2"
def main():
filenames = [
"quickstart",
"prereqs",
"structure",
"view",
"events",
"fetch",
"routing",
"release_and_debugging",
"complex_apps",
"misc",
"server_integration",
"about",
"js",
]
for filename in filenames:
# Perform the conversion
os.system(f'pandoc ./markdown/{filename}.md -s --highlight-style {STYLE}\
-o src/book/{filename}.html --metadata pagetitle="{filename}"')
# Trim everything except for the HTML body; Pandoc outputs full files.
with open(f'./src/book/{filename}.html', encoding="utf8") as f:
data = f.read()
regex = re.compile(r'<body>(.*?)</body>', re.DOTALL)
m = re.search(regex, data)
body = m.groups(0)[0]
# Update all instances of the version, so we don't have to in Markdown.
body = re.sub(r'seed/0\.\d\.(\d{1,3})', "seed/" + VERSION, body)
# Create a new rust file
with open(f'./src/book/{filename}.rs', 'w', encoding="utf8") as f:
f.write('pub fn text() -> String {\n')
f.write('r#####"')
f.write(body)
f.write('"#####.into()\n')
f.write('}')
# Clean up the temporary HTML files
os.remove(f'./src/book/{filename}.html')
if __name__ == "__main__":
main()