Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too many <br> tags. #15

Open
jet10000 opened this issue Mar 14, 2017 · 1 comment
Open

Too many <br> tags. #15

jet10000 opened this issue Mar 14, 2017 · 1 comment

Comments

@jet10000
Copy link

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
Copy link
Author

jet10000 commented Mar 14, 2017

@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 />'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant