-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmd_toc.py
45 lines (32 loc) · 1.24 KB
/
md_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
"""
add a table of contents to markdown
references:
- https://meta.stackexchange.com/questions/189749/
could also use:
- https://about.gitlab.com/handbook/markdown-guide/#table-of-contents-toc
- https://stackoverflow.com/questions/9721944/automatic-toc-in-github-flavoured-markdown
"""
import re
from collections import defaultdict
from sys import argv
def add_toc(lines, placeholder="<!-- TOC -->\n"):
seen = defaultdict(int)
toc = []
for line in lines:
if m := re.match(r"## (.+)$", line):
link = m.group(1).lower()
link = "#" + re.sub(r"[^A-Za-z\d_-]", "", re.sub(r"\s+", "-", link))
seen[link] += 1
if seen[link] > 1:
link = link + f"-{seen[link] - 1}"
toc.append(f"{len(toc) + 1}. [{m.group(1)}]({link})\n")
for i, line in enumerate(lines):
if line == placeholder:
lines[i:i+1] = toc
return lines
if __name__ == "__main__":
input_filename = argv[1] if len(argv) > 1 else "programming-resources.md"
output_filename = argv[2] if len(argv) > 2 else "README.md"
with (open(input_filename, "r") as fi,
open(output_filename, "w") as fo):
fo.writelines(add_toc(fi.readlines()))