feat(writer): add renderer (ChapterContent -> DocxInjector.Block)

This commit is contained in:
lhl
2026-08-13 10:08:41 +08:00
parent 9f53d928ab
commit 1c40262401
3 changed files with 58 additions and 0 deletions
+8
View File
@@ -87,3 +87,11 @@ def test_original_content_style_preserved(tmp_path):
# 模板原有段落(尾部固定内容)在注入后仍存在且未被破坏
assert any("尾部固定内容" in p.text for p in out.paragraphs)
# ---------- Block 支持 table/list/note 等 kindrenderer 依赖) ----------
def test_block_accepts_table_list_note():
assert Block(kind="table", text="t", rows=[["x"]]).kind == "table"
assert Block(kind="list", text="a\nb").kind == "list"
assert Block(kind="note", text="n").kind == "note"
+25
View File
@@ -0,0 +1,25 @@
from genesis.writer.renderer import render_chapter_blocks
from genesis.writer.models import ChapterContent, ContentBlock
def _content():
blocks = [
ContentBlock(block_id="1", type="heading", level=2, text="小節"),
ContentBlock(block_id="2", type="paragraph", text="正文"),
ContentBlock(block_id="3", type="table", caption="表1", rows=[["a", "b"], ["1", "2"]]),
ContentBlock(block_id="4", type="list", items=["項目一", "項目二"]),
ContentBlock(block_id="5", type="note", text="注意"),
]
return ChapterContent(chapter_id="db_design", version=1, title="DB 設計", blocks=blocks)
def test_render_maps_all_block_types():
out = render_chapter_blocks(_content())
kinds = [b.kind for b in out]
assert kinds == ["heading", "paragraph", "table", "list", "note"]
# 表:rows 透传
table = out[2]
assert table.kind == "table"
assert table.rows == [["a", "b"], ["1", "2"]]
# 列表:items 拼接进 text
assert "項目一" in out[3].text and "項目二" in out[3].text