django-precise-bbcode icon indicating copy to clipboard operation
django-precise-bbcode copied to clipboard

Too many <br> tags.

Open jet10000 opened this issue 8 years ago • 1 comments
trafficstars

django have template tags linebreaks and linebreaksbr.

I use TextField for store my post. in template {{ post.content|safe|bbcode }}, if I input many enter for linebreak, bbcode template tag convert it to too many
tag.

so, I wish bbcode template tag can parse it like linebreaks, not linebreaksbr

thanks

jet10000 avatar Mar 14 '17 07:03 jet10000

@register.filter("linebreaks", is_safe=True, needs_autoescape=True)
@stringfilter
def linebreaks_filter(value, autoescape=True):
    """
    Replaces line breaks in plain text 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>``).
    """
    autoescape = autoescape and not isinstance(value, SafeData)
    return mark_safe(linebreaks(value, autoescape))


@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def linebreaksbr(value, autoescape=True):
    """
    Converts all newlines in a piece of plain text to HTML line breaks
    (``<br />``).
    """
    autoescape = autoescape and not isinstance(value, SafeData)
    value = normalize_newlines(value)
    if autoescape:
        value = escape(value)
    return mark_safe(value.replace('\n', '<br />'))

jet10000 avatar Mar 14 '17 07:03 jet10000