forked from AnthonyCalandra/modern-cpp-features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-generate-readme.py
89 lines (68 loc) · 2.25 KB
/
auto-generate-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
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
from pathlib import Path
class MarkdownParser():
def __init__(self, text):
self.text = text
self.lines = text.split('\n')
def title(self):
return self.lines[0].split(' ')[1]
def header(self, name, level, include_header=False):
start = False
end = False
content = []
mark = '#' * level
for line in self.lines:
if start and not end:
end |= (f'{mark} ' in line[:(level + 1)]) and (not f'{mark} {name}' in line)
if end:
start = False
else:
content.append(line)
else:
start = (f'{mark} {name}' in line)
if start:
end = False
if include_header:
content.append(line)
content = '\n'.join(content)
return content
def overview(self):
overview = self.header('Overview', 2)
overview = overview.split('\n')
overview = '\n'.join(overview[1:]) # remove the first line
return overview
def features(self):
return self.header('C++', 2, True)
def combine(text, parsers):
overview = ''
features = ''
title = ''
for p in parsers:
title += p.title().replace('C++', '') + '/'
overview += p.overview() + '\n'
features += p.features() + '\n'
title = title[:-1]
overview = overview.replace('README.md#', '#')
features = features.replace('README.md#', '#')
text = text.replace('# C++\n', f'# C++{title}\n')
text = text.replace(f'<!-- overview -->', overview)
text = text.replace(f'<!-- features -->', features)
return text
def main():
src_dir = Path(__file__).parent
parsers = []
srcs = list(src_dir.glob('CPP*.md'))
srcs.sort(reverse=True)
for file in srcs:
with open(file, 'r') as fp:
text = fp.read()
p = MarkdownParser(text)
parsers.append(p)
template_file = src_dir / 'readme-template.md'
with open(template_file, 'r') as fp:
text = fp.read()
text = combine(text, parsers)
readme_file = src_dir / 'README.md'
with open(readme_file, 'w') as fp:
fp.write(text)
if __name__ == '__main__':
main()