Ticket #4573: django-linebreaks.diff

File django-linebreaks.diff, 3.4 KB (added by Lfe, 8 years ago)

Last patch somehow didn't apply cleanly. Here's the new version (with a proper mime-type)

  • django/utils/html.py

     
    2828        html = str(html)
    2929    return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
    3030
    31 def linebreaks(value):
    32     "Converts newlines into <p> and <br />s"
     31def linebreaks(value, paragraph=True, xhtml=True):
     32    "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)"
     33    br = '<br />'
     34    if not xhtml:
     35        br = '<br>'
     36
    3337    value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
    34     paras = re.split('\n{2,}', value)
    35     paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
    36     return '\n\n'.join(paras)
    3738
     39    if paragraph:
     40        paras = re.split('\n{2,}', value)
     41        paras = ['<p>%s</p>' % p.strip().replace('\n', br) for p in paras]
     42        return '\n\n'.join(paras)
     43    else:
     44        return value.strip().replace('\n', br)
     45
    3846def strip_tags(value):
    3947    "Returns the given HTML with all tags stripped"
    4048    return re.sub(r'<[^>]*?>', '', value)
  • django/template/defaultfilters.py

     
    259259    return escape(value)
    260260escape = stringfilter(escape)
    261261
    262 def linebreaks(value):
    263     "Converts newlines into <p> and <br />s"
     262def linebreaks(value, arg=True):
     263    "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)"
    264264    from django.utils.html import linebreaks
    265     return linebreaks(value)
     265    if arg is "html" or arg is False:
     266        arg = False
     267    return linebreaks(value, paragraph=True, xhtml=arg)
    266268linebreaks = stringfilter(linebreaks)
    267269
    268 def linebreaksbr(value):
    269     "Converts newlines into <br />s"
    270     return value.replace('\n', '<br />')
     270def linebreaksbr(value, arg=True):
     271    "Converts newlines into line breaks (<br />, <br>)"
     272    from django.utils.html import linebreaks
     273    if arg is "html" or arg is False:
     274        arg = False
     275    return linebreaks(value, paragraph=False, xhtml=arg)
    271276linebreaksbr = stringfilter(linebreaksbr)
    272277
    273278def removetags(value, tags):
  • tests/regressiontests/defaultfilters/tests.py

     
    440440'123'
    441441>>> linebreaks(123)
    442442'<p>123</p>'
     443>>> linebreaks('hello\nworld', 'html')
     444'<p>hello<br>world</p>'
    443445>>> linebreaksbr(123)
    444446'123'
     447>>> linebreaksbr('hello\nworld!')
     448'hello<br />world'
     449>>> linebreaksbr('hello\nworld!', 'html')
     450'hello<br>world'
    445451>>> removetags(123, 'a')
    446452'123'
    447453>>> striptags(123)
  • docs/templates.txt

     
    10591059~~~~~~~~~~
    10601060
    10611061Converts newlines into ``<p>`` and ``<br />`` tags.
     1062Takes an optional argument - ``html`` (or 0) which returns ``<br>``
     1063instead of ``<br />``.
    10621064
    10631065linebreaksbr
    10641066~~~~~~~~~~~~
    10651067
    10661068Converts newlines into ``<br />`` tags.
     1069Takes an optional argument - ``html`` (or 0) which returns ``<br>``
     1070instead of ``<br />``.
    10671071
    10681072linenumbers
    10691073~~~~~~~~~~~
Back to Top