christopher-ramirez / secretary

Take the power of Jinja2 templates to OpenOffice and LibreOffice.
Other
190 stars 48 forks source link

It looks like the secretary tool doesn't work with the latest Jinja #73

Open M-Startc opened 3 years ago

M-Startc commented 3 years ago

I have Jinja2=3.0.1 and I see "jinja2.nodes.EvalContext object at ...." in the document fields instead of the needed content. I could get the secretary tool working after Jinja2 downgrade to 2.11.2 I use python 3.8 in my project and use the tool with OpenOffice odt document

Thank you

Jonathan-Cortex commented 3 years ago

I second this, exact same symptoms in a project I'm working on, jinja >3 doesn't work and gives "jinja2.nodes.EvalContext object at ..." but works fine after downgrade. I used the same version as M-Startc to be sure (Your comment saved me btw, thank you !). Python 3.8 as well.

tigerfoot commented 2 years ago

same

Jenselme commented 2 years ago

In addition to my PR #74, if you can't apply it, you should be able to prevent the issue by passing a custom environment:

from secretary import Renderer, UndefinedSilently, pad_string
from jinja2 import Environment
# markupsafe is a dependency of jinja2.
from markupsafe import Markup

def finalize_value(value):
    if isinstance(value, Markup):
            return value

    # get_escaped_var_value is a static method.
    return Markup(Renderer.get_escaped_var_value(value))

environment = Environment(undefined=UndefinedSilently,
                                           autoescape=True,
                                           finalize=finalize_value)
environment.filters['pad'] = pad_string
environment.globals['SafeValue'] = Markup
renderer = Renderer(environment)
# Those are standard method, we need to have a proper instance for them to work.
environment.filters['markdown'] = renderer.markdown_filter
environment.filters['image'] = renderer.image_filter

I haven't tested it but it should work and provide you the same level of functionality.

raffienficiaud commented 2 years ago

@Jenselme tested and it works, thanks!

PhE commented 2 years ago

You can also patch jinja before importing secretary:

    import jinja2
    import markupsafe
    jinja2.Markup = markupsafe.Markup
    jinja2.evalcontextfilter = jinja2.pass_context
    import secretary