-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgen-catalog-json.py
executable file
·60 lines (49 loc) · 1.38 KB
/
gen-catalog-json.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
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import yaml
import json
import os
from markdown import Markdown
from io import StringIO
def unmark_element(element, stream=None):
if stream is None:
stream = StringIO()
if element.text:
stream.write(element.text)
for sub in element:
unmark_element(sub, stream)
if element.tail:
stream.write(element.tail)
return stream.getvalue()
Markdown.output_formats["plain"] = unmark_element
plain_md = Markdown(output_format="plain")
plain_md.stripTopLevelTags = False
def walkNavs(navs, parent):
for item in navs:
for k, v in item.items():
menu = {
"title": k
}
if isinstance(v, (list, tuple)):
menu["type"] = "catalog"
menu["path"] = os.path.join(rootDir, parent.get("title"), k)
menu["children"] = []
walkNavs(v, menu)
elif v.endswith(".md"):
menu["type"] = "file"
menu["path"] = os.path.join(rootDir, v)
else:
menu["type"] = "link"
menu["path"] = v
parent["children"].append(menu)
mkdocsData = open("mkdocs.yml").read()
mkdocs = yaml.load(mkdocsData, Loader=yaml.SafeLoader)
currDir = os.path.basename(os.getcwd())
rootDir = os.path.join(currDir, mkdocs["docs_dir"])
rootTitle = mkdocs["site_name"]
root = {
"title": rootTitle,
"path": rootDir,
"children": []
}
walkNavs(mkdocs["nav"], root)
print(json.dumps(root))