-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontensifier.py
executable file
·37 lines (28 loc) · 971 Bytes
/
contensifier.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
#!/usr/bin/python
import re
import argparse
import urllib
punctuation_regex = r'[^\w\- ]' # https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb#L26
# setup arguments
parser = argparse.ArgumentParser(description='Auto-generate a linked TOC for a markdown doc')
parser.add_argument('filename', type=str, help='markdown file')
args = parser.parse_args()
# open the file
doc = urllib.urlopen(args.filename)
# get lines with headings
taglist = []
for line in doc:
if '#' in line:
taglist.append(line)
# remove new lines, tags, and whitespaces
for tag in taglist:
clean_tag = tag.replace('\n','').replace('#','').strip()
level = tag.count('#') - 1
# omit h1 tags
if level is 0:
continue
# remove capitals and hypenate spaces
link = clean_tag.lower().replace(' ', '-')
# remove punctuation
link = re.sub(punctuation_regex, '', link)
print '\t'*(level - 1) + '- [' + clean_tag + '](#'+ link + ')'