forked from iphysresearch/DataSciComp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.py
executable file
·78 lines (71 loc) · 2.79 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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#####################################################
# ghtoc : a markdown toc generator
# Author : Kai Yuan <[email protected]>
# Usage : toc.py <markdown file>
# Date : 2014-12-02
# License :
#Copyright (C) 2014 Kai Yuan
#
#Permission is hereby granted, free of charge, to any person obtaining
#a copy of this software and associated documentation files (the "Software"),
#to deal in the Software without restriction, including without limitation
#the rights to use, copy, modify, merge, publish, distribute, sublicense,
#and/or sell copies of the Software, and to permit persons to whom the
#Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included
#in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
#DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
#OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#####################################################
import re
import sys
import shutil,datetime
top_level=77
lnk_temp='%s- [%s](#%s)'
TOC='#### Table of contents'
REF='(toc generated by [ghtoc](https://github.com/sk1418/ghtoc))'
def generate_toc(fname):
global top_level
lines = []
with open(fname, 'r') as file:
lines = file.readlines()
ts = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
destf= '.'.join((fname,ts,'bak'))
shutil.copy(fname, destf)
print "Backup was created: [%s]"%destf
headers = [e.rstrip() for e in lines if re.match(r'#+', e)]
#find top_level
for i,h in enumerate(headers):
ln = len(re.search(r'^#+',h).group(0))
top_level = ln if ln < top_level else top_level
headers[i] = re.sub(r'^#+\s*', str(ln)+' ', h)
headers = [tr_header(h) for h in headers]
with open(fname,'w') as f:
f.write(TOC+'\n')
f.write(REF+'\n')
f.write('\n'.join(headers) + '\n')
f.write(''.join(lines) + '\n')
f.write('\n\n')
def tr_header(header):
global lnk_temp
lvl, txt = re.findall(r'^(\d+) (.*)', header)[0]
# return lnk_temp%((int(lvl)-top_level)*' ', txt, re.sub(' ','-',re.sub('[^-a-z0-9 ]','',txt.lower())))
return lnk_temp%((int(lvl)-top_level)*' ', txt, re.sub(' ','-',txt))
if __name__ == '__main__':
if len(sys.argv)<2:
print """
Usage:
toc.py <markdown file>
"""
else:
infile = sys.argv[1]
generate_toc(infile)