#!/usr/bin/env python # -*- coding: utf-8 -*- """ 从原型抽取 SVG 图标,生成前端图标模块。 为什么要有这一步 ---------------- 原型里的图标是内联 SVG sprite(统一 viewBox、1.7px 描边、currentColor)。 如果前端换成别的图标库(Element Plus Icons / FontAwesome), 视觉上立刻就能看出"原型一个样、实现另一个样"。 所以:**图标以原型为准,用脚本抽出来生成代码**,而不是找一套"差不多的"。 用法 ---- python scripts/extract-icons.py """ from __future__ import annotations import io import os import re import sys if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8") ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SOURCES = [ os.path.join(ROOT, "docs", "prototype", "admin-web.html"), os.path.join(ROOT, "docs", "prototype", "student-app.html"), os.path.join(ROOT, "docs", "prototype", "teacher-app.html"), ] OUT = os.path.join(ROOT, "frontend-pc", "src", "components", "icon-defs.ts") def main() -> int: symbols: dict[str, str] = {} for p in SOURCES: if not os.path.exists(p): print(f" 跳过(不存在):{p}") continue s = io.open(p, encoding="utf-8").read() for m in re.finditer(r']*\bid="([^"]+)"[^>]*>(.*?)', s, re.S): name, body = m.group(1), m.group(2) # 先抽到的为准(admin-web 的图标集最全) if name not in symbols: symbols[name] = re.sub(r"\s+", " ", body).strip() print(f"[OK] 抽到 {len(symbols)} 个图标") if not symbols: print("[X] 没抽到图标,检查原型里 的写法") return 1 os.makedirs(os.path.dirname(OUT), exist_ok=True) lines = [ "// ⚠️ 本文件由脚本生成,请勿手改!", "// 来源 :docs/prototype/*.html 里的 ", "// 重新生成:python scripts/extract-icons.py", "//", "// 图标取自原型的内联 SVG sprite(统一 24×24、1.7px 描边、currentColor),", "// 保证前端与原型用的是同一套图形,不会出现「原型一个样、实现另一个样」。", "", "/** 图标名 → 内部 SVG 标记(不含 外壳) */", "export const ICON_DEFS: Record = {", ] for name in sorted(symbols): body = symbols[name].replace("\\", "\\\\").replace("'", "\\'") lines.append(f" '{name}': '{body}',") lines += [ "}", "", "/** 所有可用图标名 */", "export const ICON_NAMES = Object.keys(ICON_DEFS)", "", ] io.open(OUT, "w", encoding="utf-8", newline="\n").write("\n".join(lines)) rel = os.path.relpath(OUT, ROOT) print(f" 已生成 {rel}({os.path.getsize(OUT)} 字节)") print(f" 图标:{', '.join(sorted(symbols))}") return 0 if __name__ == "__main__": raise SystemExit(main())