-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_toc.py
36 lines (25 loc) · 987 Bytes
/
gen_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
import os
def generate_table_of_contents(directory):
toc = []
for root, dirs, files in os.walk(directory):
# ignore dot-folders
dirs[:] = [d for d in dirs if not d.startswith('.')]
files.sort()
for file in files:
if not file.endswith(".md"):
continue
filepath = os.path.relpath(os.path.join(root, file), directory)
# get titles
with open(os.path.join(root, file), 'r', encoding='utf-8') as f:
for i, line in enumerate(f):
if i == 2:
title = line.strip()[2:]
break
depth = filepath.count(os.path.sep)
indent = ' ' * depth
# construct entry
toc.append(f"{indent}- [{title}]({filepath})")
return "\n".join(toc)
if __name__ == "__main__":
table_of_contents = generate_table_of_contents('docs')
print(table_of_contents)