Files
2026Technology-Competition/src/genesis/writer/renderer.py
T

26 lines
1.1 KiB
Python

"""渲染:ChapterContent 的 ContentBlock 序列 → DocxInjector.Block 序列(Phase 5)。
字段塌缩(外视 #6):table.headers/caption、list.items/style 在渲染时显式丢弃,
仅保留 DocxInjector.Block 支持的 (kind, text, level, rows)。
"""
from __future__ import annotations
from genesis.writer.docx_injector import Block
from genesis.writer.models import ChapterContent
def render_chapter_blocks(content: ChapterContent) -> list[Block]:
out: list[Block] = []
for b in content.blocks:
if b.type == "heading":
out.append(Block(kind="heading", text=b.text or b.caption or "", level=b.level or 1))
elif b.type == "table":
out.append(Block(kind="table", text=b.caption or "", rows=b.rows or []))
elif b.type == "list":
out.append(Block(kind="list", text="\n".join(b.items or [])))
elif b.type == "note":
out.append(Block(kind="note", text=b.text or b.caption or ""))
else: # paragraph 及未知类型
out.append(Block(kind="paragraph", text=b.text or b.caption or ""))
return out