我正在使用 Jinja2 的 nl2br 过滤器,它看起来像:
import re
from jinja2 import environmentfilter, Markup, escape
_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')
@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
问题是如果“值”除了 ascii 字符之外还有其他字符(例如:“/mɒnˈtænə/”会导致它失败)。我收到此错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 889, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 879, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 876, in wsgi_app
rv = self.dispatch_request()
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 695, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/mcrittenden/Dropbox/Code/dropdo/dropdo.py", line 105, in view
return render_template(template, src = url, data = content)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/templating.py", line 85, in render_template
context, ctx.app)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/templating.py", line 69, in _render
rv = template.render(context)
File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.5.5-py2.6.egg/jinja2/environment.py", line 891, in render
return self.environment.handle_exception(exc_info, True)
File "/home/mcrittenden/Dropbox/Code/dropdo/templates/text.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/home/mcrittenden/Dropbox/Code/dropdo/templates/layout.html", line 25, in top-level template code
{% block content %}{% endblock %}
File "/home/mcrittenden/Dropbox/Code/dropdo/templates/text.html", line 8, in block "content"
{{ data|nl2br }}
File "/home/mcrittenden/Dropbox/Code/dropdo/dropdo.py", line 26, in nl2br
for p in _paragraph_re.split(escape(value)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 12: ordinal not in range(128)
防止错误但不完全删除问题字符的最佳方法是什么?