-
Notifications
You must be signed in to change notification settings - Fork 38
/
toc.py
132 lines (107 loc) · 4.65 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding:utf-8 -*-
import sys
import os
from optparse import OptionParser
class fileConfigElement:
def __init__(self, title="", file=""):
self.title = title
self.file = file
def githubUrl(url):
# 将空格替换为 "-"
url = url.replace(" ", "-")
# 删除掉 specialChars 中的字符,不对 '-' 做任何操作
englishSpecialChars = r'\/"<>@#$%^&*()+,.!:;`='
chineseSpecialChars = r',。、!()?“”:;|'
specialChars = englishSpecialChars + chineseSpecialChars
for specialChar in specialChars:
url = url.replace(specialChar, '')
return url
if __name__ == "__main__":
osConf = fileConfigElement("## 操作系统总结", "document/system/system.md")
shellConf = fileConfigElement("## shell 编程总结", "document/shell/shell.md")
cmdConf = fileConfigElement("## shell 命令行总结", "document/shell/command.md")
gdbConf = fileConfigElement("## gdb 总结", "tools/gdb/gdb.md")
gitConf = fileConfigElement("## git 总结", "tools/git/git.md")
netConf = fileConfigElement("## 计算机网络总结", "document/tcp_ip/network.md")
rootReadmePath = "README.md"
repoLink = "https://github.com/jinbooooom/linux/blob/master/"
confList = [osConf,
shellConf,
cmdConf,
gdbConf,
gitConf,
netConf
]
saveTocPath = "jinbo_toc.md"
with open(saveTocPath, 'w') as fw:
for conf in confList:
print(f"prepare process {conf.file}")
fw.write(f"{conf.title}\n")
filePath, fileName = os.path.split(conf.file)
linkPrefix = repoLink + conf.file
#print(linkPrefix)
skip = 0
with open(conf.file, 'r') as fr:
for readLine in fr.readlines():
# 找到了代码块,就都跳过,不管以 # 开头的内容,因为这在代码区可能指的是注释而不是文本区的标题等级
if readLine.find("```") == 0:
skip = ~skip
continue
if (skip):
continue
if readLine[0] != "#":
continue
level = 0
while readLine[level] == "#":
level += 1
title_from = level
title_end = level
# 去掉末尾的换行和回车
while readLine[title_end] not in "\r\n":
title_end += 1
title = readLine[title_from : title_end]
# 去掉左右空格
title = title.strip()
# 处理含有超链接的标题 [title](link)
idx_start = title.find("[")
idx_end = title.find("](")
if idx_end != -1:
title = title[idx_start + 1 : idx_end]
# 将空格替换为%20
#url = title.replace(' ' , '%20')
url = title
url = githubUrl(url)
blank_str = ""
sign_str = "#"
for i in range(level - 1):
blank_str += " "
sign_str += "#"
# 第一个无序列表不含有空格
blank_str = blank_str[2: ]
title = f"[{title}]({linkPrefix}#{url})"
title = blank_str + " - " + title + '\n'
# print(title)
fw.write(title)
print(f"process {conf.file} done!")
bkRootReadmePath = "bk_" + rootReadmePath
with open(bkRootReadmePath, 'w') as fw:
print(f"extract the contents of the README.md")
with open(rootReadmePath, 'r') as fr:
#print(fr.readlines())
for readLine in fr.readlines():
fw.write(readLine)
if readLine.find("---------------------") == 0:
print("find delimiter ---------------------")
break
print(f"add contents of toc")
with open(saveTocPath, 'r') as fr:
for readLine in fr.readlines():
fw.write(readLine)
print("remove and rename file")
if os.path.exists(saveTocPath):
os.remove(saveTocPath)
if os.path.exists(rootReadmePath):
os.remove(rootReadmePath)
if os.path.exists(bkRootReadmePath):
os.rename(bkRootReadmePath, rootReadmePath)
print("new README.md was generated, everything is done!")