forked from dekuNukem/PicoRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_toc.py
45 lines (37 loc) · 882 Bytes
/
make_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
import sys
if len(sys.argv) <= 1:
print(__file__, 'text_file')
exit()
depth = 3
while 1:
try:
depth = int(input("how many levels? (1 to 4, default 3): "))
except Exception as e:
depth = 3
break
if 1 <= depth <= 4:
break
def make_section(text):
level = text.split(' ')[0].count('#')
if level > depth:
return
text = text.lstrip("#").replace('\r', '').replace('\n', '').strip()
link = text.lower().replace('.', '')
result = ''
for letter in link:
if letter.isalnum():
result += letter
else:
result += '-'
result = result.strip('-')
print(' '*(level-2) + f'- [{text}](#{result})\n')
text_file = open(sys.argv[1], encoding="utf8")
print("## Table of Contents\n")
for line in text_file:
if 'table of content' in line.lower():
continue
if line.startswith("# "):
continue
if line.startswith("#"):
make_section(line)
text_file.close()