-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateMenu.py
More file actions
84 lines (69 loc) · 2.47 KB
/
generateMenu.py
File metadata and controls
84 lines (69 loc) · 2.47 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
from natsort import natsorted
from natsort import ns
MD_EXTENSIONS = ['.md', '.markdown']
BASE_DIR = '.'
IGNORED_FOLDERS = ['.git', '.vscode', '.idea', 'docs', '_image', '_attachment']
IGNORED_MD_FILES = ['README', '_sidebar'] # do NOT include file extensions
def removeSpaces(filepath):
if ' ' in filepath:
new_filepath = filepath.replace(' ', '')
print("[removeSpaces] rename", filepath, "to", new_filepath)
os.rename(filepath, new_filepath)
return new_filepath
else:
return filepath
menu = "* [**Home**](/)\n"
def generateMenuItem(filepath, level):
global menu
basename = os.path.basename(filepath)
spaces = ' '*(level-1)*4
# isdir
if os.path.isdir(filepath):
menu += '\n' + spaces + '* ' + basename + '\n'
# isfile
else:
fileNameWithoutExt, _ = os.path.splitext(basename)
menu += spaces + "- [%s](%s)" % (fileNameWithoutExt, filepath) + '\n'
def walk(root, level):
root = removeSpaces(root)
basename = os.path.basename(root)
# isdir
if os.path.isdir(root):
if basename in IGNORED_FOLDERS: return
# TODO: do something with the folder
print("[level=%d] Dir: %s" % (level, root))
generateMenuItem(root, level)
# sort and walk every file in this folder
fileList = os.listdir(root)
# fileList.sort()
fileList = natsorted(fileList, alg=ns.PATH)
for file in fileList:
filepath = os.path.join(root, file)
walk(filepath, level+1)
# isfile
else:
fileNameWithoutExt, ext = os.path.splitext(basename)
if (ext not in MD_EXTENSIONS) or (fileNameWithoutExt in IGNORED_MD_FILES): return
# TODO: do something with the file
print("[level=%d] File: %s" % (level, root))
generateMenuItem(root, level)
def walkSubdirs(root, level):
fileList = os.listdir(root)
# fileList.sort()
fileList = natsorted(fileList, alg=ns.PATH)
for file in fileList:
filepath = os.path.join(root, file)
if os.path.isdir(filepath):
walk(filepath, level+1)
def generateMenu():
"""Generate menu and write to '_sidebar.md'
"""
# walk(BASE_DIR, 0) # walk including BASE_DIR
walkSubdirs(BASE_DIR, 0) # walk without BASE_DIR itself
global menu
# print("---------- Menu --------------")
# print(menu)
with open(os.path.join(BASE_DIR, '_sidebar.md'), 'w') as f:
f.write(menu)
generateMenu()