forked from anghunk/linuxdo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.py
More file actions
23 lines (20 loc) · 724 Bytes
/
toc.py
File metadata and controls
23 lines (20 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 运行 `py toc.py` 自动生成 README.md 目录
import re
def generate_toc(md_file):
toc = []
with open(md_file, 'r', encoding='utf-8') as file:
for line in file:
if line.startswith('## '):
title = line[3:].strip()
link = re.sub(r'\s+', '-', title.lower())
toc.append(f' - [{title}](#{link})')
elif line.startswith('### '):
title = line[4:].strip()
link = re.sub(r'\s+', '-', title.lower())
toc.append(f' - [{title}](#{link})')
return '\n'.join(toc)
if __name__ == "__main__":
md_file = 'README.md'
toc = generate_toc(md_file)
print("## 目录")
print(toc)