forked from patcg-individual-drafts/ipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.py
executable file
·81 lines (69 loc) · 1.89 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import argparse
from fileinput import input
import re
from sys import argv, stderr
import tempfile
_seen = dict()
_xml = re.compile(r"</?[a-zA-Z]\w+>")
def href(title):
"""
An attempt to replicate how GitHub creates title anchors.
"""
global _xml
title = _xml.sub("", title)
h = "".join(
[x.lower() if x.isalnum() else ("-" if x == " " else "") for x in title]
)
global _seen
_seen[h] = _seen.get(h, 0) + 1
if _seen[h] > 1:
return f"{h}-{_seen[h]-1}"
return h
parser = argparse.ArgumentParser(description="Add a markdown ToC")
parser.add_argument(
"-d",
"--depth",
dest="depth",
type=ord,
default=3,
help="Maximum heading level to include",
)
parser.add_argument(
"-t",
"--title",
dest="titles",
action="append",
default=["table of contents", "Table of Contents"],
help="Title of ToC section",
)
parser.add_argument("file", nargs=1, help="Markdown file")
args = parser.parse_args()
tocfile = tempfile.NamedTemporaryFile()
with open(tocfile.name, "w") as toc:
intoc = False
for line in input(files=(args.file)):
if line.startswith("#"):
line = line.rstrip("#").strip()
title = line.lstrip("#")
level = len(line) - len(title)
title = title.strip()
if level <= args.depth:
toc.write(f"{' ' * (level - 1)}* [{title}](#{href(title)})\n")
elif intoc:
continue
intoc = False
for line in input(files=(args.file)):
if line.startswith("#"):
title = line.strip("#").strip().lower()
intoc = title in args.titles
if intoc:
print(line.rstrip())
print()
with open(tocfile.name) as toc:
for t in toc:
print(t.rstrip())
print()
if intoc:
continue
print(line.rstrip())