Commits

Takeshi Komiya committed a56b815

* Add nl2br filter

Comments (0)

Files changed (3)

app/lib/__init__.py

Empty file added.

app/lib/myfilters/__init__.py

+import re
+
+from jinja2 import evalcontextfilter, 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
+
+
+def install(app):
+    app.template_filter('nl2br')(nl2br)
 from google.appengine.ext.webapp.util import run_wsgi_app
 from models import Map, Marker, Polyline
 from flask import Flask, request, render_template
+import myfilters
+
 
 PAGESIZE=10
 
 app = Flask(__name__)
 app.debug = True
+myfilters.install(app)
 
 
 def get_template_hash():