fix(定时任务): require_html 选内置模板时用真实模板出页,保留全部子页面
- 模板是 JS 驱动多页 SPA,子页面靠脚本从 DATA 渲染;之前用 style_reference 抽 CSS 摘要 让模型自由渲染,结果只剩主页、子页面全丢 - 改为:_run_html_page_attempt 见 tpl:<id> 参考即提前分支到 _run_html_template_reference, 用真实模板文件出页——可转换模板=检索数据→转换→fill_template;只展示模板=原样输出; 转换失败兜底默认数据,保证页面与子页面始终可用 - 抽出 _convert_to_template_data / _finalize_template_html 复用,删除已废弃的 style_reference - 测试覆盖:真模板保留 const DATA/script/id=tabs;只展示原样;转换失败回退 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ec60b77a71
commit
ba885ca201
3 files changed
+242
-152
No files matched your search
+3
-28
@@ -356,39 +356,14 @@ def _deep_merge(base: dict[str, Any], overrides: dict[str, Any]) -> None:
|
||||
base[k] = v
|
||||
|
||||
|
||||
# ── 作为 require_html 渲染的「风格参考」───────────────────────────────────────────
|
||||
# 「参考收藏页面」下拉里把模板选项的值存成 ``tpl:<id>``,与用户收藏(普通 id)区分。
|
||||
# ── require_html 模式选「内置模板」作参考 ─────────────────────────────────────────
|
||||
# 「参考收藏页面」下拉里模板选项的值存成 ``tpl:<id>``,与用户收藏(普通 id)区分。
|
||||
# 选了模板 → 调度器用**真实模板**出页(保留全部子页面),不抽风格摘要(那会丢子页面)。
|
||||
REFERENCE_PREFIX = "tpl:"
|
||||
|
||||
_STYLE_RE = re.compile(r"<style[^>]*>(.*?)</style>", re.DOTALL | re.IGNORECASE)
|
||||
_SCRIPT_BLOCK_RE = re.compile(r"<script[^>]*>.*?</script>", re.DOTALL | re.IGNORECASE)
|
||||
_BODY_RE = re.compile(r"<body[^>]*>(.*?)</body>", re.DOTALL | re.IGNORECASE)
|
||||
|
||||
|
||||
def reference_template_id(reference: str | None) -> str | None:
|
||||
"""``tpl:no1`` → ``no1``;非模板参考(普通收藏 id / 空)返回 None。"""
|
||||
if isinstance(reference, str) and reference.startswith(REFERENCE_PREFIX):
|
||||
return reference[len(REFERENCE_PREFIX) :] or None
|
||||
return None
|
||||
|
||||
|
||||
def style_reference(template_id: str, *, max_style: int = 24000, max_layout: int = 6000) -> dict[str, str]:
|
||||
"""抽取模板的紧凑「风格/版式参考」(``<style>`` CSS + 去脚本的静态结构骨架)。
|
||||
|
||||
模板内嵌的巨大 DATA/脚本会被剥掉,只留视觉相关的 CSS 与 body 结构,供
|
||||
require_html 的渲染模型**模仿其风格**,而不会把整份(几百 KB~MB 的)模板塞进 prompt。
|
||||
"""
|
||||
text = read_template_text(template_id)
|
||||
styles = _STYLE_RE.findall(text)
|
||||
css = "\n".join(s.strip() for s in styles).strip()
|
||||
if len(css) > max_style:
|
||||
css = css[:max_style] + "\n/* …(CSS 截断) */"
|
||||
layout = ""
|
||||
body = _BODY_RE.search(text)
|
||||
if body is not None:
|
||||
skeleton = _SCRIPT_BLOCK_RE.sub("", body.group(1))
|
||||
skeleton = re.sub(r"\n[ \t]*\n+", "\n", skeleton).strip()
|
||||
if len(skeleton) > max_layout:
|
||||
skeleton = skeleton[:max_layout] + "\n<!-- …(结构截断) -->"
|
||||
layout = skeleton
|
||||
return {"style": css, "layout": layout}
|
||||
Reference in new issue
Block a user