Ticket #4573: django-linebreaks-v3.diff

File django-linebreaks-v3.diff, 3.4 KB (added by Johan Bergström <bugs@…>, 17 years ago)

Fixed a typo..

  • 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=""):
     263    "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)"
    264264    from django.utils.html import linebreaks
    265     return linebreaks(value)
     265    xhtml = True
     266    if arg == "html":
     267        xhtml = False
     268    return linebreaks(value, paragraph=True, xhtml=xhtml)
    266269linebreaks = stringfilter(linebreaks)
    267270
    268 def linebreaksbr(value):
    269     "Converts newlines into <br />s"
    270     return value.replace('\n', '<br />')
     271def linebreaksbr(value, arg=""):
     272    "Converts newlines into line breaks (<br />, <br>)"
     273    from django.utils.html import linebreaks
     274    xhtml = True
     275    if arg == "html":
     276        xhtml = False
     277    return linebreaks(value, paragraph=False, xhtml=xhtml)
    271278linebreaksbr = stringfilter(linebreaksbr)
    272279
    273280def 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`` which returns ``<br>``
     1063instead of ``<br />``.
    10621064
    10631065linebreaksbr
    10641066~~~~~~~~~~~~
    10651067
    10661068Converts newlines into ``<br />`` tags.
     1069Takes an optional argument - ``html`` which returns ``<br>``
     1070instead of ``<br />``.
    10671071
    10681072linenumbers
    10691073~~~~~~~~~~~
Back to Top