-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeJSON.py
More file actions
101 lines (87 loc) · 3.05 KB
/
makeJSON.py
File metadata and controls
101 lines (87 loc) · 3.05 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import json
REPLACEMENTS = [
("_", " "),
("(star)", "*"),
("(slash)", "/"),
("(colon)", ":"),
("(question)", "?"),
]
def clean_name(name):
name = name[name.find("~") + 1 :]
for args in REPLACEMENTS:
name = name.replace(*args)
return name[:-3] if name.endswith(".md") else name
def generate_json(folder_path):
json_data = []
for item in sorted(os.listdir(folder_path)):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
json_data.append(generate_category_json(item, item_path))
else:
raise Exception(f"Extraneous files in category directory {item}")
return json_data
def generate_category_json(category_name, category_path):
return {
"type": "category",
"name": clean_name(category_name),
"children": generate_section_json(category_path),
}
def generate_section_json(category_path):
json_data = []
for item in sorted(os.listdir(category_path)):
item_path = os.path.join(category_path, item)
if os.path.isdir(item_path):
potential_srcs = list(
filter(
lambda file: clean_name(file) == clean_name(item),
os.listdir(item_path),
)
)
if len(potential_srcs) != 1:
raise Exception(
f"Incorrect number of .md files for section {item}, {len(potential_srcs)} candidates, must be 1"
)
path = f"{os.path.join(item_path, potential_srcs[0])}"
json_data.append(
{
"type": "section",
"name": clean_name(item),
"path": path,
"children": generate_subsection_json(item, item_path),
}
)
elif item.endswith(".md"):
json_data.append(
{
"type": "section",
"name": clean_name(item),
"path": item_path,
"children": [],
}
)
return json_data
def generate_subsection_json(section, subsection_path):
json_data = []
for item in sorted(os.listdir(subsection_path)):
if clean_name(item) == clean_name(section):
continue
if item.endswith(".md"):
json_data.append(
{
"type": "subsection",
"name": clean_name(os.path.basename(item)),
"path": os.path.join(subsection_path, item),
}
)
else:
raise Exception(
f"Extraneous non .md files in subsection directory {subsection_path}"
)
return json_data
root_folder = "src"
json_data = generate_json(root_folder)
json_file_path = "navbar.json"
with open(json_file_path, "w") as json_file:
json.dump(json_data, json_file, indent=4)
print("JSON Completed")