chore: 迁移 mermaid 绘图技能到 meta 层
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
#!/usr/bin/env python3
|
||||
"""新建一个「主题文件夹 + 渲染页骨架 + .mmd 骨架」,并在图形索引页登记主题。
|
||||
|
||||
用法:
|
||||
python3 .agents/skills/mermaid-diagram/scripts/new_figure.py \
|
||||
--topic crm-data-sync --title "CRM 数据同步流程" \
|
||||
--figures "ingest:入库加工,master-read:主表直读"
|
||||
|
||||
约定(见 .agents/skills/mermaid-diagram/SKILL.md):
|
||||
docs/diagrams/<topic>/index.html ← 渲染页(分节 + 图例 + ../viewer.js + ?v=)
|
||||
docs/diagrams/<topic>/<name>.mmd ← 图源(骨架已带统一配色 classDef)
|
||||
docs/diagrams/viewer.js ← 共享渲染器(首次运行时从技能 assets 铺设)
|
||||
docs/diagrams/index.html ← 图形索引(脚本插入主题条目)
|
||||
|
||||
图表根目录 = 向上找到 `.agents/` 的那一层下的 docs/diagrams(本工作区即 meta 仓库根)。
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
SKILL_DIR = Path(__file__).resolve().parent.parent
|
||||
VIEWER_SRC = SKILL_DIR / "assets" / "viewer.js"
|
||||
|
||||
PALETTE = """ classDef src fill:#ffe1e1,stroke:#c0392b,stroke-width:3.5px,color:#7b1414
|
||||
classDef api fill:#e8f0fe,stroke:#1a56b0,stroke-width:1.2px,color:#111
|
||||
classDef take fill:#e7f6ea,stroke:#2e8b57,stroke-width:1.2px,color:#14532d
|
||||
classDef use fill:#f3f0fb,stroke:#6b4fa0,stroke-width:1.2px,color:#3b2a63
|
||||
classDef origin fill:#fff3cd,stroke:#b8860b,stroke-dasharray:4 3,color:#664d03
|
||||
classDef note fill:#f7f7f7,stroke:#999,stroke-dasharray:4 3,color:#333
|
||||
classDef warn fill:#ffe1e1,stroke:#c0392b,stroke-width:2.2px,color:#7b1414
|
||||
"""
|
||||
|
||||
MMD_STUB = """flowchart LR
|
||||
%% {title}
|
||||
%% 依据:<写清类名/方法名/表名从哪来,未经验证的内容标注来源>
|
||||
|
||||
{palette}
|
||||
subgraph T1["{title}"]
|
||||
direction LR
|
||||
A["「入口」<br/>类名#方法"]:::api
|
||||
B["「取」<br/>条件 → 聚合(表.列)"]:::take
|
||||
C["「用」<br/>结果用途"]:::use
|
||||
D["「源」<br/>该值来自哪个字段 / 哪个导入列"]:::origin
|
||||
A --> B --> C
|
||||
B -.-> D
|
||||
end
|
||||
"""
|
||||
|
||||
PAGE = """<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{title}</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"></script>
|
||||
<style>
|
||||
body {{ margin: 0 auto; max-width: 1600px; padding: 24px; background: #fafafa; font-family: "PingFang SC", "Microsoft YaHei", sans-serif; color: #333; }}
|
||||
h1 {{ font-size: 20px; }}
|
||||
h2 {{ font-size: 16px; border-left: 4px solid #1a56b0; padding-left: 10px; margin-top: 40px; }}
|
||||
.tip {{ color: #777; font-size: 13px; line-height: 1.8; }}
|
||||
.legend {{ font-size: 13px; color: #555; margin: 4px 0 0; }}
|
||||
.legend span {{ display: inline-block; padding: 1px 8px; border-radius: 4px; margin-right: 6px; border: 1px solid; }}
|
||||
.diagram {{ background: #fff; border: 1px solid #e0e0e0; border-radius: 8px; padding: 16px; margin: 10px 0; }}
|
||||
code {{ font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }}
|
||||
details {{ background: #fff; border: 1px solid #e0e0e0; border-radius: 6px; padding: 8px 12px; margin: 10px 0; }}
|
||||
summary {{ cursor: pointer; font-size: 13.5px; color: #1a56b0; }}
|
||||
table {{ border-collapse: collapse; width: 100%; font-size: 13px; margin-top: 6px; }}
|
||||
th, td {{ border: 1px solid #e0e0e0; padding: 5px 8px; text-align: left; vertical-align: top; }}
|
||||
th {{ background: #f4f6f8; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{title}</h1>
|
||||
<p class="tip">图源为同目录 <code>.mmd</code> 纯文本(可直接编辑);改动后需递增下方 <code>?v=</code> 版本号以避开浏览器缓存。<br/>
|
||||
节点首行的「」为角色(入口 / 取 / 用 / 源 …);列名写 <code>表名.列名</code>,简写对照见各图内的「表名简写」框。<br/>
|
||||
本地预览:在 <code>docs/diagrams</code> 目录执行 <code>python3 -m http.server 8000</code>,再访问
|
||||
<code>http://localhost:8000/{topic}/index.html</code>。<br/>
|
||||
每张图默认<b>只读视图</b>、一眼看全;点「查看」进入交互后 <b>Ctrl+滚轮</b>缩放、<b>右键拖拽</b>平移,<kbd>Esc</kbd> 退出。</p>
|
||||
|
||||
{sections}
|
||||
<script src="../viewer.js?v=2"></script>
|
||||
<script>MermaidViewer.init();</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
INDEX = """<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LPT 图表索引</title>
|
||||
<style>
|
||||
body {{ margin: 0 auto; max-width: 1600px; padding: 24px; background: #fafafa; font-family: "PingFang SC", "Microsoft YaHei", sans-serif; color: #333; }}
|
||||
h1 {{ font-size: 20px; }}
|
||||
.tip {{ color: #777; font-size: 13px; line-height: 1.8; }}
|
||||
ul {{ list-style: none; padding: 0; }}
|
||||
li {{ background: #fff; border: 1px solid #e0e0e0; border-radius: 8px; padding: 12px 16px; margin: 12px 0; }}
|
||||
li a {{ font-size: 15px; color: #1a56b0; text-decoration: none; }}
|
||||
li a:hover {{ text-decoration: underline; }}
|
||||
li p {{ margin: 6px 0 0; color: #777; font-size: 13px; }}
|
||||
code {{ font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>LPT 图表索引</h1>
|
||||
<p class="tip">每个业务主题一个文件夹,图源为同目录 <code>.mmd</code> 纯文本,渲染页靠 <code>../viewer.js</code> 共享渲染器。<br/>
|
||||
本地预览:在 <code>docs/diagrams</code> 目录执行 <code>python3 -m http.server 8000</code>,再访问
|
||||
<code>http://localhost:8000/<主题>/index.html</code>。</p>
|
||||
<ul>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
SECTION = """ <h2>{heading}</h2>
|
||||
<div class="diagram" data-src="{name}.mmd?v=1" data-height="72vh">加载中…</div>
|
||||
<p class="legend">
|
||||
<span style="background:#e8f0fe;border-color:#1a56b0">蓝 入口</span>
|
||||
<span style="background:#e7f6ea;border-color:#2e8b57">绿 取值</span>
|
||||
<span style="background:#f3f0fb;border-color:#6b4fa0">紫 用途 / 处理</span>
|
||||
<span style="background:#fff3cd;border-color:#b8860b">橙(虚线)值来源</span>
|
||||
<span style="background:#f7f7f7;border-color:#999">灰(虚线)说明</span>
|
||||
</p>
|
||||
"""
|
||||
|
||||
|
||||
def parse_figures(raw: str):
|
||||
out = []
|
||||
for item in raw.split(","):
|
||||
item = item.strip()
|
||||
if not item:
|
||||
continue
|
||||
name, _, heading = item.partition(":")
|
||||
name = name.strip()
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]+", name):
|
||||
sys.exit(f"图名只能用字母/数字/._- :{name}")
|
||||
out.append((name, heading.strip() or name))
|
||||
if not out:
|
||||
sys.exit("--figures 至少要给一个图")
|
||||
return out
|
||||
|
||||
|
||||
def find_diagrams_root(start: Path) -> Path:
|
||||
for p in [start, *start.parents]:
|
||||
if (p / ".agents").is_dir():
|
||||
return p / "docs" / "diagrams"
|
||||
sys.exit("未找到工作区根目录(向上找不到 .agents/),请在 meta 仓库内执行或用 --root 指定")
|
||||
|
||||
|
||||
def ensure_viewer(root: Path, refresh: bool):
|
||||
target = root / "viewer.js"
|
||||
if not VIEWER_SRC.exists():
|
||||
sys.exit(f"技能缺少共享渲染器:{VIEWER_SRC}")
|
||||
if target.exists() and not refresh:
|
||||
print(f"已存在共享渲染器,未覆盖:{target}(如需同步请加 --refresh-viewer)")
|
||||
return
|
||||
shutil.copyfile(VIEWER_SRC, target)
|
||||
print(f"铺设共享渲染器:{target}")
|
||||
|
||||
|
||||
def ensure_index(root: Path):
|
||||
index = root / "index.html"
|
||||
if index.exists():
|
||||
return index
|
||||
index.write_text(INDEX, encoding="utf-8")
|
||||
print(f"新建图形索引:{index}")
|
||||
return index
|
||||
|
||||
|
||||
def register_in_index(root: Path, topic: str, title: str, page: str):
|
||||
index = ensure_index(root)
|
||||
html = index.read_text(encoding="utf-8")
|
||||
if f'href="{topic}/' in html:
|
||||
print(f"提示:索引页已登记 {topic},跳过")
|
||||
return
|
||||
entry = (
|
||||
" <li>\n"
|
||||
f' <a href="{topic}/{page}">{title}</a>\n'
|
||||
f' <p>目录:<code>{topic}/</code></p>\n'
|
||||
" </li>\n"
|
||||
)
|
||||
if " </ul>" not in html:
|
||||
print(f"提示:索引页缺少插入锚点,请手动登记 {topic}")
|
||||
return
|
||||
html = html.replace(" </ul>", entry + " </ul>", 1)
|
||||
index.write_text(html, encoding="utf-8")
|
||||
print(f"已在索引页登记主题:{index}")
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--topic", required=True, help="主题文件夹名(英文/数字/-)")
|
||||
ap.add_argument("--title", required=True, help="页面标题")
|
||||
ap.add_argument("--figures", required=True, help="图名:小节标题,逗号分隔,如 a:入库,b:直读")
|
||||
ap.add_argument("--page", default="index.html", help="渲染页文件名,默认 index.html")
|
||||
ap.add_argument("--root", help="图表根目录(默认向上查找 .agents/);传 docs/diagrams 本身也可")
|
||||
ap.add_argument("--force", action="store_true", help="覆盖已存在的页面/图源")
|
||||
ap.add_argument("--refresh-viewer", action="store_true", help="用技能 assets 覆盖 docs/diagrams/viewer.js")
|
||||
args = ap.parse_args()
|
||||
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]+", args.topic):
|
||||
sys.exit("--topic 只能用字母/数字/._-")
|
||||
|
||||
if args.root:
|
||||
base = Path(args.root).resolve()
|
||||
root = base if base.name == "diagrams" else base / "docs" / "diagrams"
|
||||
else:
|
||||
root = find_diagrams_root(Path.cwd().resolve())
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
topic_dir = root / args.topic
|
||||
topic_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
ensure_viewer(root, args.refresh_viewer)
|
||||
|
||||
figures = parse_figures(args.figures)
|
||||
sections = "\n".join(SECTION.format(heading=h, name=n) for n, h in figures)
|
||||
|
||||
page_path = topic_dir / args.page
|
||||
if page_path.exists() and not args.force:
|
||||
print(f"已存在,未覆盖:{page_path}")
|
||||
else:
|
||||
page_path.write_text(PAGE.format(title=args.title, topic=args.topic, sections=sections), encoding="utf-8")
|
||||
print(f"写入渲染页:{page_path}")
|
||||
|
||||
for name, heading in figures:
|
||||
mmd = topic_dir / f"{name}.mmd"
|
||||
if mmd.exists() and not args.force:
|
||||
print(f"已存在,未覆盖:{mmd}")
|
||||
continue
|
||||
mmd.write_text(MMD_STUB.format(title=heading, palette=PALETTE), encoding="utf-8")
|
||||
print(f"写入图源:{mmd}")
|
||||
|
||||
register_in_index(root, args.topic, args.title, args.page)
|
||||
|
||||
print("\n预览:")
|
||||
print(f" cd {root} && python3 -m http.server 8000")
|
||||
print(f" http://localhost:8000/{args.topic}/{args.page}")
|
||||
print("\n下一步:按 .agents/skills/mermaid-diagram/SKILL.md 的「图源规范」填充 .mmd,改完递增页面里的 ?v=。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user