Index: django/utils/html.py =================================================================== --- django/utils/html.py (revision 5883) +++ django/utils/html.py (working copy) @@ -30,12 +30,20 @@ return force_unicode(html).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''') escape = allow_lazy(escape, unicode) -def linebreaks(value): - "Convert newlines into
and
s."
+def linebreaks(value, paragraph=True, xhtml=True):
+ """
+ Converts newlines into paragraphs (
) containing line breaks (
,
)
+ """
+ br = '
'
+ if not xhtml:
+ br = '
'
value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
- paras = re.split('\n{2,}', value)
- paras = [u'
%s
' % p.strip().replace('\n', '%s
' % p.strip().replace('\n', br) for p in paras] + return u'\n\n'.join(paras) + else: + return value.strip().replace('\n', br) linebreaks = allow_lazy(linebreaks, unicode) def strip_tags(value): Index: django/template/defaultfilters.py =================================================================== --- django/template/defaultfilters.py (revision 5883) +++ django/template/defaultfilters.py (working copy) @@ -253,15 +253,26 @@ return escape(value) escape = stringfilter(escape) -def linebreaks(value): - "Converts newlines into and
s"
+def linebreaks(value , arg=""):
+ """
+ Converts newlines into paragraphs and linebreaks. Accepts 'html' as
+ optional parameter; this will return html compliant newlines (
)
+ instead of the default xhtml (
).
+ """
from django.utils.html import linebreaks
- return linebreaks(value)
+ xhtml = (arg != 'html')
+ return linebreaks(value, paragraph=True, xhtml=xhtml)
linebreaks = stringfilter(linebreaks)
-def linebreaksbr(value):
- "Converts newlines into
s"
- return value.replace('\n', '
')
+def linebreaksbr(value, arg=""):
+ """
+ Converts newlines into linebreaks. Accepts 'html' as optional parameter;
+ this will return html compliant newlines (
) instead of the default
+ xhtml (
).
+ """
+ from django.utils.html import linebreaks
+ xhtml = (arg != 'html')
+ return linebreaks(value, paragraph=False, xhtml=xhtml)
linebreaksbr = stringfilter(linebreaksbr)
def removetags(value, tags):
Index: tests/regressiontests/utils/tests.py
===================================================================
--- tests/regressiontests/utils/tests.py (revision 5883)
+++ tests/regressiontests/utils/tests.py (working copy)
@@ -39,13 +39,14 @@
def test_linebreaks(self):
f = html.linebreaks
items = (
- ("para1\n\npara2\r\rpara3", "
para1
\n\npara2
\n\npara3
"), - ("para1\nsub1\rsub2\n\npara2", "para1
sub1
sub2
para2
"), - ("para1\r\n\r\npara2\rsub1\r\rpara4", "para1
\n\npara2
sub1
para4
"), - ("para1\tmore\n\npara2", "para1\tmore
\n\npara2
"), + (("para1\n\npara2\r\rpara3", True, True ), "para1
\n\npara2
\n\npara3
"), + (("para1\n\npara2\r\rpara3", False, False ), "para1para1
sub1
sub2
para2
"), + (("para1\r\n\r\npara2\rsub1\r\rpara4", False, True ), "para1para1\tmore
\n\npara2
"), ) for value, output in items: - self.check_output(f, value, output) + self.assertEqual(f(value[0],paragraph=value[1],xhtml=value[2]),output) def test_strip_tags(self): f = html.strip_tags Index: tests/regressiontests/defaultfilters/tests.py =================================================================== --- tests/regressiontests/defaultfilters/tests.py (revision 5883) +++ tests/regressiontests/defaultfilters/tests.py (working copy) @@ -202,6 +202,9 @@ >>> linebreaks(u'line 1\nline 2') u'line 1
line 2
line 1
line 2
123
' >>> linebreaksbr(123) u'123' +>>> linebreaksbr('123\n123') +u'123`` and ``
`` tags.
+Converts newlines into paragraphs ``
`` containing line breaks ``
``,
+``
``.
+**Argument:** ``'html'`` will result in html compliant output.
+
linebreaksbr
~~~~~~~~~~~~
-Converts newlines into ``
`` tags.
+Converts newlines into ``
``, ``
`` tags.
+**Argument:** ``'html'`` will result in html compliant output.
+
linenumbers
~~~~~~~~~~~