LaTeX¶
The LaTeX body itself comes from tmark's writer; what stays on the Python side
is the escaping table the templates and fonts reuse, the asset plumbing, the
Pygments bridge of the highlight pass, and the template runtime.
LaTeX escaping and text-transform machinery (backend responsibility).
The writer owns escaping. This module moves :func:escape_latex_chars out of
adapters/latex/utils.py (where it was a shared utility) into the LaTeX
backend, and gathers the unicode → LaTeX text transforms (dashes, smart
quotes, sub/superscript runs, emoji segmentation, math-payload protection)
that the legacy adapters/handlers/inline.py PRE phase applied to plain
text nodes. The IR carries raw text in :class:~tmark.ir.Str; the writer
escapes it here exactly as the old pipeline did, so the produced LaTeX is
byte-identical.
escape_latex_chars ¶
escape_latex_chars(
text: str, *, legacy_accents: bool = False
) -> str
Escape LaTeX special characters leveraging pylatexenc.
Source code in src/texsmith/writers/latex/escaper.py
75 76 77 78 79 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 116 117 | |
escape_text_segment ¶
escape_text_segment(
text: str,
*,
legacy_accents: bool = False,
emoji_renderer: Callable[[str], str] | None = None,
) -> str
Escape a plain-text run, splitting out emoji clusters.
Mirrors adapters/handlers/inline.py:_escape_text_segment. Plain
fragments go through :func:prepare_plain_text; emoji clusters are handed
to emoji_renderer (font command or artifact image). When no renderer is
supplied, emoji are escaped as ordinary text.
Source code in src/texsmith/writers/latex/escaper.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
prepare_plain_text ¶
prepare_plain_text(
text: str, *, legacy_accents: bool = False
) -> str
Escape and apply the unicode text transforms for a plain-text run.
Mirrors adapters/handlers/inline.py:_prepare_plain_text: smart quotes
and dashes are normalised first, the text is escaped, then unicode
sub/superscript runs become \textsubscript / \textsuperscript.
Source code in src/texsmith/writers/latex/escaper.py
299 300 301 302 303 304 305 306 307 308 309 310 | |
Asset helpers shared across media and block handlers.
AssetOptions
dataclass
¶
AssetOptions(
assets: AssetRegistry,
source_dir: Path,
document_path: Path,
copy_assets: bool = True,
convert_assets: bool = False,
hash_assets: bool = False,
emitter: Any = None,
diagrams_backend: str = "playwright",
project_dir: Path | None = None,
http_user_agent: str | None = None,
mermaid_config: Any = None,
drawio_crop: Any = None,
)
What the asset helpers need to store or convert one image.
Built once per document by the assets pass (PassContext has no
single object shaped like this); passed explicitly instead of the
RenderContextLike duck a _LegacyContext used to fake around a
string-keyed runtime dict.
store_local_image_asset ¶
store_local_image_asset(
opts: AssetOptions,
resolved: Path,
*,
options: Mapping[str, str] | None = None,
) -> Path
Copy or convert a local asset and register it on the context.
options are the per-image render options (crop for draw.io) the
document set through attr_list. They select the converter behaviour and
therefore take part in the asset key: the same diagram rendered cropped and
uncropped is two distinct assets.
Source code in src/texsmith/writers/latex/assets.py
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
store_remote_image_asset ¶
store_remote_image_asset(
opts: AssetOptions, url: str
) -> Path
Fetch a remote asset, mirror it locally, and register it.
Source code in src/texsmith/writers/latex/assets.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
LaTeX-specific utilities exposed by Texsmith.
escape_latex_chars ¶
escape_latex_chars(
text: str, *, legacy_accents: bool = False
) -> str
Escape LaTeX special characters leveraging pylatexenc.
Source code in src/texsmith/writers/latex/escaper.py
75 76 77 78 79 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 116 117 | |
Pygments integration helpers for LaTeX rendering.
PygmentsLatexHighlighter ¶
PygmentsLatexHighlighter(
*,
commandprefix: str = "PY",
style: str = "bw",
verboptions: str | None = None,
)
Convert source code to LaTeX using Pygments.
Source code in src/texsmith/adapters/latex/pygments.py
63 64 65 66 67 68 69 70 71 72 | |
add_break_points ¶
add_break_points(latex: str, chars: str) -> str
Insert \allowbreak after each break character of latex.
Highlighted output interleaves \PY{token} markers with escaped
source text; the markers are left untouched so a token name is never
rewritten, and only the payload between them gains break opportunities.
Source code in src/texsmith/adapters/latex/pygments.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
render ¶
render(
code: str,
language: str,
*,
linenos: bool,
highlight_lines: Iterable[int] | None = None,
) -> tuple[str, str]
Return the LaTeX code and style definitions for a payload.
Source code in src/texsmith/adapters/latex/pygments.py
79 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 116 117 118 119 120 121 122 123 124 125 | |
render_inline ¶
render_inline(code: str, language: str) -> tuple[str, str]
Return inline LaTeX macros for a code snippet (no Verbatim env).
Source code in src/texsmith/adapters/latex/pygments.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
Utilities for parsing and presenting output from LaTeX engine builds.
LatexLogParser ¶
LatexLogParser()
Incrementally parse LaTeX output into structured messages.
Source code in src/texsmith/adapters/latex/engines/latex/log.py
106 107 108 109 | |
finalize ¶
finalize() -> list[LatexMessage]
Flush any pending message.
Source code in src/texsmith/adapters/latex/engines/latex/log.py
165 166 167 | |
process_line ¶
process_line(line: str) -> list[LatexMessage]
Process a log line and return messages that have just completed.
Source code in src/texsmith/adapters/latex/engines/latex/log.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
LatexMessage
dataclass
¶
LatexMessage(
severity: LatexMessageSeverity,
summary: str,
details: list[str] = list(),
indent: int = 0,
)
Structured LaTeX message extracted from the build log.
LatexMessageSeverity ¶
Bases: Enum
Classification severity extracted from LaTeX output.
LatexStreamResult
dataclass
¶
LatexStreamResult(
returncode: int, messages: list[LatexMessage]
)
Result of streaming LaTeX engine output.
parse_latex_log ¶
parse_latex_log(log_path: Path) -> list[LatexMessage]
Parse a LaTeX log file into structured messages.
Source code in src/texsmith/adapters/latex/engines/latex/log.py
485 486 487 488 489 490 491 492 493 494 | |
stream_latexmk_output ¶
stream_latexmk_output(
command: Sequence[str],
*,
cwd: str,
env: Mapping[str, str],
console: Console,
verbosity: int = 0,
) -> LatexStreamResult
Execute a LaTeX engine command and render output incrementally.
Source code in src/texsmith/adapters/latex/engines/latex/log.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
LaTeX escaping — single source of truth lives in the LaTeX writer.
Escaping is a backend responsibility, so the canonical implementation moved to
:mod:texsmith.writers.latex.escaper. This module re-exports it so existing
importers (templates, fonts, extensions, legacy handler helpers) keep a stable
import path without duplicating the logic.
escape_latex_chars ¶
escape_latex_chars(
text: str, *, legacy_accents: bool = False
) -> str
Escape LaTeX special characters leveraging pylatexenc.
Source code in src/texsmith/writers/latex/escaper.py
75 76 77 78 79 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 116 117 | |
Public template helpers shared across the conversion pipeline.
BaseTemplate ¶
BaseTemplate(root: Path)
Base class shared by template implementations.
Source code in src/texsmith/core/templates/base.py
67 68 69 70 71 72 73 74 75 | |
render_template ¶
render_template(template_name: str, **context: Any) -> str
Render a template using the configured Jinja environment.
Source code in src/texsmith/core/templates/base.py
77 78 79 80 81 82 83 84 85 | |
ResolvedAsset
dataclass
¶
ResolvedAsset(
source: Path,
destination: Path,
template: bool = False,
encoding: str | None = None,
template_name: str | None = None,
)
Resolved template asset ready to be materialised.
TemplateAsset ¶
Bases: BaseModel
Description of individual template assets.
TemplateAttributeSpec ¶
Bases: BaseModel
Typed attribute definition used to build template defaults.
format says what the value is: markdown (the default) a line or
two of prose rendered through the writer, raw a value the template
emits as it stands, and file the path of a file whose text the
template inlines verbatim — a title page, an imprint, a preamble the
author keeps in their own .tex.
default_value ¶
default_value() -> Any
Return a deep copy of the attribute default.
Source code in src/texsmith/core/templates/manifest.py
630 631 632 | |
TemplateBinding
dataclass
¶
TemplateBinding(
runtime: TemplateRuntime | None,
instance: WrappableTemplate | None,
name: str | None,
engine: str | None,
requires_shell_escape: bool,
slots: dict[str, TemplateSlot],
default_slot: str,
base_level: int | None,
)
Binding between slot requests and a LaTeX template.
pass_specs ¶
pass_specs() -> tuple[PassSpec, ...]
The IR passes the bound template declares ([latex.template] passes).
Empty without a template: the passes of a template apply only while that template renders, never globally.
Source code in src/texsmith/core/templates/runtime.py
50 51 52 53 54 55 56 57 58 | |
slot_levels ¶
slot_levels(*, offset: int = 0) -> dict[str, int]
Return the resolved base level for each slot.
Source code in src/texsmith/core/templates/runtime.py
60 61 62 63 | |
TemplateError ¶
Bases: LatexRenderingError
Raised when a LaTeX template cannot be loaded or rendered.
TemplateInfo ¶
Bases: BaseModel
Metadata describing the LaTeX template payload.
attribute_defaults ¶
attribute_defaults() -> dict[str, Any]
Return a deep copy of template attribute defaults.
Source code in src/texsmith/core/templates/manifest.py
988 989 990 | |
attribute_owners ¶
attribute_owners() -> dict[str, str]
Return attribute ownership map (name -> owner).
Source code in src/texsmith/core/templates/manifest.py
1003 1004 1005 | |
emit_defaults ¶
emit_defaults() -> dict[str, Any]
Return default attributes emitted by the template.
Source code in src/texsmith/core/templates/manifest.py
992 993 994 | |
pass_specs ¶
pass_specs() -> tuple[PassSpec, ...]
The IR passes declared by passes, resolved when the manifest loaded.
build_pipeline(extra=info.pass_specs()) places them among the
bundled passes by their after constraints, for the duration of this
template's render only.
Source code in src/texsmith/core/templates/manifest.py
959 960 961 962 963 964 965 966 | |
resolve_attributes ¶
resolve_attributes(
overrides: Mapping[str, Any] | None = None,
) -> dict[str, Any]
Return defaults merged with overrides using the attribute specification.
Source code in src/texsmith/core/templates/manifest.py
999 1000 1001 | |
resolve_slots ¶
resolve_slots() -> tuple[dict[str, TemplateSlot], str]
Return declared slots ensuring a single default sink exists.
Source code in src/texsmith/core/templates/manifest.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | |
TemplateManifest ¶
Bases: BaseModel
Structured manifest describing a multi-backend template.
latex is always present (the primary backend); typst is optional and
only present for templates that also support the Typst backend.
load
classmethod
¶
load(manifest_path: Path) -> TemplateManifest
Load and validate a manifest from disk.
Source code in src/texsmith/core/templates/manifest.py
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 | |
section ¶
section(backend: str) -> TemplateInfo
Return the :class:TemplateInfo for backend (latex/typst).
Raises :class:TemplateError when a template is asked for a backend it
does not declare — explicit, never a silent fallback to LaTeX.
Source code in src/texsmith/core/templates/manifest.py
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 | |
TemplateRuntime
dataclass
¶
TemplateRuntime(
instance: WrappableTemplate,
name: str,
engine: str | None,
requires_shell_escape: bool,
slots: dict[str, TemplateSlot],
default_slot: str,
base_level: int | None,
extras: dict[str, Any] = dict(),
)
Resolved template metadata reused across conversions.
TemplateSlot ¶
Bases: BaseModel
Configuration describing how content is injected into a template slot.
resolve_level ¶
resolve_level(fallback: int) -> int
Return the base level applied to rendered headings for this slot.
Source code in src/texsmith/core/templates/manifest.py
93 94 95 96 97 98 99 100 | |
TemplateWrapResult
dataclass
¶
TemplateWrapResult(
latex_output: str,
template_context: dict[str, Any],
output_path: Path | None,
asset_paths: list[Path] = list(),
asset_pairs: list[tuple[Path, Path]] = list(),
rendered_fragments: list[str] = list(),
)
Result artefacts produced after wrapping LaTeX with a template.
WrappableTemplate ¶
WrappableTemplate(root: Path)
Bases: BaseTemplate
Template capable of wrapping a generated LaTeX fragment.
Source code in src/texsmith/core/templates/base.py
67 68 69 70 71 72 73 74 75 | |
iter_assets ¶
iter_assets() -> Iterable[ResolvedAsset]
Yield declared template assets.
Source code in src/texsmith/core/templates/base.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
prepare_context ¶
prepare_context(
latex_body: str,
*,
overrides: Mapping[str, Any] | None = None,
) -> dict[str, Any]
Build the rendering context shared by the template and its assets.
Source code in src/texsmith/core/templates/base.py
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
wrap_document ¶
wrap_document(
latex_body: str,
*,
overrides: Mapping[str, Any] | None = None,
context: Mapping[str, Any] | None = None,
) -> str
Render the template entry point using the provided LaTeX payload.
Source code in src/texsmith/core/templates/base.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
build_template_overrides ¶
build_template_overrides(
front_matter: Mapping[str, Any] | None,
) -> dict[str, Any]
Build template overrides from front matter while preserving metadata.
Source code in src/texsmith/core/templates/runtime.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
coerce_base_level ¶
coerce_base_level(
value: Any, *, allow_none: bool = True
) -> int | None
Normalise base-level metadata to an integer or None.
Source code in src/texsmith/core/templates/runtime.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
copy_template_assets ¶
copy_template_assets(
template: WrappableTemplate,
output_dir: Path,
*,
context: Mapping[str, Any] | None = None,
overrides: Mapping[str, Any] | None = None,
assets: Iterable[ResolvedAsset] | None = None,
) -> list[Path]
Copy the template declared assets into the selected output directory.
Source code in src/texsmith/core/templates/loader.py
72 73 74 75 76 77 78 79 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 | |
declared_containers ¶
declared_containers(template: str | None) -> frozenset[str]
The container names template declares (containers), for both backends.
Read straight from the manifest, because it has to be known before the
documents are parsed: tmark reports solution as
container-unknown while it parses, and no pass — not even the
template's own, which rewrites that very container — can take a record
back. The parse is one for both backends (prepare_documents runs before
the LaTeX binding is resolved and before the Typst template is loaded), so
the names of [latex.template] and [typst.template] are read
together.
A template that cannot be loaded declares nothing: that failure belongs to the render, which reports it with its own message.
Source code in src/texsmith/core/templates/runtime.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
discover_templates ¶
discover_templates() -> list[dict[str, str]]
Return available templates in discovery order.
Source code in src/texsmith/core/templates/loader.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
extract_base_level_override ¶
extract_base_level_override(
overrides: Mapping[str, Any] | None,
) -> Any
Extract a base level override from template metadata overrides.
Source code in src/texsmith/core/templates/runtime.py
105 106 107 108 109 110 111 112 113 114 115 116 | |
extract_language_from_front_matter ¶
extract_language_from_front_matter(
front_matter: Mapping[str, Any] | None,
) -> str | None
Inspect front matter for language hints.
Source code in src/texsmith/core/templates/runtime.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
load_template ¶
load_template(identifier: str) -> WrappableTemplate
Load a template selected by name or filesystem path.
Source code in src/texsmith/core/templates/loader.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
load_template_runtime ¶
load_template_runtime(template: str) -> TemplateRuntime
Resolve template metadata for repeated conversions.
Source code in src/texsmith/core/templates/runtime.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
normalise_template_language ¶
normalise_template_language(
value: str | None,
) -> str | None
Normalise language codes and map them through babel aliases when available.
Source code in src/texsmith/core/templates/runtime.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
register_attribute_normaliser ¶
register_attribute_normaliser(
name: str,
func: Callable[[Any, TemplateAttributeSpec, Any], Any],
*,
override: bool = False,
) -> None
Register a custom attribute normaliser under a short name.
Public counterpart of the internal _register_attribute_normaliser
decorator. Template packages may call this at import time so that a
manifest can refer to the normaliser by name. Registering over an
existing built-in name raises TemplateError unless override=True.
Source code in src/texsmith/core/templates/manifest.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
resolve_template_binding ¶
resolve_template_binding(
*,
template: str | None,
template_runtime: TemplateRuntime | None,
template_overrides: Mapping[str, Any],
slot_requests: Mapping[str, str],
warn: Callable[[str], None] | None = None,
) -> tuple[TemplateBinding, dict[str, str]]
Resolve template runtime metadata and apply slot overrides.
Source code in src/texsmith/core/templates/runtime.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
resolve_template_language ¶
resolve_template_language(
explicit: str | None,
front_matter: Mapping[str, Any] | None,
) -> str
Resolve the effective template language from CLI and front matter inputs.
Source code in src/texsmith/core/templates/runtime.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
wrap_template_document ¶
wrap_template_document(
*,
template: WrappableTemplate,
default_slot: str,
slot_outputs: Mapping[str, str],
slot_output_overrides: Mapping[str, str] | None = None,
document_state: DocumentState,
template_overrides: Mapping[str, Any] | None,
output_dir: Path,
copy_assets: bool = True,
output_name: str | None = None,
bibliography_path: Path | None = None,
emitter: DiagnosticEmitter | None = None,
fragments: list[str] | None = None,
template_runtime: TemplateRuntime | None = None,
) -> TemplateWrapResult
Wrap LaTeX content using a template and optional asset copying.
Source code in src/texsmith/core/templates/wrapper.py
71 72 73 74 75 76 77 78 79 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
Core classes used to load and wrap LaTeX templates.
BaseTemplate ¶
BaseTemplate(root: Path)
Base class shared by template implementations.
Source code in src/texsmith/core/templates/base.py
67 68 69 70 71 72 73 74 75 | |
render_template ¶
render_template(template_name: str, **context: Any) -> str
Render a template using the configured Jinja environment.
Source code in src/texsmith/core/templates/base.py
77 78 79 80 81 82 83 84 85 | |
ResolvedAsset
dataclass
¶
ResolvedAsset(
source: Path,
destination: Path,
template: bool = False,
encoding: str | None = None,
template_name: str | None = None,
)
Resolved template asset ready to be materialised.
TemplateError ¶
Bases: LatexRenderingError
Raised when a LaTeX template cannot be loaded or rendered.
WrappableTemplate ¶
WrappableTemplate(root: Path)
Bases: BaseTemplate
Template capable of wrapping a generated LaTeX fragment.
Source code in src/texsmith/core/templates/base.py
67 68 69 70 71 72 73 74 75 | |
iter_assets ¶
iter_assets() -> Iterable[ResolvedAsset]
Yield declared template assets.
Source code in src/texsmith/core/templates/base.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
prepare_context ¶
prepare_context(
latex_body: str,
*,
overrides: Mapping[str, Any] | None = None,
) -> dict[str, Any]
Build the rendering context shared by the template and its assets.
Source code in src/texsmith/core/templates/base.py
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
wrap_document ¶
wrap_document(
latex_body: str,
*,
overrides: Mapping[str, Any] | None = None,
context: Mapping[str, Any] | None = None,
) -> str
Render the template entry point using the provided LaTeX payload.
Source code in src/texsmith/core/templates/base.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
load_specialised_template ¶
load_specialised_template(
path: Path,
) -> WrappableTemplate | None
Import a template-specific module to retrieve a specialised implementation.
Source code in src/texsmith/core/templates/base.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
Helpers for loading template instances from disk or entry points.
copy_template_assets ¶
copy_template_assets(
template: WrappableTemplate,
output_dir: Path,
*,
context: Mapping[str, Any] | None = None,
overrides: Mapping[str, Any] | None = None,
assets: Iterable[ResolvedAsset] | None = None,
) -> list[Path]
Copy the template declared assets into the selected output directory.
Source code in src/texsmith/core/templates/loader.py
72 73 74 75 76 77 78 79 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 | |
discover_templates ¶
discover_templates() -> list[dict[str, str]]
Return available templates in discovery order.
Source code in src/texsmith/core/templates/loader.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
load_template ¶
load_template(identifier: str) -> WrappableTemplate
Load a template selected by name or filesystem path.
Source code in src/texsmith/core/templates/loader.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
Pydantic models describing LaTeX template manifests.
CompatInfo ¶
Bases: BaseModel
Compatibility constraints declared by the template.
LatexSection ¶
Bases: BaseModel
Section grouping LaTeX-specific manifest settings.
TemplateAsset ¶
Bases: BaseModel
Description of individual template assets.
TemplateAttributeResolver ¶
TemplateAttributeResolver(
specs: Mapping[str, TemplateAttributeSpec],
)
Resolve attribute values from overrides using a typed specification.
Source code in src/texsmith/core/templates/manifest.py
761 762 | |
TemplateAttributeSpec ¶
Bases: BaseModel
Typed attribute definition used to build template defaults.
format says what the value is: markdown (the default) a line or
two of prose rendered through the writer, raw a value the template
emits as it stands, and file the path of a file whose text the
template inlines verbatim — a title page, an imprint, a preamble the
author keeps in their own .tex.
default_value ¶
default_value() -> Any
Return a deep copy of the attribute default.
Source code in src/texsmith/core/templates/manifest.py
630 631 632 | |
TemplateError ¶
Bases: LatexRenderingError
Raised when a LaTeX template cannot be loaded or rendered.
TemplateInfo ¶
Bases: BaseModel
Metadata describing the LaTeX template payload.
attribute_defaults ¶
attribute_defaults() -> dict[str, Any]
Return a deep copy of template attribute defaults.
Source code in src/texsmith/core/templates/manifest.py
988 989 990 | |
attribute_owners ¶
attribute_owners() -> dict[str, str]
Return attribute ownership map (name -> owner).
Source code in src/texsmith/core/templates/manifest.py
1003 1004 1005 | |
emit_defaults ¶
emit_defaults() -> dict[str, Any]
Return default attributes emitted by the template.
Source code in src/texsmith/core/templates/manifest.py
992 993 994 | |
pass_specs ¶
pass_specs() -> tuple[PassSpec, ...]
The IR passes declared by passes, resolved when the manifest loaded.
build_pipeline(extra=info.pass_specs()) places them among the
bundled passes by their after constraints, for the duration of this
template's render only.
Source code in src/texsmith/core/templates/manifest.py
959 960 961 962 963 964 965 966 | |
resolve_attributes ¶
resolve_attributes(
overrides: Mapping[str, Any] | None = None,
) -> dict[str, Any]
Return defaults merged with overrides using the attribute specification.
Source code in src/texsmith/core/templates/manifest.py
999 1000 1001 | |
resolve_slots ¶
resolve_slots() -> tuple[dict[str, TemplateSlot], str]
Return declared slots ensuring a single default sink exists.
Source code in src/texsmith/core/templates/manifest.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | |
TemplateManifest ¶
Bases: BaseModel
Structured manifest describing a multi-backend template.
latex is always present (the primary backend); typst is optional and
only present for templates that also support the Typst backend.
load
classmethod
¶
load(manifest_path: Path) -> TemplateManifest
Load and validate a manifest from disk.
Source code in src/texsmith/core/templates/manifest.py
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 | |
section ¶
section(backend: str) -> TemplateInfo
Return the :class:TemplateInfo for backend (latex/typst).
Raises :class:TemplateError when a template is asked for a backend it
does not declare — explicit, never a silent fallback to LaTeX.
Source code in src/texsmith/core/templates/manifest.py
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 | |
TemplateSlot ¶
Bases: BaseModel
Configuration describing how content is injected into a template slot.
resolve_level ¶
resolve_level(fallback: int) -> int
Return the base level applied to rendered headings for this slot.
Source code in src/texsmith/core/templates/manifest.py
93 94 95 96 97 98 99 100 | |
TypstSection ¶
Bases: BaseModel
Section grouping Typst-specific manifest settings.
Mirrors :class:LatexSection: it reuses the backend-agnostic
:class:TemplateInfo (attributes / slots) so a single template package can
declare a parallel [typst.template] block driving the Typst backend.
register_attribute_normaliser ¶
register_attribute_normaliser(
name: str,
func: Callable[[Any, TemplateAttributeSpec, Any], Any],
*,
override: bool = False,
) -> None
Register a custom attribute normaliser under a short name.
Public counterpart of the internal _register_attribute_normaliser
decorator. Template packages may call this at import time so that a
manifest can refer to the normaliser by name. Registering over an
existing built-in name raises TemplateError unless override=True.
Source code in src/texsmith/core/templates/manifest.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
Runtime helpers for binding LaTeX templates to rendered documents.
TemplateBinding
dataclass
¶
TemplateBinding(
runtime: TemplateRuntime | None,
instance: WrappableTemplate | None,
name: str | None,
engine: str | None,
requires_shell_escape: bool,
slots: dict[str, TemplateSlot],
default_slot: str,
base_level: int | None,
)
Binding between slot requests and a LaTeX template.
pass_specs ¶
pass_specs() -> tuple[PassSpec, ...]
The IR passes the bound template declares ([latex.template] passes).
Empty without a template: the passes of a template apply only while that template renders, never globally.
Source code in src/texsmith/core/templates/runtime.py
50 51 52 53 54 55 56 57 58 | |
slot_levels ¶
slot_levels(*, offset: int = 0) -> dict[str, int]
Return the resolved base level for each slot.
Source code in src/texsmith/core/templates/runtime.py
60 61 62 63 | |
TemplateRuntime
dataclass
¶
TemplateRuntime(
instance: WrappableTemplate,
name: str,
engine: str | None,
requires_shell_escape: bool,
slots: dict[str, TemplateSlot],
default_slot: str,
base_level: int | None,
extras: dict[str, Any] = dict(),
)
Resolved template metadata reused across conversions.
build_template_overrides ¶
build_template_overrides(
front_matter: Mapping[str, Any] | None,
) -> dict[str, Any]
Build template overrides from front matter while preserving metadata.
Source code in src/texsmith/core/templates/runtime.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
coerce_base_level ¶
coerce_base_level(
value: Any, *, allow_none: bool = True
) -> int | None
Normalise base-level metadata to an integer or None.
Source code in src/texsmith/core/templates/runtime.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
declared_containers ¶
declared_containers(template: str | None) -> frozenset[str]
The container names template declares (containers), for both backends.
Read straight from the manifest, because it has to be known before the
documents are parsed: tmark reports solution as
container-unknown while it parses, and no pass — not even the
template's own, which rewrites that very container — can take a record
back. The parse is one for both backends (prepare_documents runs before
the LaTeX binding is resolved and before the Typst template is loaded), so
the names of [latex.template] and [typst.template] are read
together.
A template that cannot be loaded declares nothing: that failure belongs to the render, which reports it with its own message.
Source code in src/texsmith/core/templates/runtime.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
extract_base_level_override ¶
extract_base_level_override(
overrides: Mapping[str, Any] | None,
) -> Any
Extract a base level override from template metadata overrides.
Source code in src/texsmith/core/templates/runtime.py
105 106 107 108 109 110 111 112 113 114 115 116 | |
extract_language_from_front_matter ¶
extract_language_from_front_matter(
front_matter: Mapping[str, Any] | None,
) -> str | None
Inspect front matter for language hints.
Source code in src/texsmith/core/templates/runtime.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
load_template_runtime ¶
load_template_runtime(template: str) -> TemplateRuntime
Resolve template metadata for repeated conversions.
Source code in src/texsmith/core/templates/runtime.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
normalise_template_language ¶
normalise_template_language(
value: str | None,
) -> str | None
Normalise language codes and map them through babel aliases when available.
Source code in src/texsmith/core/templates/runtime.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
resolve_template_binding ¶
resolve_template_binding(
*,
template: str | None,
template_runtime: TemplateRuntime | None,
template_overrides: Mapping[str, Any],
slot_requests: Mapping[str, str],
warn: Callable[[str], None] | None = None,
) -> tuple[TemplateBinding, dict[str, str]]
Resolve template runtime metadata and apply slot overrides.
Source code in src/texsmith/core/templates/runtime.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
resolve_template_language ¶
resolve_template_language(
explicit: str | None,
front_matter: Mapping[str, Any] | None,
) -> str
Resolve the effective template language from CLI and front matter inputs.
Source code in src/texsmith/core/templates/runtime.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |