Add automatic index generator
This commit is contained in:
27
scripts/index_generator.py
Normal file
27
scripts/index_generator.py
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
def generate_index():
|
||||
characters = [f.replace('.md', '') for f in os.listdir('characters') if f.endswith('.md')]
|
||||
index = {}
|
||||
|
||||
for chapter_file in sorted(os.listdir('chapters')):
|
||||
if not chapter_file.endswith('.md'): continue
|
||||
with open(os.path.join('chapters', chapter_file), 'r') as f:
|
||||
content = f.read()
|
||||
for char in characters:
|
||||
if re.search(r'\b' + char + r'\b', content, re.IGNORECASE):
|
||||
if char not in index: index[char] = []
|
||||
index[char].append(chapter_file)
|
||||
|
||||
with open('KNOWLEDGE_GRAPH.md', 'w') as f:
|
||||
f.write('# Knowledge Graph\n\n')
|
||||
for char, chapters in index.items():
|
||||
f.write(f'## {char}\n')
|
||||
for chap in chapters:
|
||||
f.write(f'- [{chap}](chapters/{chap})\n')
|
||||
f.write('\n')
|
||||
|
||||
if __name__ == "__main__":
|
||||
generate_index()
|
||||
Reference in New Issue
Block a user