Skip to content

Commit 468bbc3

Browse files
committed
完成文件的过滤删除和排序
1 parent 5793ae5 commit 468bbc3

File tree

2 files changed

+68
-168
lines changed

2 files changed

+68
-168
lines changed

blog/generate/buildSidebar.py

Lines changed: 68 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,68 @@
22
from os.path import splitext, basename, join, isdir, relpath, abspath
33
from os import listdir
44

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
513

6-
base_dir = None
7-
start_with = None
8-
show_file = None
9-
ignore_file_name = None
10-
include_start_with = None
1114

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):
4516
"""
46-
获取文件名(不包括扩展名)
17+
是否需要生成文件
18+
1. 扩展名不是md的不生成
19+
2. 不是README.md _sidebar.md。不生成
20+
3. 在跳过列表里的不生成
4721
:param file_path: 文件路径
4822
:return: 文件名(不包括扩展名)
4923
"""
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':
5326
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:
5530
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
5640
return True
5741

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
5954

6055
def build_next_level(base_path):
6156
'''
6257
创建下一级节点的目录_sidebar.md
6358
todo:排除子目录下没有md文件的子目录
6459
'''
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()
6762
result = "\n"
6863
for item in items:
6964
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)
7267
readme_path = os.path.join(rel_path,"README.md")
7368
result += "- [" + item + "](" + readme_path + ')\n'
7469

@@ -83,8 +78,8 @@ def build_full_level(base_path):
8378
'''
8479
创建所有子节点的目录_sidebar.md
8580
'''
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,'')
8883
if '' == result:
8984
return
9085
basename = os.path.basename(base_path)
@@ -93,19 +88,17 @@ def build_full_level(base_path):
9388
f.write('## '+ basename + '\n')
9489
f.write(result)
9590

96-
def deep_traverse(base_dir,prefix):
91+
def deep_traverse(base_path,prefix):
9792
'''
9893
深度递归遍历
9994
'''
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'
10698
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+' ')
109102
if '' == result:
110103
return ''
111104
return title + result
@@ -115,7 +108,9 @@ def build_readme_now(base_path):
115108
创建当前节点的readme文件,指向当前目录下的md文件。
116109
深度小于depth+1 就要生成readme文件
117110
'''
118-
print("build readme now:"+base_dir)
111+
if not good_dir(base_path):
112+
return
113+
print("build readme now:"+base_path)
119114
readme_path = os.path.join(base_path, 'README.md')
120115

121116
basename = os.path.basename(base_path)
@@ -126,119 +121,46 @@ def build_readme_now(base_path):
126121
f.write(build_md_items('',base_path))
127122

128123

124+
129125
def build_md_items(prefix,base_path):
130126
'''
131127
todo:排除前缀不符合需求的文件
132128
'''
133129
items = os.listdir(base_path)
134130
result = "\n"
135131
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):
138134
result += build_md_item(prefix,item)
139135
return result
140136

141137
def build_md_item(prefix,file_path):
138+
if not good_file(file_path):
139+
return ''
142140
base_name = os.path.basename(file_path)
143141
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'
145143

146144

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)
149147
'''
150148
now=depth创建递归目录,不再按层遍历
151149
'''
152150
if now >= depth:
153-
build_full_level(root)
151+
build_full_level(base_path)
154152
return
155153

156154
'''
157155
now<depth创建下层目录,并继续递归
158156
'''
159-
build_next_level(root)
160-
items = listdir(root)
157+
build_next_level(base_path)
158+
items = listdir(base_path)
161159
for item in items:
162-
node = join(root, item)
160+
node = join(base_path, item)
163161
if isdir(node):
164162
layer_traverse(node,now+1,depth)
165163

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
242164

243165

244166
if __name__ == "__main__":
@@ -247,9 +169,4 @@ class Node:
247169
第n-1层会生成n层目录的_sidebar
248170
第n层生成之后所有目录的_sidebar
249171
'''
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)

blog/generate/config.ini

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)