-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrfc2book.py
executable file
·84 lines (68 loc) · 2.81 KB
/
rfc2book.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
#!/usr/bin/env python3
# This script is based on the `generate-book.py` script from the Rust RFCs
# repository: https://github.com/rust-lang/rfcs/blob/2b78d7bd05f718dc7e5023372f8a692d3b448600/generate-book.py
"""
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():
src_path = None
rfc_path = None
cwd = os.getcwd()
if cwd.endswith('book'):
# change source paths if we are being run inside the `book` directory.
src_path = 'src'
rfc_path = '../rfcs'
else:
# otherwise, assume the script is being run inside the repo root.
src_path = 'book/src'
rfc_path = 'rfcs'
book_rfcs = f'{src_path}/rfcs'
if os.path.exists(book_rfcs):
# Clear out src to remove stale links in case you switch branches.
shutil.rmtree(book_rfcs)
os.mkdir(book_rfcs)
with open(f'{src_path}/index.md', 'r') as summary_in:
summary = summary_in.read()
with open(f'{src_path}/SUMMARY.md', 'w') as summary_out:
summary_out.write(summary)
header = "\n# mnemOS RFCs\n"
print(header, end='')
summary_out.write(header)
index_item = '\n- [introduction](rfcs/README.md)\n';
print(index_item, end='')
summary_out.write(f'\n{index_item}')
collect(summary_out, rfc_path, src_path, 1)
print('')
def collect(summary, path, srcpath, 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:
symlink(f'../../../{path}/{entry.name}', f'{srcpath}/rfcs/{entry.name}')
indent = ' '*depth
name = entry.name[:-3]
if name != 'README':
link_path = entry.path[5:]
index_item = f'{indent}- [{name}](rfcs/{link_path})\n'
print(index_item, end='')
summary.write(index_item)
maybe_subdir = os.path.join(path, name)
if os.path.isdir(maybe_subdir):
collect(summary, maybe_subdir, srcpath, depth+1)
def symlink(src, dst):
if not os.path.exists(dst):
os.symlink(src, dst)
if __name__ == '__main__':
main()