Skip to content

Commit 2c05d71

Browse files
committed
生成脚本完成
1 parent 468bbc3 commit 2c05d71

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

blog/generate/buildSidebar.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from configparser import ConfigParser
22
from os.path import splitext, basename, join, isdir, relpath, abspath
33
from os import listdir
4+
import os
45

56
# docsify根目录
6-
root_dir=/root/gitee/notes/blog
7+
root_dir='/root/gitee/notes/blog'
78
# 要处理的文件或文件夹
89
exclude_start_with=['_','*','.']
910
exclude_file = ['readme.md']
@@ -30,23 +31,23 @@ def good_file(base_path):
3031
return False
3132

3233
for item in exclude_start_with:
33-
if base_name.starts_with(item):
34+
if base_name.startswith(item):
3435
return False
3536

36-
rel_path = relpath(root_dir, base_path)
37+
rel_path = relpath(base_path,root_dir)
3738
for item in exclude_dir:
3839
if rel_path.startswith(item):
3940
return False
4041
return True
4142

4243
def good_dir(base_path):
43-
rel_path = relpath(root_dir, base_path)
44+
rel_path = relpath(base_path,root_dir)
4445
for item in exclude_dir:
4546
if rel_path.startswith(item):
4647
return False
47-
for dirpath, dirnames, filenames in os.walk(root_dir):
48+
for dirpath, dirnames, filenames in os.walk(base_path):
4849
for filename in filenames:
49-
abspath = os.path.join(dirpath):
50+
abspath = os.path.join(dirpath,filename)
5051
if good_file(abspath):
5152
return True
5253

@@ -57,13 +58,13 @@ def build_next_level(base_path):
5758
创建下一级节点的目录_sidebar.md
5859
todo:排除子目录下没有md文件的子目录
5960
'''
60-
print("build next level:"+base_path)
61-
items = os.listdir(base_path).sort()
61+
items = sorted(os.listdir(base_path))
6262
result = "\n"
63+
print("build next level:"+base_path + ",items:",items)
6364
for item in items:
6465
abspath = os.path.join(base_path,item)
6566
if isdir(abspath) and good_dir(abspath):
66-
rel_path = relpath(root_dir, abspath)
67+
rel_path = relpath(abspath,root_dir)
6768
readme_path = os.path.join(rel_path,"README.md")
6869
result += "- [" + item + "](" + readme_path + ')\n'
6970

@@ -93,10 +94,12 @@ def deep_traverse(base_path,prefix):
9394
深度递归遍历
9495
'''
9596
if os.path.isfile(base_path):
97+
if not good_file(base_path):
98+
return ''
9699
return build_md_item(prefix,base_path)
97100
title = prefix + '- ' + os.path.basename(base_path) + '\n'
98101
result = ''
99-
for item in os.listdir(base_path).sort():
102+
for item in sorted(os.listdir(base_path)):
100103
abspath = os.path.join(base_path,item)
101104
result += deep_traverse(abspath,prefix+' ')
102105
if '' == result:
@@ -126,20 +129,20 @@ def build_md_items(prefix,base_path):
126129
'''
127130
todo:排除前缀不符合需求的文件
128131
'''
129-
items = os.listdir(base_path)
130132
result = "\n"
131-
for item in items:
133+
for item in sorted(os.listdir(base_path)):
132134
abspath = join(base_path, item)
133-
if os.path.isfile(abspath):
134-
result += build_md_item(prefix,item)
135+
if os.path.isfile(abspath) and good_file(abspath):
136+
result += build_md_item(prefix,abspath)
135137
return result
136138

137139
def build_md_item(prefix,file_path):
138-
if not good_file(file_path):
139-
return ''
140+
140141
base_name = os.path.basename(file_path)
141142
title = os.path.splitext(base_name)[0]
142-
return prefix + "- [" + title + "](" + relpath(root_dir, file_path) + ')\n'
143+
rel_path = relpath(file_path,root_dir)
144+
print(root_dir,file_path,rel_path)
145+
return prefix + "- [" + title + "](" + rel_path + ')\n'
143146

144147

145148
def layer_traverse(base_path,now,depth):

0 commit comments

Comments
 (0)