Skip to content

Commit c64e1a3

Browse files
committed
完成初版的侧边栏和readme生成
1 parent 2c05d71 commit c64e1a3

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

blog/docsify/build_sidebar.py

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
from configparser import ConfigParser
2+
from os.path import splitext, basename, join, isdir, relpath, abspath
3+
from os import listdir
4+
import os
5+
6+
# docsify根目录
7+
root_dir='/root/gitee/notes'
8+
# 要处理的文件或文件夹
9+
exclude_start_with=['_','*','.']
10+
exclude_file = ['readme.md']
11+
exclude_dir = ['.vscode','.git']
12+
# 想要在几级目录生成文件,默认"0"表示在根目录生成,可以配合侧边栏折叠插件使用
13+
create_depth=1
14+
15+
16+
def good_file(base_path):
17+
"""
18+
是否需要生成文件
19+
1. 扩展名不是md的不生成
20+
2. 不是README.md _sidebar.md。不生成
21+
3. 在跳过列表里的不生成
22+
:param file_path: 文件路径
23+
:return: 文件名(不包括扩展名)
24+
"""
25+
file_extension = splitext(base_path)[1]
26+
if file_extension != '.md':
27+
return False
28+
29+
base_name = os.path.basename(base_path)
30+
if base_name.lower() in exclude_file:
31+
return False
32+
33+
for item in exclude_start_with:
34+
if base_name.startswith(item):
35+
return False
36+
37+
rel_path = relpath(base_path,root_dir)
38+
for item in exclude_dir:
39+
if rel_path.startswith(item):
40+
return False
41+
return True
42+
43+
def good_dir(base_path):
44+
rel_path = relpath(base_path,root_dir)
45+
for item in exclude_dir:
46+
if rel_path.startswith(item):
47+
return False
48+
for dirpath, dirnames, filenames in os.walk(base_path):
49+
for filename in filenames:
50+
abspath = os.path.join(dirpath,filename)
51+
if good_file(abspath):
52+
return True
53+
54+
return False
55+
56+
def build_next_level(base_path):
57+
'''
58+
创建下一级节点的目录_sidebar.md
59+
todo:排除子目录下没有md文件的子目录
60+
'''
61+
items = sorted(os.listdir(base_path))
62+
result = "\n"
63+
print("build next level:"+base_path + ",items:",items)
64+
for item in items:
65+
abspath = os.path.join(base_path,item)
66+
if isdir(abspath) and good_dir(abspath):
67+
rel_path = relpath(abspath,root_dir)
68+
readme_path = os.path.join(rel_path,"README.md")
69+
result += "- [" + item + "](" + readme_path.replace(' ','%20') + ')\n'
70+
71+
basename = os.path.basename(base_path)
72+
# 如果README.md文件不存在,则创建
73+
sidebar_path = os.path.join(base_path,"_sidebar.md")
74+
with open(sidebar_path, 'w') as f:
75+
f.write('## '+ basename + '\n')
76+
f.write(result)
77+
78+
def build_full_level(base_path):
79+
'''
80+
创建所有子节点的目录_sidebar.md
81+
'''
82+
print("build full path:"+base_path)
83+
result = deep_traverse(base_path,'')
84+
if '' == result:
85+
return
86+
basename = os.path.basename(base_path)
87+
sidebar_path = os.path.join(base_path,"_sidebar.md")
88+
with open(sidebar_path, 'w') as f:
89+
f.write('## '+ basename + '\n')
90+
f.write(result)
91+
92+
def deep_traverse(base_path,prefix):
93+
'''
94+
深度递归遍历
95+
'''
96+
if os.path.isfile(base_path):
97+
if not good_file(base_path):
98+
return ''
99+
return build_md_item(prefix,base_path)
100+
title = prefix + '- ' + os.path.basename(base_path) + '\n'
101+
result = ''
102+
for item in sorted(os.listdir(base_path)):
103+
abspath = os.path.join(base_path,item)
104+
result += deep_traverse(abspath,prefix+' ')
105+
if '' == result:
106+
return ''
107+
return title + result
108+
109+
def build_readme_now(base_path):
110+
'''
111+
创建当前节点的readme文件,指向当前目录下的md文件。
112+
深度小于depth+1 就要生成readme文件
113+
'''
114+
if not good_dir(base_path):
115+
return
116+
print("build readme now:"+base_path)
117+
readme_path = os.path.join(base_path, 'README.md')
118+
119+
basename = os.path.basename(base_path)
120+
# 如果README.md文件不存在,则创建
121+
if not os.path.exists(readme_path):
122+
with open(readme_path, 'w') as f:
123+
f.write('## '+ basename + '\n')
124+
f.write(build_md_items('',base_path))
125+
126+
127+
128+
def build_md_items(prefix,base_path):
129+
'''
130+
todo:排除前缀不符合需求的文件
131+
'''
132+
result = "\n"
133+
for item in sorted(os.listdir(base_path)):
134+
abspath = join(base_path, item)
135+
if os.path.isfile(abspath) and good_file(abspath):
136+
result += build_md_item(prefix,abspath)
137+
return result
138+
139+
def build_md_item(prefix,file_path):
140+
141+
base_name = os.path.basename(file_path)
142+
title = os.path.splitext(base_name)[0]
143+
rel_path = relpath(file_path,root_dir)
144+
print(root_dir,file_path,rel_path)
145+
return prefix + "- [" + title + "](" + rel_path.replace(' ','%20') + ')\n'
146+
147+
148+
def layer_traverse(base_path,now,depth):
149+
build_readme_now(base_path)
150+
'''
151+
now=depth创建递归目录,不再按层遍历
152+
'''
153+
if now >= depth:
154+
build_full_level(base_path)
155+
return
156+
157+
'''
158+
now<depth创建下层目录,并继续递归
159+
'''
160+
build_next_level(base_path)
161+
items = listdir(base_path)
162+
for item in items:
163+
node = join(base_path, item)
164+
if isdir(node):
165+
layer_traverse(node,now+1,depth)
166+
167+
168+
169+
if __name__ == "__main__":
170+
'''
171+
如果默认n层目录
172+
第n-1层会生成n层目录的_sidebar
173+
第n层生成之后所有目录的_sidebar
174+
'''
175+
layer_traverse(root_dir,0,create_depth)

0 commit comments

Comments
 (0)