fix(writer): map_template consumes real ParsedTemplate sections (positional section ids)

This commit is contained in:
lhl
2026-08-13 09:38:14 +08:00
parent c1fbcff088
commit d237201407
2 changed files with 53 additions and 24 deletions
+27 -13
View File
@@ -1,22 +1,36 @@
"""模板 → 有序章节规格映射(Phase 5)。"""
"""模板 → 有序章节规格映射(Phase 5)。
真实 ParsedTemplate.sections 为 ChapterMarker 列表;章节由 type=="heading" 起,
紧随其后的 type=="placeholder" 且形如 `section:<id>` 的标记归属该章,
用于确定 chapter_id 与 section_placeholder(语言无关、按文档顺序)。
"""
from __future__ import annotations
import re
from genesis.data_models import ParsedTemplate
from genesis.writer.models import ChapterSpec
_SECTION_RE = re.compile(r"^section:(.+)$")
def map_template(parsed) -> list[ChapterSpec]:
"""按模板 Heading 层级顺序产出有序章节列表。
`parsed.chapters` 为 WordTemplateParser 产出的章节标记列表,
每项含 chapter_id / title / section_placeholder 属性。
"""
def map_template(parsed: ParsedTemplate) -> list[ChapterSpec]:
specs: list[ChapterSpec] = []
for ch in getattr(parsed, "chapters", []):
specs.append(
ChapterSpec(
chapter_id=getattr(ch, "chapter_id", ""),
title=getattr(ch, "title", ""),
section_placeholder=getattr(ch, "section_placeholder", None),
idx = 0
current: ChapterSpec | None = None
for ch in getattr(parsed, "sections", []):
t = getattr(ch, "type", None)
if t == "heading":
idx += 1
current = ChapterSpec(
chapter_id=f"chapter_{idx}", title=getattr(ch, "name", ""), section_placeholder=None
)
)
specs.append(current)
elif t == "placeholder" and current is not None:
m = _SECTION_RE.match(getattr(ch, "name", ""))
if m:
sid = m.group(1)
current.section_placeholder = getattr(ch, "name", "")
if current.chapter_id.startswith("chapter_"):
current.chapter_id = sid
return specs