acoomans / flask-autodoc

Flask autodoc automatically creates an online documentation for your flask application.
MIT License
98 stars 49 forks source link

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

Open Thorleon opened 6 years ago

Thorleon commented 6 years ago

Seems like there is a missing filter in _autodocdefault.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 commented 6 years ago

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 commented 6 years ago

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'