Skip to content

템플릿 자동화

"""Check the template before rendering: both formats."""
from pathlib import Path
from pywpdoc import Document


def run(fmt: str, out_dir: Path) -> Path:
    out_dir.mkdir(parents=True, exist_ok=True)
    doc = Document.new(fmt)
    doc.add_paragraph("Customer: {{customer.name}}")
    data = {"customer": {"name": "Rydolabs"}}
    report = doc.check_template(data)
    assert report.ok and report.tag_count == 1
    assert doc.render_template(data) == 1
    path = out_dir / f"template.{fmt}"
    doc.save(path)
    assert "Rydolabs" in Document.open(path).paragraphs[-1].text
    return path


if __name__ == "__main__":
    import sys
    print(run(sys.argv[1], Path(sys.argv[2])))
  1. doc.check_template(data)로 원본을 수정하지 않고 태그를 검사합니다.
  2. 반환 객체의 report.ok, missing_keys, issues, tag_count로 치환 가능 여부를 판단합니다.
  3. 문제가 없으면 doc.render_template(data)를 실행하고 저장합니다.

조건/반복 블록은 sections=True, 반복 표 행은 table_rows=True 설정이 필요할 수 있습니다. 이러한 템플릿은 간단한 변수 치환보다 구조 제약이 커서 실제 포맷별 문서로 검증해야 합니다. render_template()은 원본 문서 객체를 수정합니다.