forked from jcjohnson/pytorch-examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_readme.py
53 lines (46 loc) · 1.55 KB
/
build_readme.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
import os
"""
GitHub doesn't provide an include mechanism for README files so we have to
implement our own.
"""
def main():
build_readme('README_raw.md', 'README.md')
for d in os.listdir('.'):
if not os.path.isdir(d) or d.startswith('.'):
continue
in_path = os.path.join(d, 'README_raw.md')
out_path = os.path.join(d, 'README.md')
build_readme(in_path, out_path)
def build_readme(in_path, out_path):
if not os.path.isfile(in_path):
return
contents = []
with open(in_path, 'r') as fin:
for line in fin:
if line.startswith('## '):
contents.append(line[3:-1])
with open(in_path, 'r') as fin, open(out_path, 'w') as fout:
for line in fin:
if line.startswith(':CONTENTS'):
for sec in contents:
href = sec.lower().replace(' ','-').replace(':','')
fout.write("- <a href='#%s'>%s</a>\n" % (href, sec))
elif line.startswith(':INCLUDE'):
include_path = line.split(' ')[1].strip()
include_path = os.path.join(os.path.split(in_path)[0], include_path)
fout.write('# Code in file %s\n' % include_path)
skip_toggle = False
skip_next = False
with open(include_path, 'r') as finc:
for ll in finc:
if ll.startswith('"""'):
skip_next = skip_toggle
skip_toggle = not skip_toggle
elif not skip_toggle and not skip_next:
fout.write(ll)
elif skip_next:
skip_next = False
else:
fout.write('%s' % line)
if __name__ == '__main__':
main()