1
1
from configparser import ConfigParser
2
2
from os .path import splitext , basename , join , isdir , relpath , abspath
3
3
from os import listdir
4
+ import os
4
5
5
6
# docsify根目录
6
- root_dir = / root / gitee / notes / blog
7
+ root_dir = ' /root/gitee/notes/blog'
7
8
# 要处理的文件或文件夹
8
9
exclude_start_with = ['_' ,'*' ,'.' ]
9
10
exclude_file = ['readme.md' ]
@@ -30,23 +31,23 @@ def good_file(base_path):
30
31
return False
31
32
32
33
for item in exclude_start_with :
33
- if base_name .starts_with (item ):
34
+ if base_name .startswith (item ):
34
35
return False
35
36
36
- rel_path = relpath (root_dir , base_path )
37
+ rel_path = relpath (base_path , root_dir )
37
38
for item in exclude_dir :
38
39
if rel_path .startswith (item ):
39
40
return False
40
41
return True
41
42
42
43
def good_dir (base_path ):
43
- rel_path = relpath (root_dir , base_path )
44
+ rel_path = relpath (base_path , root_dir )
44
45
for item in exclude_dir :
45
46
if rel_path .startswith (item ):
46
47
return False
47
- for dirpath , dirnames , filenames in os .walk (root_dir ):
48
+ for dirpath , dirnames , filenames in os .walk (base_path ):
48
49
for filename in filenames :
49
- abspath = os .path .join (dirpath ):
50
+ abspath = os .path .join (dirpath , filename )
50
51
if good_file (abspath ):
51
52
return True
52
53
@@ -57,13 +58,13 @@ def build_next_level(base_path):
57
58
创建下一级节点的目录_sidebar.md
58
59
todo:排除子目录下没有md文件的子目录
59
60
'''
60
- print ("build next level:" + base_path )
61
- items = os .listdir (base_path ).sort ()
61
+ items = sorted (os .listdir (base_path ))
62
62
result = "\n "
63
+ print ("build next level:" + base_path + ",items:" ,items )
63
64
for item in items :
64
65
abspath = os .path .join (base_path ,item )
65
66
if isdir (abspath ) and good_dir (abspath ):
66
- rel_path = relpath (root_dir , abspath )
67
+ rel_path = relpath (abspath , root_dir )
67
68
readme_path = os .path .join (rel_path ,"README.md" )
68
69
result += "- [" + item + "](" + readme_path + ')\n '
69
70
@@ -93,10 +94,12 @@ def deep_traverse(base_path,prefix):
93
94
深度递归遍历
94
95
'''
95
96
if os .path .isfile (base_path ):
97
+ if not good_file (base_path ):
98
+ return ''
96
99
return build_md_item (prefix ,base_path )
97
100
title = prefix + '- ' + os .path .basename (base_path ) + '\n '
98
101
result = ''
99
- for item in os .listdir (base_path ). sort ( ):
102
+ for item in sorted ( os .listdir (base_path )):
100
103
abspath = os .path .join (base_path ,item )
101
104
result += deep_traverse (abspath ,prefix + ' ' )
102
105
if '' == result :
@@ -126,20 +129,20 @@ def build_md_items(prefix,base_path):
126
129
'''
127
130
todo:排除前缀不符合需求的文件
128
131
'''
129
- items = os .listdir (base_path )
130
132
result = "\n "
131
- for item in items :
133
+ for item in sorted ( os . listdir ( base_path )) :
132
134
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 )
135
137
return result
136
138
137
139
def build_md_item (prefix ,file_path ):
138
- if not good_file (file_path ):
139
- return ''
140
+
140
141
base_name = os .path .basename (file_path )
141
142
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 '
143
146
144
147
145
148
def layer_traverse (base_path ,now ,depth ):
0 commit comments