2012-03-01 11:11:25 +01:00
|
|
|
from django.template.defaultfilters import stringfilter
|
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import mark_safe
|
2012-03-01 11:23:56 +01:00
|
|
|
from django.utils.encoding import force_unicode
|
2012-03-01 11:11:25 +01:00
|
|
|
from django import template
|
2012-03-01 11:23:56 +01:00
|
|
|
import re
|
2012-03-01 11:11:25 +01:00
|
|
|
|
2012-03-01 13:24:07 +01:00
|
|
|
from trans.util import split_plural
|
|
|
|
|
2012-03-01 11:11:25 +01:00
|
|
|
register = template.Library()
|
|
|
|
|
2012-03-01 11:23:56 +01:00
|
|
|
|
|
|
|
def fmt_whitespace(value):
|
|
|
|
value = re.sub(r'( +| $|^ )', '<span class="hlspace">\\1</span>', value)
|
|
|
|
return value
|
|
|
|
|
2012-03-01 11:11:25 +01:00
|
|
|
@register.filter
|
|
|
|
@stringfilter
|
2012-03-01 11:23:56 +01:00
|
|
|
def fmttranslation(value):
|
2012-03-01 13:24:07 +01:00
|
|
|
plurals = split_plural(value)
|
|
|
|
parts = []
|
|
|
|
for value in plurals:
|
|
|
|
value = escape(force_unicode(value))
|
|
|
|
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
|
|
|
|
paras = re.split('\n', value)
|
|
|
|
paras = [fmt_whitespace(p) for p in paras]
|
|
|
|
value = '<br />'.join(paras)
|
|
|
|
parts.append(value)
|
|
|
|
value = '<hr />'.join(parts)
|
2012-03-01 11:23:56 +01:00
|
|
|
return mark_safe(value)
|