render.py
4.07 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
102
103
104
105
#!/usr/bin/env python3
"""5-vision — python render.py"""
import json, os, sys, glob
from string import Template
HERE = os.path.dirname(os.path.abspath(__file__))
PARENT = os.path.dirname(HERE)
SHARED = os.path.join(HERE, "templates", "shared")
SKILL_TPL = os.path.join(HERE, "templates", "5-api-spec")
DATA_DIR = os.path.join(HERE, "data")
sys.path.insert(0, PARENT)
from _resolve import resolve_refs
from _build import build_apis_html, build_changelog_bar_html
def css():
with open(os.path.join(SHARED, "design-system.css"), encoding="utf-8") as f:
return f.read()
def tpl(name, data):
with open(os.path.join(SKILL_TPL, name), encoding="utf-8") as f:
raw = f.read()
raw = resolve_refs(raw, data)
return Template(raw)
def load_data():
data = {}
with open(os.path.join(DATA_DIR, "common.json"), encoding="utf-8") as f:
data.update(json.load(f))
ov = os.path.join(DATA_DIR, "overview.json")
if os.path.exists(ov):
with open(ov, encoding="utf-8") as f:
data.update(json.load(f))
# 扫描 M-*.json 作为 module 列表
modules = []
for fp in sorted(glob.glob(os.path.join(DATA_DIR, "M-*.json"))):
with open(fp, encoding="utf-8") as f:
modules.append(json.load(f))
data["modules"] = modules
return data
def _build_module_accordion(mod):
"""为每个模块生成带 API 明细的 accordion"""
apis = mod.get("apis", [])
rows = ""
for api in apis:
m = api.get("method", "GET")
mb = f'<span class="method-badge method-{m.lower()}">{m}</span>'
rows += (
f'<tr>'
f'<td>{mb}</td>'
f'<td><code class="api-path">{api.get("path","")}</code></td>'
f'<td>{api.get("name","")}</td>'
f'<td style="color:var(--color-text-secondary);font-size:0.82rem">{api.get("description","")}</td>'
f'</tr>'
)
table = (
f'<table class="data-table">'
f'<thead><tr><th>方法</th><th>路径</th><th>名称</th><th>描述</th></tr></thead>'
f'<tbody>{rows}</tbody></table>'
) if rows else ""
return (
f'<details class="accordion" open>'
f'<summary class="accordion-summary">{mod["id"]} {mod.get("name","")} '
f'<span class="accordion-meta">{mod.get("desc","")}</span>'
f'<a href="{mod["id"]}-{mod.get("fileKey",mod["id"])}-api.html" class="accordion-detail-link">查看详情 →</a>'
f'</summary>'
f'<div class="accordion-body"><div class="accordion-section"><h4>接口列表 <span style="font-weight:400;color:var(--color-text-muted);font-size:0.8rem">(共 {len(apis)} 个)</span></h4>{table}</div></div>'
f'</details>'
)
def main():
data = load_data()
modules = data.get("modules", [])
# 为每个模块构建 api_cards / method_badges / changelog_html
for mod in modules:
method_badges, api_cards = build_apis_html(mod.get("apis", []))
mod["api_cards"] = api_cards
mod["method_badges"] = method_badges
mod["changelog_html"] = build_changelog_bar_html(mod.get("changelogs", []))
# 自动生成 module_accordions(带 API 明细表 + 详情链接)
data["module_accordions"] = "".join(
_build_module_accordion(mod)
for mod in modules
)
data["api_count"] = sum(len(mod.get("apis", [])) for mod in modules)
ov = tpl("overview.html", data)
with open(os.path.join(HERE, "overview.html"), "w", encoding="utf-8") as f:
f.write(ov.safe_substitute(**data, CSS=css()))
det_raw = resolve_refs(open(os.path.join(SKILL_TPL, "M-XX-api.html"), encoding="utf-8").read(), data)
for mod in data.get("modules", []):
mod_data = {**data, "module": mod}
mod_raw = resolve_refs(det_raw, mod_data)
html = Template(mod_raw).safe_substitute(**mod_data, CSS=css())
name = f'{mod["id"]}-{mod.get("fileKey",mod["id"])}-api.html'
with open(os.path.join(HERE, name), "w", encoding="utf-8") as f:
f.write(html)
print(f"5-vision: overview + {len(data.get('modules',[]))} API → {HERE}")
if __name__ == "__main__":
main()