Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jinja2.exceptions.TemplateAssertionError: no filter named 'nl2br' #39

Open
Thorleon opened this issue Mar 28, 2018 · 2 comments
Open

jinja2.exceptions.TemplateAssertionError: no filter named 'nl2br' #39

Thorleon opened this issue Mar 28, 2018 · 2 comments

Comments

@Thorleon
Copy link

Seems like there is a missing filter in autodoc_default.html , which results in error like:
raise TemplateAssertionError(msg, lineno, self.name, self.filename) jinja2.exceptions.TemplateAssertionError: no filter named 'nl2br'

Workaround for this is to add into Flask app:


    @app.template_filter()
    @evalcontextfilter
    def nl2br(eval_ctx, value):
        result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') \
                              for p in _paragraph_re.split(escape(value)))
        if eval_ctx.autoescape:
            result = Markup(result)
        return result

Python 3.6
pip freeze:
Flask==0.12.2
Jinja2==2.9.6
Flask-Autodoc==0.1.2

@strachg
Copy link

strachg commented May 10, 2018

Is this still a valid fix or has this issue been fixed in the autodoc package? I'm seeing this error but I can see this in the autodoc module:

def add_custom_nl2br_filters(self, app):
    """Add a custom filter nl2br to jinja2
     Replaces all newline to <BR>
    """
    _paragraph_re = re.compile(r'(?:\r\n|\r|\n){3,}')

    @app.template_filter()
    @evalcontextfilter
    def nl2br(eval_ctx, value):
        result = '\n\n'.join('%s' % p.replace('\n', '<br>\n')
                             for p in _paragraph_re.split(value))
        return result

But I'm still getting the error. I'm not beyond saying that I've probably just screwed up something with my implementation of autodoc.

Running:
Flask-Autodoc==0.1.2

@strachg
Copy link

strachg commented May 10, 2018

Ok, I sorted my issue. If anyone else is suffering from this. it looks like the nl2br function still isn't quite right but if you use the one detailed here - http://jinja.pocoo.org/docs/2.10/api/#custom-filters then it made sure the
were not being converted to <br>

The crux of it seems to be this bit here:

result = u'\n\n'.join(u'

%s

' % p.replace('\n', Markup('
\n'))

There is an extra Markup() around the '
\n'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants