forked from rust-lang/rfcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-book.py
executable file
·60 lines (45 loc) · 1.79 KB
/
generate-book.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
#!/usr/bin/env python3
"""
This auto-generates the mdBook SUMMARY.md file based on the layout on the filesystem.
This generates the `src` directory based on the contents of the `text` directory.
Most RFCs should be kept to a single chapter. However, in some rare cases it
may be necessary to spread across multiple pages. In that case, place them in
a subdirectory with the same name as the RFC. For example:
0123-my-awesome-feature.md
0123-my-awesome-feature/extra-material.md
It is recommended that if you have static content like images that you use a similar layout:
0123-my-awesome-feature.md
0123-my-awesome-feature/diagram.svg
The chapters are presented in sorted-order.
"""
import os
import shutil
import subprocess
def main():
if os.path.exists('src'):
# Clear out src to remove stale links in case you switch branches.
shutil.rmtree('src')
os.mkdir('src')
for path in os.listdir('text'):
symlink(f'../text/{path}', f'src/{path}')
symlink('../README.md', 'src/introduction.md')
with open('src/SUMMARY.md', 'w') as summary:
summary.write('[Introduction](introduction.md)\n\n')
collect(summary, 'text', 0)
subprocess.call(['mdbook', 'build'])
def collect(summary, path, depth):
entries = [e for e in os.scandir(path) if e.name.endswith('.md')]
entries.sort(key=lambda e: e.name)
for entry in entries:
indent = ' '*depth
name = entry.name[:-3]
link_path = entry.path[5:]
summary.write(f'{indent}- [{name}]({link_path})\n')
maybe_subdir = os.path.join(path, name)
if os.path.isdir(maybe_subdir):
collect(summary, maybe_subdir, depth+1)
def symlink(src, dst):
if not os.path.exists(dst):
os.symlink(src, dst)
if __name__ == '__main__':
main()