From 468bbc3c3eed192ea149b945b98f8bc5d96d1792 Mon Sep 17 00:00:00 2001 From: estom Date: Thu, 18 Jan 2024 22:48:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=88=A0=E9=99=A4=E5=92=8C=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog/generate/buildSidebar.py | 219 +++++++++++----------------------- blog/generate/config.ini | 17 --- 2 files changed, 68 insertions(+), 168 deletions(-) delete mode 100644 blog/generate/config.ini diff --git a/blog/generate/buildSidebar.py b/blog/generate/buildSidebar.py index f28d6188..2c951c7d 100644 --- a/blog/generate/buildSidebar.py +++ b/blog/generate/buildSidebar.py @@ -2,73 +2,68 @@ from configparser import ConfigParser from os.path import splitext, basename, join, isdir, relpath, abspath from os import listdir - -base_dir = None -start_with = None -show_file = None -ignore_file_name = None -include_start_with = None - -out_file_list = [] -create_depth = -1 +# docsify根目录 +root_dir=/root/gitee/notes/blog +# 要处理的文件或文件夹 +exclude_start_with=['_','*','.'] +exclude_file = ['readme.md'] +exclude_dir = ['.vscode','.git'] +# 想要在几级目录生成文件,默认"0"表示在根目录生成,可以配合侧边栏折叠插件使用 +create_depth=1 -def read_config(): - global base_dir, show_file, start_with, ignore_file_name, ReadmeFile, _sidebarFile, out_file_list, create_depth,include_start_with - - cf = ConfigParser() - cf.read("config.ini", encoding='utf-8') - base_dir = cf.get("config", "base_dir") - start_with = cf.get("config", "ignore_start_with").split("|") - show_file = cf.get("config", "show_file").split('|') - ignore_file_name = cf.get("config", "ignore_file_name").split("|") - include_start_with = cf.get("config", "include_start_with").split("|") - - out_file_list = cf.get("outFile", "eachFile").split("|") - create_depth = int(cf.get("outFile", "create_depth")) - - -def check_file_extension(file_path): +def good_file(base_path): """ - 检查文件后缀是否为指定的后缀 - :param file_path: 文件路径 - :return: 如果文件后缀为指定的后缀,返回True;否则返回False - """ - file_extension = splitext(file_path)[1] - if file_extension in show_file: - return True - else: - return False - - -def check_file_name_satified(file_path): - """ - 获取文件名(不包括扩展名) + 是否需要生成文件 + 1. 扩展名不是md的不生成 + 2. 不是README.md _sidebar.md。不生成 + 3. 在跳过列表里的不生成 :param file_path: 文件路径 :return: 文件名(不包括扩展名) """ - file_name_with_extension = basename(file_path) - file_name = splitext(file_name_with_extension)[0] - if file_name[0] in start_with or file_name in ignore_file_name: + file_extension = splitext(base_path)[1] + if file_extension != '.md': return False - if file_name[0] not in include_start_with: + + base_name = os.path.basename(base_path) + if base_name.lower() in exclude_file: return False + + for item in exclude_start_with: + if base_name.starts_with(item): + return False + + rel_path = relpath(root_dir, base_path) + for item in exclude_dir: + if rel_path.startswith(item): + return False return True - +def good_dir(base_path): + rel_path = relpath(root_dir, base_path) + for item in exclude_dir: + if rel_path.startswith(item): + return False + for dirpath, dirnames, filenames in os.walk(root_dir): + for filename in filenames: + abspath = os.path.join(dirpath): + if good_file(abspath): + return True + + return False def build_next_level(base_path): ''' 创建下一级节点的目录_sidebar.md todo:排除子目录下没有md文件的子目录 ''' - print("build next level:"+root_path) - items = os.listdir(base_path) + print("build next level:"+base_path) + items = os.listdir(base_path).sort() result = "\n" for item in items: abspath = os.path.join(base_path,item) - if isdir(abspath): - rel_path = relpath(root_dir, base_dir) + if isdir(abspath) and good_dir(abspath): + rel_path = relpath(root_dir, abspath) readme_path = os.path.join(rel_path,"README.md") result += "- [" + item + "](" + readme_path + ')\n' @@ -83,8 +78,8 @@ def build_full_level(base_path): ''' 创建所有子节点的目录_sidebar.md ''' - print("build full path"+root_path) - result = deep_traverse(base_dir,'') + print("build full path:"+base_path) + result = deep_traverse(base_path,'') if '' == result: return basename = os.path.basename(base_path) @@ -93,19 +88,17 @@ def build_full_level(base_path): f.write('## '+ basename + '\n') f.write(result) -def deep_traverse(base_dir,prefix): +def deep_traverse(base_path,prefix): ''' 深度递归遍历 ''' - if os.path.isfile(base_dir) - if base_dir.endwith('.md') - return build_md_item(prefix,base_dir) - else: - return '' - title = prefix + '- ' + os.path.basename(base_dir) + '\n' + if os.path.isfile(base_path): + return build_md_item(prefix,base_path) + title = prefix + '- ' + os.path.basename(base_path) + '\n' result = '' - for items in os.listdir(base_dir): - result += deep_traverse(base_dir,prefix+' ') + for item in os.listdir(base_path).sort(): + abspath = os.path.join(base_path,item) + result += deep_traverse(abspath,prefix+' ') if '' == result: return '' return title + result @@ -115,7 +108,9 @@ def build_readme_now(base_path): 创建当前节点的readme文件,指向当前目录下的md文件。 深度小于depth+1 就要生成readme文件 ''' - print("build readme now:"+base_dir) + if not good_dir(base_path): + return + print("build readme now:"+base_path) readme_path = os.path.join(base_path, 'README.md') basename = os.path.basename(base_path) @@ -126,6 +121,7 @@ def build_readme_now(base_path): f.write(build_md_items('',base_path)) + def build_md_items(prefix,base_path): ''' todo:排除前缀不符合需求的文件 @@ -133,112 +129,38 @@ def build_md_items(prefix,base_path): items = os.listdir(base_path) result = "\n" for item in items: - abspath = join(root, item) - if abspath is not dir: + abspath = join(base_path, item) + if os.path.isfile(abspath): result += build_md_item(prefix,item) return result def build_md_item(prefix,file_path): + if not good_file(file_path): + return '' base_name = os.path.basename(file_path) title = os.path.splitext(base_name)[0] - return prefix + "- [" + title + "](" + relpath(root_dir, base_dir) + ')\n' + return prefix + "- [" + title + "](" + relpath(root_dir, file_path) + ')\n' -def layer_traverse(root,now,depth): - build_readme_now(root) +def layer_traverse(base_path,now,depth): + build_readme_now(base_path) ''' now=depth创建递归目录,不再按层遍历 ''' if now >= depth: - build_full_level(root) + build_full_level(base_path) return ''' now