2
2
from os .path import splitext , basename , join , isdir , relpath , abspath
3
3
from os import listdir
4
4
5
+ # docsify根目录
6
+ root_dir = / root / gitee / notes / blog
7
+ # 要处理的文件或文件夹
8
+ exclude_start_with = ['_' ,'*' ,'.' ]
9
+ exclude_file = ['readme.md' ]
10
+ exclude_dir = ['.vscode' ,'.git' ]
11
+ # 想要在几级目录生成文件,默认"0"表示在根目录生成,可以配合侧边栏折叠插件使用
12
+ create_depth = 1
5
13
6
- base_dir = None
7
- start_with = None
8
- show_file = None
9
- ignore_file_name = None
10
- include_start_with = None
11
14
12
- out_file_list = []
13
- create_depth = - 1
14
-
15
-
16
- def read_config ():
17
- global base_dir , show_file , start_with , ignore_file_name , ReadmeFile , _sidebarFile , out_file_list , create_depth ,include_start_with
18
-
19
- cf = ConfigParser ()
20
- cf .read ("config.ini" , encoding = 'utf-8' )
21
- base_dir = cf .get ("config" , "base_dir" )
22
- start_with = cf .get ("config" , "ignore_start_with" ).split ("|" )
23
- show_file = cf .get ("config" , "show_file" ).split ('|' )
24
- ignore_file_name = cf .get ("config" , "ignore_file_name" ).split ("|" )
25
- include_start_with = cf .get ("config" , "include_start_with" ).split ("|" )
26
-
27
- out_file_list = cf .get ("outFile" , "eachFile" ).split ("|" )
28
- create_depth = int (cf .get ("outFile" , "create_depth" ))
29
-
30
-
31
- def check_file_extension (file_path ):
32
- """
33
- 检查文件后缀是否为指定的后缀
34
- :param file_path: 文件路径
35
- :return: 如果文件后缀为指定的后缀,返回True;否则返回False
36
- """
37
- file_extension = splitext (file_path )[1 ]
38
- if file_extension in show_file :
39
- return True
40
- else :
41
- return False
42
-
43
-
44
- def check_file_name_satified (file_path ):
15
+ def good_file (base_path ):
45
16
"""
46
- 获取文件名(不包括扩展名)
17
+ 是否需要生成文件
18
+ 1. 扩展名不是md的不生成
19
+ 2. 不是README.md _sidebar.md。不生成
20
+ 3. 在跳过列表里的不生成
47
21
:param file_path: 文件路径
48
22
:return: 文件名(不包括扩展名)
49
23
"""
50
- file_name_with_extension = basename (file_path )
51
- file_name = splitext (file_name_with_extension )[0 ]
52
- if file_name [0 ] in start_with or file_name in ignore_file_name :
24
+ file_extension = splitext (base_path )[1 ]
25
+ if file_extension != '.md' :
53
26
return False
54
- if file_name [0 ] not in include_start_with :
27
+
28
+ base_name = os .path .basename (base_path )
29
+ if base_name .lower () in exclude_file :
55
30
return False
31
+
32
+ for item in exclude_start_with :
33
+ if base_name .starts_with (item ):
34
+ return False
35
+
36
+ rel_path = relpath (root_dir , base_path )
37
+ for item in exclude_dir :
38
+ if rel_path .startswith (item ):
39
+ return False
56
40
return True
57
41
58
-
42
+ def good_dir (base_path ):
43
+ rel_path = relpath (root_dir , base_path )
44
+ for item in exclude_dir :
45
+ if rel_path .startswith (item ):
46
+ return False
47
+ for dirpath , dirnames , filenames in os .walk (root_dir ):
48
+ for filename in filenames :
49
+ abspath = os .path .join (dirpath ):
50
+ if good_file (abspath ):
51
+ return True
52
+
53
+ return False
59
54
60
55
def build_next_level (base_path ):
61
56
'''
62
57
创建下一级节点的目录_sidebar.md
63
58
todo:排除子目录下没有md文件的子目录
64
59
'''
65
- print ("build next level:" + root_path )
66
- items = os .listdir (base_path )
60
+ print ("build next level:" + base_path )
61
+ items = os .listdir (base_path ). sort ()
67
62
result = "\n "
68
63
for item in items :
69
64
abspath = os .path .join (base_path ,item )
70
- if isdir (abspath ):
71
- rel_path = relpath (root_dir , base_dir )
65
+ if isdir (abspath ) and good_dir ( abspath ) :
66
+ rel_path = relpath (root_dir , abspath )
72
67
readme_path = os .path .join (rel_path ,"README.md" )
73
68
result += "- [" + item + "](" + readme_path + ')\n '
74
69
@@ -83,8 +78,8 @@ def build_full_level(base_path):
83
78
'''
84
79
创建所有子节点的目录_sidebar.md
85
80
'''
86
- print ("build full path" + root_path )
87
- result = deep_traverse (base_dir ,'' )
81
+ print ("build full path:" + base_path )
82
+ result = deep_traverse (base_path ,'' )
88
83
if '' == result :
89
84
return
90
85
basename = os .path .basename (base_path )
@@ -93,19 +88,17 @@ def build_full_level(base_path):
93
88
f .write ('## ' + basename + '\n ' )
94
89
f .write (result )
95
90
96
- def deep_traverse (base_dir ,prefix ):
91
+ def deep_traverse (base_path ,prefix ):
97
92
'''
98
93
深度递归遍历
99
94
'''
100
- if os .path .isfile (base_dir )
101
- if base_dir .endwith ('.md' )
102
- return build_md_item (prefix ,base_dir )
103
- else :
104
- return ''
105
- title = prefix + '- ' + os .path .basename (base_dir ) + '\n '
95
+ if os .path .isfile (base_path ):
96
+ return build_md_item (prefix ,base_path )
97
+ title = prefix + '- ' + os .path .basename (base_path ) + '\n '
106
98
result = ''
107
- for items in os .listdir (base_dir ):
108
- result += deep_traverse (base_dir ,prefix + ' ' )
99
+ for item in os .listdir (base_path ).sort ():
100
+ abspath = os .path .join (base_path ,item )
101
+ result += deep_traverse (abspath ,prefix + ' ' )
109
102
if '' == result :
110
103
return ''
111
104
return title + result
@@ -115,7 +108,9 @@ def build_readme_now(base_path):
115
108
创建当前节点的readme文件,指向当前目录下的md文件。
116
109
深度小于depth+1 就要生成readme文件
117
110
'''
118
- print ("build readme now:" + base_dir )
111
+ if not good_dir (base_path ):
112
+ return
113
+ print ("build readme now:" + base_path )
119
114
readme_path = os .path .join (base_path , 'README.md' )
120
115
121
116
basename = os .path .basename (base_path )
@@ -126,119 +121,46 @@ def build_readme_now(base_path):
126
121
f .write (build_md_items ('' ,base_path ))
127
122
128
123
124
+
129
125
def build_md_items (prefix ,base_path ):
130
126
'''
131
127
todo:排除前缀不符合需求的文件
132
128
'''
133
129
items = os .listdir (base_path )
134
130
result = "\n "
135
131
for item in items :
136
- abspath = join (root , item )
137
- if abspath is not dir :
132
+ abspath = join (base_path , item )
133
+ if os . path . isfile ( abspath ) :
138
134
result += build_md_item (prefix ,item )
139
135
return result
140
136
141
137
def build_md_item (prefix ,file_path ):
138
+ if not good_file (file_path ):
139
+ return ''
142
140
base_name = os .path .basename (file_path )
143
141
title = os .path .splitext (base_name )[0 ]
144
- return prefix + "- [" + title + "](" + relpath (root_dir , base_dir ) + ')\n '
142
+ return prefix + "- [" + title + "](" + relpath (root_dir , file_path ) + ')\n '
145
143
146
144
147
- def layer_traverse (root ,now ,depth ):
148
- build_readme_now (root )
145
+ def layer_traverse (base_path ,now ,depth ):
146
+ build_readme_now (base_path )
149
147
'''
150
148
now=depth创建递归目录,不再按层遍历
151
149
'''
152
150
if now >= depth :
153
- build_full_level (root )
151
+ build_full_level (base_path )
154
152
return
155
153
156
154
'''
157
155
now<depth创建下层目录,并继续递归
158
156
'''
159
- build_next_level (root )
160
- items = listdir (root )
157
+ build_next_level (base_path )
158
+ items = listdir (base_path )
161
159
for item in items :
162
- node = join (root , item )
160
+ node = join (base_path , item )
163
161
if isdir (node ):
164
162
layer_traverse (node ,now + 1 ,depth )
165
163
166
-
167
-
168
-
169
-
170
- def save_structure (root_dir , base_dir = base_dir , depth = 0 ):
171
- """
172
- 遍历指定目录及其所有子目录,生成并保存目录结构。
173
- 遍历方案:按层遍历当前目录,如果层数小于depth则只生成下一层的目录。如果等于depth则生成所有子节点的目录。
174
- :param root_dir: 要处理的根目录路径
175
- :param base_dir: 用来获得root_dir对base_dir的相对路径
176
- :param depth: 递归深度,文件夹深度。0表示根目录只在最开始生辰_sidebar,1表示两级目录
177
- """
178
- root = root_dir
179
- dirs = []
180
- files = []
181
- i = 0
182
- for item in listdir (root ):
183
- if isdir (join (root , item )):
184
- dirs .append (item )
185
- else :
186
- files .append (item )
187
- subdir_structure = ''
188
- subdir_name = basename (root )
189
-
190
-
191
-
192
-
193
- if depth != 0 :
194
- if create_depth == 0 :
195
- subdir_structure += "- " + subdir_name + '\n '
196
- else :
197
- subdir_structure += "- [" + subdir_name + "](" + relpath (root , base_dir ) + ')\n '
198
- else :
199
- if create_depth == 0 :
200
- subdir_structure += "- " + "首页" + '\n '
201
- else :
202
- subdir_structure += "- [" + "首页" + "](" + relpath (root , base_dir ) + ')\n '
203
-
204
- for file in files :
205
- if check_file_name_satified (join (root , file )):
206
- if check_file_extension (file ):
207
- subdir_structure += " " + "- [" + file + "](" + relpath (join (root , file ),
208
- base_dir ) + ')\n '
209
-
210
- for subdir in dirs :
211
- subdir_path = join (root , subdir )
212
- if check_file_name_satified (subdir_path ):
213
- next_struct = save_structure (subdir_path , base_dir , depth + 1 )
214
- next_struct = next_struct [:- 1 ] if next_struct .endswith ("\n " ) else next_struct
215
- next_struct = next_struct .replace ("\n " , "\n " ) + "\n "
216
- subdir_structure += " " + next_struct
217
-
218
- back_struct = subdir_structure
219
- if depth == 1 :
220
- subdir_structure = "- [" + "返回首页" + "](" + "" + '?id=main)\n ' + subdir_structure
221
- elif depth != 0 :
222
- abs_pre_path = abspath (join (root , ".." ))
223
- rel_pre_path = relpath (abs_pre_path , base_dir )
224
- subdir_structure = "- [" + "返回上一级" + "](" + rel_pre_path + ')\n ' + subdir_structure
225
-
226
- subdir_structure = subdir_structure .replace ('\\ ' , '/' )
227
- print ("%s : finished" % root_dir )
228
- if create_depth == - 1 :
229
- for file_name in out_file_list :
230
- with open (join (root , file_name ), 'w' , encoding = "utf-8" ) as f :
231
- f .write (subdir_structure )
232
- else :
233
- if depth == 0 :
234
- for file_name in out_file_list :
235
- with open (join (root , file_name ), 'w' , encoding = "utf-8" ) as f :
236
- f .write (subdir_structure )
237
- return back_struct
238
-
239
-
240
- class Node :
241
- pass
242
164
243
165
244
166
if __name__ == "__main__" :
@@ -247,9 +169,4 @@ class Node:
247
169
第n-1层会生成n层目录的_sidebar
248
170
第n层生成之后所有目录的_sidebar
249
171
'''
250
- import os
251
- read_config ()
252
- os .chdir (base_dir )
253
- print ("cwd:" + os .getcwd ())
254
- layer_traverse (base_dir ,0 ,create_depth )
255
- # save_structure(base_dir, base_dir, 0)
172
+ layer_traverse (root_dir ,0 ,create_depth )
0 commit comments