parent
7a9f0894fd
commit
76ff8318de
1 changed files with 33 additions and 0 deletions
@ -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( |
||||
'&', '&').replace('<', '<') |
||||
.replace('>', '>').replace( |
||||
'"', '"').replace("'", ''')) |
||||
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…
Reference in new issue