feat: MD-style popup print for strategy doc and checklist

Open clean white document in new tab with auto print dialog instead of printing the dark hub page.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-06 03:28:44 +08:00
parent 0d5b20c0db
commit 22b2db34fc
6 changed files with 245 additions and 105 deletions
+212 -16
View File
@@ -218,6 +218,217 @@ def strategy_meta_payload() -> dict[str, Any]:
return {"ok": True, "exchanges": tabs}
def _checklist_html(checklist: dict[str, Any]) -> str:
groups = checklist.get("groups") or []
parts = [f"<h2>{escape_html(str(checklist.get('title') or '开仓检查清单'))}</h2>"]
for grp in groups:
if not isinstance(grp, dict):
continue
gtitle = escape_html(str(grp.get("title") or ""))
parts.append(f"<h3>{gtitle}</h3><ul class=\"checklist\">")
for item in grp.get("items") or []:
parts.append(f"<li><span class=\"box\" aria-hidden=\"true\">☐</span> {escape_html(str(item))}</li>")
parts.append("</ul>")
footnotes = checklist.get("footnotes") or []
if footnotes:
parts.append("<ul class=\"footnotes\">")
for note in footnotes:
parts.append(f"<li>{escape_html(str(note))}</li>")
parts.append("</ul>")
return "\n".join(parts)
def _print_document_css() -> str:
return """
body {
margin: 0;
padding: 36px 28px 48px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
"Hiragino Sans GB", "Microsoft YaHei", sans-serif;
font-size: 14px;
line-height: 1.65;
color: #1a1a1a;
background: #fff;
}
.doc {
max-width: 720px;
margin: 0 auto;
}
.doc-head {
margin-bottom: 28px;
padding-bottom: 14px;
border-bottom: 1px solid #e5e5e5;
}
.doc-head h1 {
margin: 0 0 8px;
font-size: 1.55rem;
font-weight: 600;
line-height: 1.3;
}
.doc-meta {
margin: 0;
color: #666;
font-size: 0.85rem;
}
.doc-body h2 {
font-size: 1.12rem;
margin: 1.6em 0 0.55em;
font-weight: 600;
}
.doc-body h3 {
font-size: 1rem;
margin: 1.2em 0 0.45em;
font-weight: 600;
}
.doc-body h2:first-child,
.doc-body h3:first-child {
margin-top: 0;
}
.doc-body p {
margin: 0.65em 0;
}
.doc-body table {
border-collapse: collapse;
width: 100%;
font-size: 0.92rem;
margin: 10px 0 14px;
}
.doc-body th,
.doc-body td {
border: 1px solid #d8d8d8;
padding: 8px 10px;
text-align: left;
vertical-align: top;
}
.doc-body blockquote {
margin: 12px 0;
padding: 8px 14px;
border-left: 4px solid #c8c8c8;
color: #444;
background: #fafafa;
}
.doc-body pre,
.doc-body code {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.88em;
}
.doc-body pre {
padding: 10px 12px;
background: #f6f6f6;
border: 1px solid #e8e8e8;
border-radius: 4px;
overflow-x: auto;
}
.doc-body hr {
border: none;
border-top: 1px solid #e5e5e5;
margin: 1.4em 0;
}
.checklist {
list-style: none;
margin: 0 0 16px;
padding: 0;
}
.checklist li {
margin: 7px 0;
padding: 0;
}
.box {
display: inline-block;
width: 1.05em;
margin-right: 6px;
font-size: 1.05em;
line-height: 1.2;
}
.footnotes {
margin: 18px 0 0;
padding-left: 20px;
color: #666;
font-size: 0.85rem;
}
.doc-foot {
margin-top: 28px;
padding-top: 12px;
border-top: 1px dashed #ddd;
color: #888;
font-size: 0.78rem;
}
@media print {
body { padding: 0; }
.doc { max-width: none; }
.doc-head { break-after: avoid; }
.doc-body h2, .doc-body h3 { break-after: avoid; }
.checklist li { break-inside: avoid; }
}
"""
def _print_auto_script() -> str:
return """<script>
(function () {
function done() {
try { window.close(); } catch (e) {}
}
window.addEventListener("afterprint", done, { once: true });
window.addEventListener("load", function () {
window.setTimeout(function () { window.print(); }, 120);
window.setTimeout(done, 60000);
});
})();
</script>"""
def build_print_html(exchange_key: str, part: str = "doc") -> str:
"""part: doc | checklist"""
payload = load_strategy_payload(exchange_key)
key = payload["exchange_key"]
label = payload["label"]
title = payload["title"]
version = payload.get("version") or ""
checklist = payload.get("checklist") or {}
now = datetime.now(timezone.utc).astimezone().strftime("%Y-%m-%d %H:%M")
part = (part or "doc").strip().lower()
css = _print_document_css()
if part == "checklist":
cl_title = str(checklist.get("title") or "开仓检查清单")
page_title = f"{label} · {cl_title}"
body = f"""<article class="doc">
<header class="doc-head">
<h1>{escape_html(cl_title)}</h1>
<p class="doc-meta">{escape_html(label)} · {escape_html(version)} · 打印 {escape_html(now)}</p>
</header>
<div class="doc-body">{_checklist_html(checklist)}</div>
</article>"""
elif part == "doc":
page_title = f"{label} · 策略说明"
source = payload.get("md_source") or ""
body = f"""<article class="doc">
<header class="doc-head">
<h1>{escape_html(title)}</h1>
<p class="doc-meta">{escape_html(label)} · {escape_html(version)} · 打印 {escape_html(now)}</p>
</header>
<div class="doc-body">{payload.get("strategy_html") or ""}</div>
<footer class="doc-foot">文档:{escape_html(str(source))}{(" · " + escape_html(version)) if version else ""}</footer>
</article>"""
else:
raise KeyError(part)
return f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{escape_html(page_title)}</title>
<style>{css}</style>
</head>
<body>
{body}
{_print_auto_script()}
</body>
</html>"""
def build_export_html(exchange_key: str) -> str:
payload = load_strategy_payload(exchange_key)
key = payload["exchange_key"]
@@ -225,23 +436,8 @@ def build_export_html(exchange_key: str) -> str:
title = payload["title"]
version = payload.get("version") or ""
checklist = payload.get("checklist") or {}
groups = checklist.get("groups") or []
now = datetime.now(timezone.utc).astimezone().strftime("%Y-%m-%d %H:%M")
checklist_html_parts = [
f"<h2>{escape_html(str(checklist.get('title') or '开仓检查清单'))}</h2>"
]
for grp in groups:
if not isinstance(grp, dict):
continue
gtitle = escape_html(str(grp.get("title") or ""))
checklist_html_parts.append(f"<h3>{gtitle}</h3><ul>")
for item in grp.get("items") or []:
checklist_html_parts.append(
f"<li><span class=\"box\">☐</span> {escape_html(str(item))}</li>"
)
checklist_html_parts.append("</ul>")
checklist_html = "\n".join(checklist_html_parts)
checklist_html = _checklist_html(checklist)
return f"""<!DOCTYPE html>
<html lang="zh-CN">