You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.1 KiB
33 lines
1.1 KiB
6 years ago
|
"""Define a Jinja2 Processor which applies programmable templating to the input stream."""
|
||
|
|
||
|
from typing import Iterable, Optional, Dict, cast
|
||
|
|
||
|
from jinja2 import Environment, FileSystemLoader
|
||
|
|
||
|
from .passthrough import PassThrough
|
||
|
|
||
|
|
||
|
class Jinja2(PassThrough):
|
||
|
"""Pass the input stream through Jinja2 for scritable templating."""
|
||
|
|
||
|
def process(self, input_file: Iterable, ctx: Optional[Dict] = None) -> Iterable:
|
||
|
"""Return an iterable object of the post-processed file.
|
||
|
|
||
|
Arguments:
|
||
|
input_file (iterable): An input stream
|
||
|
ctx (dict, optional): A context object generated from the processor configuration
|
||
|
|
||
|
|
||
|
Returns:
|
||
|
iterable: The post-processed output stream
|
||
|
"""
|
||
|
ctx = cast(Dict, ctx)
|
||
|
template_env = Environment(loader=FileSystemLoader(ctx["templates"]))
|
||
|
template_env.globals.update(ctx["globals"])
|
||
|
template_env.filters.update(ctx["filters"])
|
||
|
tmpl = template_env.from_string("".join([x for x in input_file]))
|
||
|
return tmpl.render(metadata=ctx)
|
||
|
|
||
|
|
||
|
processor = Jinja2
|