Skip to content

Instantly share code, notes, and snippets.

@jsykora
Created January 25, 2012 15:00
Show Gist options
  • Save jsykora/1676645 to your computer and use it in GitHub Desktop.
Save jsykora/1676645 to your computer and use it in GitHub Desktop.
Django XML Line breaks template filter
from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe, SafeData
from django.utils.html import escape
from django.utils.text import normalize_newlines
register = Library()
@register.filter()
@stringfilter
def linebreaksxml(value, autoescape=True):
"""
Converts all newlines in a piece of plain text to XML line breaks
(``<text:line-break />``).
"""
autoescape = autoescape and not isinstance(value, SafeData)
value = normalize_newlines(value)
if autoescape:
value = escape(value)
return mark_safe(value.replace('\n', '<text:line-break />'))
@jsykora
Copy link
Author

jsykora commented Mar 8, 2012

updated to include the necessary functions for safe strings. Turned on autoescape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment