-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_readme.py
executable file
·34 lines (31 loc) · 1.28 KB
/
gen_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
#!/usr/bin/env python3
def main():
with open("README.md", "w") as out:
with open("src/lib.md", "r") as f:
lines = f.readlines()
code_snippet = False
for lineno, line in enumerate(lines, start=1):
stripped = line.strip() + " "
if code_snippet:
if stripped.startswith("# "):
continue
if stripped.startswith("##"):
before, __, after = line.partition("##")
line = "#".join((before, after))
if stripped.startswith("```"):
code_snippet = False
else:
if stripped.startswith("```"):
for lang in ("rust", "toml"):
prefix = "```" + lang
if stripped.startswith(prefix):
prev, __, __ = line.partition(prefix)
out.write(f"\n{prev}<!-- AUTOGENERATED FILE -->\n\n")
line = "".join((prev, prefix, "\n"))
break
else:
raise RuntimeError("Unrecognized lang at line", lineno)
code_snippet = True
out.write(line)
if __name__ == '__main__':
main()