forked from chuenlungwang/cppprimer-note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.py
74 lines (67 loc) · 2.05 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import re
headPattern = re.compile(r"\s*((#+)\s*(.*))")
blockPattern = re.compile(r"\s*(````)")
def listfiles(dir):
files = os.listdir(dir)
files.sort()
for i in xrange(len(files)):
files[i] = dir + "/" + files[i]
return files
def parsefile(f):
ret = {"file":f,"heads":[]}
codeblock = False
for line in file(f):
if codeblock:
m = False
if blockPattern.match(line):
codeblock = False
else:
m = headPattern.match(line)
if blockPattern.match(line):
codeblock = True
if m:
# 移除 Windows 的换行符 \r
hstr = m.group(3).replace('\r', '')
ret['heads'].append((len(m.group(2)), hstr))
return ret
def tomarkdown(fstruct):
str_lst = []
pattern = re.compile(ur"""[!\"#$%&'()*+,./:;<=>?@[\]^`{|}~]""")
str_lst.append("\n## [{}]({})".format(fstruct['file'], fstruct['file']))
for h in fstruct['heads']:
hstr = h[1]
hstr = pattern.sub('', hstr)
hstr = hstr.replace('(', '')
hstr = hstr.replace(')', '')
hstr = hstr.replace(':', '')
hstr = hstr.replace(' ', '-')
str_lst.append("{}- [{}]({}#{})".format(" "*(h[0]-1)*2, h[1], fstruct['file'], hstr))
return '\n'.join(str_lst)
def writetoc(toc, readmef):
content = []
intoc = False
f = open(readmef, 'rw+')
for l in f:
if not intoc:
content.append(l)
if l.strip() == "<!-- TOC START -->":
content.append(toc)
content.append('\n\n')
intoc = True
if l.strip() == "<!-- TOC END -->":
intoc = False
content.append(l)
f.seek(0, os.SEEK_SET)
f.truncate()
f.write("".join(content))
f.close()
if __name__ == "__main__":
toc = []
for f in listfiles("docs"):
fstruct = parsefile(f)
toc.append(tomarkdown(fstruct))
writetoc("\n".join(toc), "./README.md")