render.py
3.03 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
#!/usr/bin/env python3
"""1-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", "1-tech-design")
DATA_DIR = os.path.join(HERE, "data")
sys.path.insert(0, PARENT)
from _resolve import resolve_refs
from _build import build_changelogs_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/ 目录下的拆分数据"""
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))
# 扫描 T-*.json 作为 scheme 列表
schemes = []
for fp in sorted(glob.glob(os.path.join(DATA_DIR, "T-*.json"))):
with open(fp, encoding="utf-8") as f:
schemes.append(json.load(f))
data["schemes"] = schemes
return data
def main():
data = load_data()
schemes = data.get("schemes", [])
# 自动生成导航链接(AI 不用手写 href)
data["scheme_nav_links"] = "".join(
f'<a href="{s["id"]}-{s.get("fileKey",s["id"])}.html" class="nav-link">{s["id"]}</a>'
for s in schemes
)
# 自动生成详情页跳转链接,并从 changelogs[] 生成 changelog_html
for s in schemes:
s["detail_href"] = f'{s["id"]}-{s.get("fileKey",s["id"])}.html'
s["changelog_html"] = build_changelogs_html(s.get("changelogs", []))
# overview.html
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()))
# T-XX-*.html
det_raw = resolve_refs(open(os.path.join(SKILL_TPL, "T-XX-detail.html"), encoding="utf-8").read(), data)
for s in schemes:
s_data = {**data, "scheme": s}
s_raw = resolve_refs(det_raw, s_data)
html = Template(s_raw).safe_substitute(**s_data, CSS=css())
name = f'{s["id"]}-{s.get("fileKey",s["id"])}.html'
with open(os.path.join(HERE, name), "w", encoding="utf-8") as f:
f.write(html)
# server.py(首建)
sp = os.path.join(PARENT, "server.py")
if not os.path.exists(sp):
with open(sp, "w", encoding="utf-8") as f:
f.write("""import http.server, os
PORT=9999
ROOT=os.path.dirname(os.path.abspath(__file__))
class H(http.server.SimpleHTTPRequestHandler):
def __init__(self,*a,**k):super().__init__(*a,directory=ROOT,**k)
http.server.HTTPServer(("127.0.0.1",PORT),H).serve_forever()
""")
print(f"1-vision: overview + {len(schemes)} 方案 → {HERE}")
if __name__ == "__main__":
main()