diff --git a/src/genesis/writer/template_mapper.py b/src/genesis/writer/template_mapper.py new file mode 100644 index 0000000..436879d --- /dev/null +++ b/src/genesis/writer/template_mapper.py @@ -0,0 +1,22 @@ +"""模板 → 有序章节规格映射(Phase 5)。""" +from __future__ import annotations + +from genesis.writer.models import ChapterSpec + + +def map_template(parsed) -> list[ChapterSpec]: + """按模板 Heading 层级顺序产出有序章节列表。 + + `parsed.chapters` 为 WordTemplateParser 产出的章节标记列表, + 每项含 chapter_id / title / section_placeholder 属性。 + """ + 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), + ) + ) + return specs diff --git a/tests/test_phase5_template_mapper.py b/tests/test_phase5_template_mapper.py new file mode 100644 index 0000000..001e160 --- /dev/null +++ b/tests/test_phase5_template_mapper.py @@ -0,0 +1,17 @@ +from types import SimpleNamespace +from genesis.writer.template_mapper import map_template +from genesis.writer.models import ChapterSpec + + +def _fake_parsed(): + ch1 = SimpleNamespace(chapter_id="intro", title="はじめに", section_placeholder="{{section:introduction}}") + ch2 = SimpleNamespace(chapter_id="db_design", title="DB 設計", section_placeholder=None) + return SimpleNamespace(chapters=[ch1, ch2]) + + +def test_map_template_ordered(): + specs = map_template(_fake_parsed()) + assert [s.chapter_id for s in specs] == ["intro", "db_design"] + assert specs[0].section_placeholder == "{{section:introduction}}" + assert specs[1].section_placeholder is None + assert all(isinstance(s, ChapterSpec) for s in specs)