build.py
7.91 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Magic-Vision HTML 模板渲染引擎
用法: python build.py <skill-name> --data <data-json-file> [--output <output-dir>]
零依赖,纯 Python 标准库。读取 templates/ 下的模板文件 + data.json,渲染完整 HTML。
"""
import json
import os
import sys
import argparse
from string import Template
ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATES = os.path.join(ROOT, "templates")
SHARED = os.path.join(TEMPLATES, "shared")
sys.path.insert(0, ROOT)
sys.path.insert(0, os.path.dirname(ROOT))
from _resolve import resolve_refs
from _build import build_changelogs_html
# Skill → 输出目录映射
SKILL_OUTPUT = {
"1-tech-design": "1-tech-implementation-plan",
"2-detailed-design": "2-detailed-design",
"3-operation-flow": "3-operation-flow",
"4-database-schema": "4-database-schema",
"5-api-spec": "5-api-spec",
}
def load_css():
"""加载共享 CSS"""
css_path = os.path.join(SHARED, "design-system.css")
with open(css_path, "r", encoding="utf-8") as f:
return f.read()
def load_template(skill, name):
"""加载指定 skill 的模板文件(返回原始字符串)"""
path = os.path.join(TEMPLATES, skill, name)
with open(path, "r", encoding="utf-8") as f:
return f.read()
def load_index_template():
"""加载导航中心模板(返回原始字符串)"""
path = os.path.join(SHARED, "index.html")
with open(path, "r", encoding="utf-8") as f:
return f.read()
def load_data(json_path):
"""加载数据 JSON"""
with open(json_path, "r", encoding="utf-8") as f:
return json.load(f)
def render_html(template_str, data, extra_css=""):
"""渲染 HTML:预处理点号引用 → 替换模板变量 + 内联 CSS"""
template_str = resolve_refs(template_str, data)
template = Template(template_str)
css = load_css()
if extra_css:
css += "\n" + extra_css
# 准备模板变量
vars_dict = dict(data)
vars_dict["CSS"] = css
vars_dict["ROOT"] = ROOT
return template.safe_substitute(vars_dict)
def build_tech_design(data, output_dir):
"""渲染 1-vision:技术方案设计"""
os.makedirs(output_dir, exist_ok=True)
skill = "1-tech-design"
# overview.html
tmpl = load_template(skill, "overview.html")
html = render_html(tmpl, data)
with open(os.path.join(output_dir, "overview.html"), "w", encoding="utf-8") as f:
f.write(html)
# T-XX-*.html
detail_tmpl = load_template(skill, "T-XX-detail.html")
for scheme in data.get("schemes", []):
# 预处理变更记录
scheme = {**scheme, "changelog_html": build_changelogs_html(scheme.get("changelogs", []))}
html = render_html(detail_tmpl, {**data, "scheme": scheme})
filename = f"{scheme['id']}-{scheme.get('fileKey', scheme['id'])}.html"
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f:
f.write(html)
print(f"1-vision: 已生成 overview.html + {len(data.get('schemes', []))} 个方案详情页")
def build_detailed_design(data, output_dir):
"""渲染 2-vision:功能详细设计"""
os.makedirs(output_dir, exist_ok=True)
skill = "2-detailed-design"
# planning-canvas.html
tmpl = load_template(skill, "planning-canvas.html")
html = render_html(tmpl, data)
with open(os.path.join(output_dir, "planning-canvas.html"), "w", encoding="utf-8") as f:
f.write(html)
# F-XX-*.html
detail_tmpl = load_template(skill, "F-XX-detail.html")
for feature in data.get("features", []):
html = render_html(detail_tmpl, {**data, "feature": feature})
filename = feature.get("detailPage", f"{feature['id']}-detail.html")
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f:
f.write(html)
print(f"2-vision: 已生成 planning-canvas.html + {len(data.get('features', []))} 个功能详情页")
def build_operation_flow(data, output_dir):
"""渲染 3-vision:测试操作文档"""
os.makedirs(output_dir, exist_ok=True)
skill = "3-operation-flow"
# overview.html
tmpl = load_template(skill, "overview.html")
html = render_html(tmpl, data)
with open(os.path.join(output_dir, "overview.html"), "w", encoding="utf-8") as f:
f.write(html)
# F-XX-*-operation.html
detail_tmpl = load_template(skill, "F-XX-operation.html")
for feature in data.get("features", []):
html = render_html(detail_tmpl, {**data, "feature": feature})
filename = f"{feature['id']}-{feature.get('fileKey', feature['id'])}-operation.html"
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f:
f.write(html)
print(f"3-vision: 已生成 overview.html + {len(data.get('features', []))} 个测试操作页")
def build_database_schema(data, output_dir):
"""渲染 4-vision:数据库表结构"""
os.makedirs(output_dir, exist_ok=True)
skill = "4-database-schema"
# overview.html
tmpl = load_template(skill, "overview.html")
html = render_html(tmpl, data)
with open(os.path.join(output_dir, "overview.html"), "w", encoding="utf-8") as f:
f.write(html)
# M-XX-*.html
detail_tmpl = load_template(skill, "M-XX-table.html")
for table in data.get("tables", []):
html = render_html(detail_tmpl, {**data, "table": table})
filename = f"{table['id']}-{table.get('fileKey', table['name'])}.html"
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f:
f.write(html)
print(f"4-vision: 已生成 overview.html + {len(data.get('tables', []))} 个表结构页")
def build_api_spec(data, output_dir):
"""渲染 5-vision:API 接口文档"""
os.makedirs(output_dir, exist_ok=True)
skill = "5-api-spec"
# overview.html
tmpl = load_template(skill, "overview.html")
html = render_html(tmpl, data)
with open(os.path.join(output_dir, "overview.html"), "w", encoding="utf-8") as f:
f.write(html)
# M-XX-*-api.html
detail_tmpl = load_template(skill, "M-XX-api.html")
for module in data.get("modules", []):
html = render_html(detail_tmpl, {**data, "module": module})
filename = f"{module['id']}-{module.get('fileKey', module['id'])}-api.html"
with open(os.path.join(output_dir, filename), "w", encoding="utf-8") as f:
f.write(html)
print(f"5-vision: 已生成 overview.html + {len(data.get('modules', []))} 个 API 详情页")
BUILDERS = {
"1-tech-design": build_tech_design,
"2-detailed-design": build_detailed_design,
"3-operation-flow": build_operation_flow,
"4-database-schema": build_database_schema,
"5-api-spec": build_api_spec,
}
def build_index(all_categories):
"""渲染导航中心 index.html"""
css = load_css()
raw = load_index_template()
tmpl = Template(raw)
html = tmpl.safe_substitute(CSS=css, categories_json=json.dumps(all_categories, ensure_ascii=False))
with open(os.path.join(ROOT, "index.html"), "w", encoding="utf-8") as f:
f.write(html)
print(f"index.html: 导航中心已更新 ({len(all_categories)} 个分类)")
def main():
parser = argparse.ArgumentParser(description="Magic-Vision HTML 渲染引擎")
parser.add_argument("skill", nargs="?", choices=list(BUILDERS.keys()),
help="要渲染的 skill 名称")
parser.add_argument("--data", required=True, help="数据 JSON 文件路径")
parser.add_argument("--output", help="输出目录(可选,默认根据 skill 自动确定)")
args = parser.parse_args()
data = load_data(args.data)
if args.skill:
builder = BUILDERS[args.skill]
output_dir = args.output or os.path.join(ROOT, SKILL_OUTPUT[args.skill])
builder(data, output_dir)
# 渲染 index.html
if "index" in data:
all_categories = data.get("allCategories", [data["index"]])
build_index(all_categories)
if __name__ == "__main__":
main()