remotes/origin/artem
Gena 11 years ago
parent 7a9f0894fd
commit 76ff8318de
  1. 33
      batiskaf/jinja2_ext/html_filters.py

@ -0,0 +1,33 @@
from django.utils.text import normalize_newlines
import re
from django.utils.functional import allow_lazy
from django.utils.safestring import SafeData, SafeText, mark_safe
from django.utils.encoding import force_str, force_text
from django.utils import six
def escape(text):
"""
Returns the given text with ampersands, quotes and angle brackets encoded
for use in HTML.
This function always escapes its input, even if it's already escaped and
marked as such. This may result in double-escaping. If this is a concern,
use conditional_escape() instead.
"""
return mark_safe(force_text(text).replace(
'&', '&amp;').replace('<', '&lt;')
.replace('>', '&gt;').replace(
'"', '&quot;').replace("'", '&#39;'))
escape = allow_lazy(escape, six.text_type, SafeText)
def linebreaks(value, autoescape=False):
"""Converts newlines into <p> and <br />s."""
value = normalize_newlines(value)
paras = re.split('\n{2,}', value)
if autoescape:
paras = ['<p>%s</p>' %
escape(p).replace('\n', '<br />') for p in paras]
else:
paras = ['<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
return '\n\n'.join(paras)
Loading…
Cancel
Save