88
99import jinja2
1010
11+ from . import processing
12+ from .config import render_config
1113from .types import ImageSpec , StyleDict
1214
1315__all__ = [
1416 "create_env" ,
17+ "PDFBakerTemplate" ,
1518 "prepare_template_context" ,
1619]
1720
1821
19- class HighlightingTemplate (jinja2 .Template ): # pylint: disable=too-few-public-methods
20- """A Jinja template that automatically applies highlighting to text .
22+ class PDFBakerTemplate (jinja2 .Template ): # pylint: disable=too-few-public-methods
23+ """A Jinja template with custom rendering capabilities for PDFBaker .
2124
22- This template class extends the base Jinja template to automatically
23- convert <highlight> tags to styled <tspan> elements with the highlight color .
25+ This template class extends the base Jinja template to apply
26+ additional rendering transformations to the template output .
2427 """
2528
2629 def render (self , * args : Any , ** kwargs : Any ) -> str :
27- """Render the template and apply highlighting to the result."""
28- rendered = super ().render (* args , ** kwargs )
30+ """Render the template and apply custom transformations.
2931
30- if "style" in kwargs and "highlight_color" in kwargs ["style" ]:
31- highlight_color = kwargs ["style" ]["highlight_color" ]
32+ Args:
33+ *args: Positional arguments for template rendering
34+ **kwargs: Keyword arguments for template rendering
35+ renderers: Optional list of renderer function names to apply
3236
33- def replacer (match : re .Match [str ]) -> str :
34- content = match .group (1 )
35- return f'<tspan style="fill:{ highlight_color } ">{ content } </tspan>'
37+ Returns:
38+ Rendered template with transformations applied
39+ """
40+ rendered = super ().render (* args , ** kwargs )
3641
37- rendered = re .sub (r"<highlight>(.*?)</highlight>" , replacer , rendered )
42+ for renderer_name in kwargs .get ("renderers" , []):
43+ renderer_func = globals ().get (renderer_name )
44+ if callable (renderer_func ):
45+ rendered = renderer_func (rendered , ** kwargs )
3846
3947 return rendered
4048
4149
50+ def render_highlight (rendered : str , ** kwargs : Any ) -> str :
51+ """Apply highlight tags to the rendered template content.
52+
53+ Convert <highlight> tags to styled <tspan> elements with the highlight color.
54+ """
55+ if "style" in kwargs and "highlight_color" in kwargs ["style" ]:
56+ highlight_color = kwargs ["style" ]["highlight_color" ]
57+
58+ def replacer (match : re .Match [str ]) -> str :
59+ content = match .group (1 )
60+ return f'<tspan style="fill:{ highlight_color } ">{ content } </tspan>'
61+
62+ rendered = re .sub (r"<highlight>(.*?)</highlight>" , replacer , rendered )
63+
64+ return rendered
65+
66+
4267def create_env (
43- templates_dir : Path | None = None , extensions : list [str ] | None = None
68+ templates_dir : Path | None = None ,
69+ extensions : list [str ] | None = None ,
70+ template_filters : list [str ] | None = None ,
4471) -> jinja2 .Environment :
4572 """Create and configure the Jinja environment."""
4673 if templates_dir is None :
@@ -51,20 +78,29 @@ def create_env(
5178 autoescape = jinja2 .select_autoescape (),
5279 extensions = extensions or [],
5380 )
54- env .template_class = HighlightingTemplate
81+ env .template_class = PDFBakerTemplate
82+
83+ if template_filters :
84+ for filter_name in template_filters :
85+ if hasattr (processing , filter_name ):
86+ env .filters [filter_name ] = getattr (processing , filter_name )
87+
5588 return env
5689
5790
5891def prepare_template_context (
5992 config : dict [str ], images_dir : Path | None = None
6093) -> dict [str ]:
61- """Prepare config for template rendering by resolving styles and encoding images.
94+ """Prepare template context with variables/styles/images
95+
96+ Resolves variables, styles and encodes images.
6297
6398 Args:
6499 config: Configuration with optional styles and images
65100 images_dir: Directory containing images to encode
66101 """
67- context = config .copy ()
102+ # Render configuration to resolve template strings inside strings
103+ context = render_config (config )
68104
69105 # Resolve style references to actual theme colors
70106 if "style" in context and "theme" in context :
0 commit comments