Index: django/utils/html.py =================================================================== --- django/utils/html.py (revision 5478) +++ django/utils/html.py (working copy) @@ -28,13 +28,21 @@ html = str(html) return html.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''') -def linebreaks(value): - "Converts 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', value) # normalize newlines
- paras = re.split('\n{2,}', value)
- paras = ['
%s
' % p.strip().replace('\n', '%s
' % p.strip().replace('\n', br) for p in paras] + return '\n\n'.join(paras) + else: + return value.strip().replace('\n', br) + def strip_tags(value): "Returns the given HTML with all tags stripped" return re.sub(r'<[^>]*?>', '', value) Index: django/template/defaultfilters.py =================================================================== --- django/template/defaultfilters.py (revision 5478) +++ django/template/defaultfilters.py (working copy) @@ -259,15 +259,20 @@ return escape(value) escape = stringfilter(escape) -def linebreaks(value): - "Converts newlines into and
s"
+def linebreaks(value, arg=True):
+ "Converts newlines into paragraphs (
) containing line breaks (
,
)"
from django.utils.html import linebreaks
- return linebreaks(value)
+ if arg is "html" or arg is False:
+ arg = False
+ return linebreaks(value, paragraph=True, xhtml=arg)
linebreaks = stringfilter(linebreaks)
-def linebreaksbr(value):
- "Converts newlines into
s"
- return value.replace('\n', '
')
+def linebreaksbr(value, arg=True):
+ "Converts newlines into line breaks (
,
)"
+ from django.utils.html import linebreaks
+ if arg is "html" or arg is False:
+ arg = False
+ return linebreaks(value, paragraph=False, xhtml=arg)
linebreaksbr = stringfilter(linebreaksbr)
def removetags(value, tags):
Index: tests/regressiontests/defaultfilters/tests.py
===================================================================
--- tests/regressiontests/defaultfilters/tests.py (revision 5478)
+++ tests/regressiontests/defaultfilters/tests.py (working copy)
@@ -440,8 +440,14 @@
'123'
>>> linebreaks(123)
'
123
' +>>> linebreaks('hello\nworld', 'html') +'hello
world
`` and ``
`` tags.
+Takes an optional argument - ``html`` (or 0) which returns ``
``
+instead of ``
``.
linebreaksbr
~~~~~~~~~~~~
Converts newlines into ``
`` tags.
+Takes an optional argument - ``html`` (or 0) which returns ``
``
+instead of ``
``.
linenumbers
~~~~~~~~~~~