Files
2nd/scratch/generate_indexes.py

49 lines
1.3 KiB
Python

import os
wiki_root = r"E:\Wiki\2nd\10_Wiki"
def generate_folder_index(dir_path):
rel_dir = os.path.relpath(dir_path, wiki_root)
index_name = "Index.md"
index_path = os.path.join(dir_path, index_name)
files = []
subdirs = []
for item in os.listdir(dir_path):
if item == index_name: continue
full_path = os.path.join(dir_path, item)
if os.path.isdir(full_path):
subdirs.append(item)
elif item.endswith(".md"):
files.append(item)
if not files and not subdirs:
return
content = f"# Index: {rel_dir.replace(os.sep, ' > ')}\n\n"
if subdirs:
content += "## 📁 Subcategories\n"
for sd in sorted(subdirs):
# Check if subdir has an Index.md
content += f"- [[{sd}/Index|{sd}]]\n"
content += "\n"
if files:
content += "## 📝 Documents\n"
for f in sorted(files):
name = os.path.splitext(f)[0]
content += f"- [[{name}]]\n"
with open(index_path, "w", encoding="utf-8") as f:
f.write(content)
print(f"Generated {index_path}")
# Traverse and generate for each folder
for root, dirs, files in os.walk(wiki_root):
# Exclude .git
if ".git" in dirs:
dirs.remove(".git")
generate_folder_index(root)