Auto HTML Linebreak filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def autolinebreaks(value, autoescape=None):
    """
    Checks if the content is html or plain text if plain text 
    replaces line breaks with appropriate HTML; a single
    newline becomes an HTML line break (``<br />``) and a new line
    followed by a blank line becomes a paragraph break (``</p>``).
    """
    import re
    html_match = re.compile('<br>|<br />|<p>|<table>', re.IGNORECASE)
    if not html_match.search(value):
        from django.utils.html import linebreaks
        autoescape = autoescape and not isinstance(value, SafeData)
        return mark_safe(linebreaks(value, autoescape))
    else:
        return value
autolinebreaks.is_safe = True
autolinebreaks.needs_autoescape = True
autolinebreaks = stringfilter(autolinebreaks)
register.filter(autolinebreaks)

More like this

  1. HTML to text filter by MasonM 3 years, 6 months ago
  2. Django filter stack to cleanup WYSIWYG output by jbergantine 5 months, 1 week ago
  3. make an unordered html list by techiegurl 3 years, 8 months ago
  4. Soft-wrap long lines by Ubercore 3 years, 10 months ago
  5. Avoid widows using a template filter by jcroft 4 years, 11 months ago

Comments

(Forgotten your password?)