-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-toc.py
233 lines (165 loc) · 8.48 KB
/
generator-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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-08-26
# @copyright by hoojo @2018
# @changelog Generator Project makedown —— TOC
import os
import re
#===============================================================================
# Generator Project makedown —— TOC table of Contents
#===============================================================================
# 描述:生成项目工程的 makedown 格式的 目录索引 TOC 的文档
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# 生成 目录索引 TOC 的文档 工具类
#-------------------------------------------------------------------------------
class GeneratorTOCUtils:
__rootDirectory = ".";
__makedownFile = ""
__tableOfContents = []
__language = "Python"
def __init__(self, rootDirectory, makedownFile, language="Python"):
self.__rootDirectory = rootDirectory
self.__makedownFile = makedownFile
self.__language = language
print("目标工程位置:%s,生成文件保存位置:%s" % (rootDirectory, makedownFile))
#-------------------------------------------------------------------------------
# generator makedown toc file
#-------------------------------------------------------------------------------
def genMakedownTOC(self, suffix):
self.__scan(self.__rootDirectory, suffix)
self.__save()
#-------------------------------------------------------------------------------
# generator makedown readME file
#-------------------------------------------------------------------------------
def genMakedownReadMe(self, suffix):
self.__scan(self.__rootDirectory, suffix, isReadMe=True)
self.__save()
# 保存文件
def __save(self):
with open(self.__makedownFile, "w", encoding=u'utf-8') as file:
file.seek(0)
file.truncate() #清空文件
file.writelines(self.__tableOfContents)
file.close()
def is_chinese(self, uchar):
#zh_pattern = re.compile(u'[\u4e00-\u9fa5]+')
#global zh_pattern
return re.compile(u'[\u4e00-\u9fa5]+').search(uchar)
"""判断一个unicode是否是汉字
if uchar >= u'/u4e00' and uchar <= u'/u9fa5':
return True
else:
return False
"""
def __makeFolderChapter(self, path, isReadMe=False):
path = path.replace(self.__rootDirectory, "").replace("\\", "/")
if isReadMe:
return "# %s \n[**%s**](%s)\n" % (path, path, "./" + path)
else:
return "+ [**%s**](%s)\n" % (path, "./" + path)
def __makeFileChapter(self, path, name, suffix, isReadMe=False):
path = path.replace(self.__rootDirectory, "").replace("\\", "/")
if isReadMe:
return "## %s \n[`%s`](%s)\n" % (name.replace(suffix, ""), name.replace(suffix, ""), "./" + path + "/" + name)
else:
return "\t- [`%s`](%s)\n" % (name.replace(suffix, ""), "./" + path + "/" + name)
def __makeCommentChapter(self, path, name, line, comment):
path = path.replace(self.__rootDirectory, "").replace("\\", "/")
return "\t\t+ [%s#L%d](%s#L%d)\n" % (comment, line, "./" + path + "/" + name, line)
def __makeContentChapter(self, contents):
data = ""
for content in contents:
data += content
return "\n\r```%s \n\r%s \r\n```\n\r" % (self.__language, data)
def __fetchContent(self, file):
contents, lineNumber = [], 0
with open(file, "r", encoding=u'utf-8') as file:
line = file.readline()
lineNumber += 1
while line != "":
if lineNumber >= 10:
contents.append(line)
if lineNumber >= 50:
break
line = file.readline()
lineNumber += 1
return contents
def __fetchComments(self, file):
comments, lineNumber = {}, 0
with open(file, "r", encoding=u'utf-8') as file:
line = file.readline()
lineNumber += 1
while len(line) > 0:
if line.startswith("#") and not line.startswith("#-") and not line.startswith("# -") and not line.startswith("#=") and not line.startswith("# =") and lineNumber >= 9:
if lineNumber not in comments and (lineNumber - 1) not in comments:
line = line.replace("#", "").replace("\n", "").strip()
first, last = line[0:1], line[len(line) - 1:]
if self.is_chinese(first) or self.is_chinese(last):
comments[lineNumber] = line
line = file.readline()
lineNumber += 1
return comments
#-------------------------------------------------------------------------------
# each target folder, add comment to python file
#-------------------------------------------------------------------------------
def __scan(self, dir, suffix, isReadMe=False):
for parent, dirs, files in os.walk(dir, topdown=False):
if parent.find(".git") != -1:
continue
if parent.find(".settings") != -1:
continue
if parent.find("pycache") != -1:
continue
print('parent: %s' % parent)
chapter = self.__makeFolderChapter(parent, isReadMe)
self.__tableOfContents.append(chapter)
for name in files:
file = os.path.join(parent, name)
if file.endswith(suffix) == False:
continue
print('files: %s' % name)
chapter = self.__makeFileChapter(parent, name, suffix, isReadMe)
self.__tableOfContents.append(chapter)
if isReadMe:
contents = self.__fetchContent(file)
content = self.__makeContentChapter(contents)
self.__tableOfContents.append(content)
else:
comments = self.__fetchComments(file)
for line, comment in comments.items():
content = self.__makeCommentChapter(parent, name, line, comment)
print(content)
self.__tableOfContents.append(content)
#-------------------------------------------------------------------------------
# each target folder, add comment to python file
#-------------------------------------------------------------------------------
def __scanFile(self, dir, suffix):
for root, dirs, files in os.walk(dir, topdown=False):
for name in files:
file = os.path.join(root, name)
if file.endswith(suffix) == False:
continue
if file.find(".git") != -1:
continue
print('files: %s' % file)
self.__tableOfContents.append(file + "\n")
def __scanFolder(self, dirs):
for name in dirs:
folder = os.path.join(root, name)
if folder.find(".git") != -1:
continue
if folder.find(".settings") != -1:
continue
print('dirs: %s' % folder)
#self.scanFile(folder, suffix)
#util = GeneratorTOCUtils("F:\\Example Exercise\\Python\\", "F:\\Example Exercise\\Python\\readme.md")
#util.genMakedownTOC(".py")
#util.genMakedownReadMe(".py")
util = GeneratorTOCUtils("F:\\Example Exercise\\Bash\\", "F:\\Example Exercise\\Bash\\readme.md")
util.genMakedownTOC(".sh")
#util = GeneratorTOCUtils("F:\\Example Exercise\\Bash\\", "F:\\Example Exercise\\Bash\\tutorial.md", "bash")
#util.genMakedownReadMe(".sh")