-
Notifications
You must be signed in to change notification settings - Fork 7
/
index_gen.py
executable file
·44 lines (37 loc) · 1.41 KB
/
index_gen.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
#!/usr/bin/env python3
import os
seperator = " "
# Based on https://stackoverflow.com/a/16974952
# Thank you based Ajay && based zaooza
def walk_lexicographic(path='.'):
'''
Traverses a directory tree. Each level is traversed in lexicographical
order, and when a subdirectory is encountered it will be yielded
immediately before being descended into, recursively, and then traversal of
the original directory is continued. Entries beginning with a dot are never
yielded, nor descended into.
'''
entries = sorted(
(entry for entry in os.scandir(path) if entry.name[0] != '.'),
key=lambda e: e.name
)
for entry in entries:
yield entry
if entry.is_dir():
yield from walk_lexicographic(entry.path)
def toc_lines(path='.'):
'''
Yield Markdown for a table of contents for the files rooted at ``path'',
line by line.
'''
for entry in walk_lexicographic(path):
if entry.is_dir():
yield (entry.path.count('/') - 1) * seperator + "* " + entry.name
elif entry.name not in ('README.md', 'index.md', 'index_gen.py'):
link = entry.path
prefix = (entry.path.count('/') - 1) * seperator + "* "
stem = entry.name.split('.')[0]
yield f'{prefix}[{stem}]({link})'
with open("index.md", "w") as index_file:
for toc_line in toc_lines():
print(toc_line, file=index_file)