-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify.py
84 lines (76 loc) · 3.56 KB
/
simplify.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
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 json
from pathlib import Path
from typing import List, Dict, Any
import mistletoe
from mistletoe.ast_renderer import ASTRenderer
from util import traverse, bcolors
def flatten_content(node: Dict[str, Any], new_line: str = '', root: bool = True) -> str:
out = new_line.removeprefix('\n') if root else ''
children: List[Dict] = node["children"]
for child in children:
match child['type']:
case 'LineBreak':
out += new_line
case _:
out += child["content"] if "content" in child else flatten_content(child, new_line=new_line, root=False)
return out
def simplify(filepath) -> dict:
md = json.loads(mistletoe.markdown(filepath.read_text(encoding='UTF-8'), ASTRenderer))
out: Dict[str, Any] = {"content": []}
headers: list = ['' for _ in range(6)]
for node in md['children']:
match node['type']:
case 'Heading':
level: int = node['level']
content: str = node['children'][0]['content']
endpoint: str = ''
if " % " in content:
content, endpoint = content.split(" % ", 1)
headers = [*headers[:level], content, *['' for _ in range(6 - level)]]
parent: Dict[str, Any] = traverse(out, headers[:level])
parent["content"].append('|' + content)
url: str = f'https://discord.com/developers/docs/{filepath.parts[-2].replace("_", "-")}/' \
f'{filepath.stem.lower().replace("_", "-")}#'
if level <= 3:
url += headers[level].lower().replace(" ", "-")
else:
url += "-".join([h.lower().replace(" ", "-") for h in [headers[parent["level"]], headers[level]]])
parent[content] = {
"level": level,
"url": url
} | ({
"endpoint": endpoint,
} if endpoint else {}) | {
"content": []
}
case 'Table':
traverse(out, headers)["content"].append([
[field['children'][0]['content'] for field in node['header']['children']],
*[[flatten_content(cell) for cell in row['children']] for row in node['children']]
])
pass
case 'Paragraph':
traverse(out, headers)["content"].append(flatten_content(node, '\n'))
case 'Quote':
traverse(out, headers)["content"].append(flatten_content(node, '\n> '))
case 'List':
traverse(out, headers)["content"].append([flatten_content(item) for item in node['children']])
case 'CodeFence':
traverse(out, headers)["content"].append({
'language': node['language'],
'code': flatten_content(node, '\n')
})
case 'ThematicBreak':
traverse(out, headers)["content"].append('―')
case _:
print(f'{bcolors.WARNING}Skipped a {node["type"]} at {headers}!{bcolors.ENDC}')
print(node)
return out
if __name__ == '__main__':
for filepath in Path('./discord-api-docs/docs').rglob("*.md"):
print(filepath)
simplified = simplify(filepath)
# print(simplified)
target_dir = Path('./discord-api-json/', *filepath.parts[2:-1])
target_dir.mkdir(parents=True, exist_ok=True)
target_dir.joinpath(f'{filepath.stem}.json').write_text(json.dumps(simplified, indent=2))