-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_toc.py
executable file
·55 lines (47 loc) · 1.68 KB
/
create_toc.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
#!/usr/bin/env python3
"""Create table of contents with all epics."""
import os
def get_toc():
"""Get table of contents with all epics."""
epic_dirs = [d for d in os.listdir(".") if os.path.isdir(d) and d[:1].isdigit()]
toc = []
for epic_dir in epic_dirs:
try:
num = int(epic_dir.split("-", 1)[0])
if not 0 <= num < 1000:
raise ValueError
except ValueError:
raise RuntimeError(f"Invalid directory name {epic_dir}")
spec_path = os.path.join(epic_dir, "technical_specification.md")
if not os.path.exists(spec_path):
raise RecursionError(f"No technical_specification.md found in {epic_dir}")
for line in open(spec_path):
if line.startswith("# "):
title = line[2:].strip()
break
else:
title = None
if not title:
raise RuntimeError(f"No title found in {spec_path}")
try:
desc, name = title.rsplit("(", 1)
desc = desc.rstrip()
name = name[:-1].strip().title()
except ValueError:
name = desc = None
if not name or not desc:
raise RuntimeError(f"Unexpected title in {spec_path}")
link = f"[{name}](./{spec_path})"
toc.append((num, link, desc))
return sorted(toc)
def main():
"""Print table of contents in Markdown format."""
toc = get_toc()
is_continuous = toc[-1][0] - toc[0][0] == len(toc) - 1
for num, link, desc in get_toc():
if is_continuous:
print(f"{num}. {link}: {desc}")
else:
print(f"- ({num}) {link}: {desc}")
if __name__ == "__main__":
main()