-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_generate_sidebar.py
52 lines (42 loc) · 1.68 KB
/
_generate_sidebar.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
47
48
49
50
51
import os
import re
rootdir = './documentation/'
dirs_dict = dict()
def same_name(file, root):
filename = re.sub("^[0-9]+_", "", file.replace(".md", ""))
rootname = re.sub("^[0-9]+_", "", os.path.basename(root))
print(filename, rootname)
return filename == rootname
def do_display(files, root):
for file in files:
if same_name(file, root):
return False
return True
def list_files(startpath):
output = ""
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
if root not in [rootdir]:
if do_display(files, root):
title = re.sub("^[0-9]+_", "", os.path.basename(root)) \
.capitalize() \
.replace("-", " ") \
.replace("_", " ")
output += '{}* {}\n'.format(indent, title)
for f in files:
if f.endswith(".md") and f not in ["_sidebar.md"]:
file_level = os.path.join(root,f).replace(startpath, '').count(os.sep)
if same_name(f, root):
file_level -= 1
subindent = ' ' * 4 * (file_level)
title = re.sub("^[0-9]+_", "", f)\
.capitalize()\
.replace(".md", "") \
.replace("-", " ") \
.replace("_", " ")
filepath = os.path.join(root, f).replace("\\", "/").replace("./", "")
output += '{}* [{}]({})\n'.format(subindent, title, filepath)
return output
with open("sidebar.md", "w") as _sidebar_md:
_sidebar_md.write(list_files(rootdir))