Skip to content

Target

sereto.target

render_target_to_tex(project, target, target_ix, version)

Render selected target (top-level document) to TeX format.

Source code in sereto/target.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@validate_call
def render_target_to_tex(project: Project, target: Target, target_ix: int, version: ProjectVersion) -> str:
    """Render selected target (top-level document) to TeX format."""
    assert target.path is not None

    cfg = project.config.at_version(version=version)

    # Construct path to target template
    template = project.path / "layouts/target.tex.j2"
    if not template.is_file():
        raise SeretoPathError(f"template not found: '{template}'")

    # Make shallow dict - values remain objects on which we can call their methods in Jinja
    cfg_dict = {key: getattr(cfg, key) for key in cfg.model_dump()}

    # Render Jinja2 template
    target_generator = render_jinja2(
        templates=[
            project.path / "layouts/generated",
            project.path / "layouts",
            project.path / "includes",
            project.path,
        ],
        file=template,
        vars={
            "target": target,
            "target_index": target_ix,
            "c": cfg,
            "config": project.config,
            "version": version,
            "project_path": project.path,
            **cfg_dict,
        },
    )

    return "".join(target_generator)