-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgitbook-plugin-summary.py
161 lines (137 loc) · 5.28 KB
/
gitbook-plugin-summary.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
# -*- coding: utf-8 -*-
import argparse
import os
import re
def output_markdown(dire, base_dir, output_file, append, iter_depth=0):
"""Main iterator for get information from every file/folder
i: directory, base directory(to calulate relative path),
output file name, iter depth.
p: Judge is directory or is file, then process .md/.markdown files.
o: write .md information (with identation) to output_file.
"""
ignores = ['_book', 'docs', 'images', 'node_modules', 'dict', '.git']
for filename in sort_dir_file(os.listdir(dire), base_dir):
# add list and sort
if filename in ignores:
print('continue ', filename) # output log
continue
print('Processing ', filename) # output log
file_or_path = os.path.join(dire, filename)
if os.path.isdir(file_or_path): #is dir
if mdfile_in_dir(file_or_path):
# if there is .md files in the folder, output folder name
# output_file.write(' ' * iter_depth + '* ' + filename + '\n')
createRead0(file_or_path, '0-README.md')
output_file.write(' ' * iter_depth + '* [{}]({}/{})\n'.format(
filename, filename, '0-README.md'))
output_markdown(file_or_path, base_dir, output_file, append,
iter_depth + 1) # iteration
else: # is file
if is_markdown_file(filename):
# re to find target markdown files, $ for matching end of filename
if (filename not in [
'SUMMARY.md', 'SUMMARY-GitBook-auto-summary.md',
'0-README.md', 'README.md'
]):
#or iter_depth != 0): # escape SUMMARY.md at base directory
output_file.write(
' ' * iter_depth + '* [{}]({})\n'.format(
write_md_filename(filename, append),
os.path.join(
os.path.relpath(dire, base_dir), filename)))
# iter depth for indent, relpath and join to write link.
def mdfile_in_dir(dire):
"""判断目录中是否有MD文件
"""
for root, dirs, files in os.walk(dire):
for filename in files:
if re.search('.md$|.markdown$', filename):
return True
return False
def is_markdown_file(filename):
""" 判断文件名是.Markdown
i: filename
o: filename without '.md' or '.markdown'
"""
match = re.search('.md$|.markdown$', filename)
if not match:
return False
elif len(match.group()) is len('.md'):
return filename[:-3]
elif len(match.group()) is len('.markdown'):
return filename[:-9]
#create 0-README.md
def createRead0(dir_input, filename):
readmeFile = open(os.path.join(dir_input, filename), 'w')
readmeFile.close()
print('createRead0 ', filename) # output log
def sort_dir_file(listdir, dire):
# sort dirs and files, first files a-z, then dirs a-z
list_of_file = []
list_of_dir = []
for filename in listdir:
if os.path.isdir(os.path.join(dire, filename)):
list_of_dir.append(filename)
else:
list_of_file.append(filename)
for dire in list_of_dir:
list_of_file.append(dire)
list_of_file.sort(key=str.lower)
return list_of_file
def write_md_filename(filename, append):
""" write markdown filename
i: filename and append
p: if append: find former list name and return
else: write filename
"""
if append:
for line in former_summary_list:
if re.search(filename, line):
s = re.search('\[.*\]\(', line)
return s.group()[1:-2]
else:
return is_markdown_file(filename)
else:
return is_markdown_file(filename)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-o',
'--overwrite',
help='overwrite on SUMMARY.md',
action="store_true")
parser.add_argument(
'-a', '--append', help='append on SUMMARY.md', action="store_true")
parser.add_argument('directory', help='the directory of your GitBook root')
args = parser.parse_args()
overwrite = args.overwrite
append = args.append
dir_input = args.directory
# print information
print('GitBook auto summary:', dir_input)
if overwrite:
print('--overwrite')
if append and os.path.exists(os.path.join(dir_input, 'SUMMARY.md')):
#append: read former SUMMARY.md
print('--append')
global former_summary_list
with open(os.path.join(dir_input, 'SUMMARY.md')) as f:
former_summary_list = f.readlines()
f.close()
print()
# output to flie
if (overwrite == False
and os.path.exists(os.path.join(dir_input, 'SUMMARY.md'))):
# overwrite logic
filename = 'SUMMARY-GitBook-auto-summary.md'
else:
filename = 'SUMMARY.md'
output = open(os.path.join(dir_input, filename), 'w')
output.write('# Summary\n\n')
output.write('* [README](./README.md)\n')
output_markdown(dir_input, dir_input, output, append)
output.close()
print('GitBook auto summary finished:) ')
return 0
if __name__ == '__main__':
main()