-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimwiki-indexer.py
46 lines (41 loc) · 1.56 KB
/
vimwiki-indexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os, pprint
from pathlib import Path
def get_directory_input(prompt):
while True:
directory = input(prompt)
if os.path.isdir(directory):
return directory
else:
print("Invalid directory, please try again.")
# Ask user for the VimWiki directory
vimwiki_directory = get_directory_input('Enter the path to your VimWiki directory: ')
os.chdir(vimwiki_directory)
# Function to create an index of wiki pages
def create_index(directory):
index = []
for folder, subfolder, files in os.walk(directory):
for file_name in files:
if Path(file_name).suffix == '.txt':
stem = Path(file_name).stem
if folder == './':
index.append(f'/{stem}')
else:
index.append(f'{folder[1:]}/{stem}')
index.sort()
return index
# Function to append navigation to each page
def append_navigation(directory, index):
navigation_str = '\\n'.join(index)
for folder, subfolder, files in os.walk(directory):
for file_name in files:
file_path = Path(folder, file_name)
try:
with open(file_path, 'r+') as file:
content = file.read()
file.seek(0, 0)
file.write(navigation_str.rstrip('\\r\\n') + '\\n' + content)
except Exception as e:
print(f"Error processing file {file_name}: {e}")
# Generating and appending navigation
index = create_index(vimwiki_directory)
append_navigation(vimwiki_directory, index)