Code

Ticket #4573: django-linebreaks.diff

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

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

Line 
1Index: django/utils/html.py
2===================================================================
3--- django/utils/html.py        (revision 5478)
4+++ django/utils/html.py        (working copy)
5@@ -28,13 +28,21 @@
6         html = str(html)
7     return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
8 
9-def linebreaks(value):
10-    "Converts newlines into <p> and <br />s"
11+def linebreaks(value, paragraph=True, xhtml=True):
12+    "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)"
13+    br = '<br />'
14+    if not xhtml:
15+        br = '<br>'
16+
17     value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
18-    paras = re.split('\n{2,}', value)
19-    paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
20-    return '\n\n'.join(paras)
21 
22+    if paragraph:
23+        paras = re.split('\n{2,}', value)
24+        paras = ['<p>%s</p>' % p.strip().replace('\n', br) for p in paras]
25+        return '\n\n'.join(paras)
26+    else:
27+        return value.strip().replace('\n', br)
28+
29 def strip_tags(value):
30     "Returns the given HTML with all tags stripped"
31     return re.sub(r'<[^>]*?>', '', value)
32Index: django/template/defaultfilters.py
33===================================================================
34--- django/template/defaultfilters.py   (revision 5478)
35+++ django/template/defaultfilters.py   (working copy)
36@@ -259,15 +259,20 @@
37     return escape(value)
38 escape = stringfilter(escape)
39 
40-def linebreaks(value):
41-    "Converts newlines into <p> and <br />s"
42+def linebreaks(value, arg=True):
43+    "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)"
44     from django.utils.html import linebreaks
45-    return linebreaks(value)
46+    if arg is "html" or arg is False:
47+        arg = False
48+    return linebreaks(value, paragraph=True, xhtml=arg)
49 linebreaks = stringfilter(linebreaks)
50 
51-def linebreaksbr(value):
52-    "Converts newlines into <br />s"
53-    return value.replace('\n', '<br />')
54+def linebreaksbr(value, arg=True):
55+    "Converts newlines into line breaks (<br />, <br>)"
56+    from django.utils.html import linebreaks
57+    if arg is "html" or arg is False:
58+        arg = False
59+    return linebreaks(value, paragraph=False, xhtml=arg)
60 linebreaksbr = stringfilter(linebreaksbr)
61 
62 def removetags(value, tags):
63Index: tests/regressiontests/defaultfilters/tests.py
64===================================================================
65--- tests/regressiontests/defaultfilters/tests.py       (revision 5478)
66+++ tests/regressiontests/defaultfilters/tests.py       (working copy)
67@@ -440,8 +440,14 @@
68 '123'
69 >>> linebreaks(123)
70 '<p>123</p>'
71+>>> linebreaks('hello\nworld', 'html')
72+'<p>hello<br>world</p>'
73 >>> linebreaksbr(123)
74 '123'
75+>>> linebreaksbr('hello\nworld!')
76+'hello<br />world'
77+>>> linebreaksbr('hello\nworld!', 'html')
78+'hello<br>world'
79 >>> removetags(123, 'a')
80 '123'
81 >>> striptags(123)
82Index: docs/templates.txt
83===================================================================
84--- docs/templates.txt  (revision 5478)
85+++ docs/templates.txt  (working copy)
86@@ -1059,11 +1059,15 @@
87 ~~~~~~~~~~
88 
89 Converts newlines into ``<p>`` and ``<br />`` tags.
90+Takes an optional argument - ``html`` (or 0) which returns ``<br>``
91+instead of ``<br />``.
92 
93 linebreaksbr
94 ~~~~~~~~~~~~
95 
96 Converts newlines into ``<br />`` tags.
97+Takes an optional argument - ``html`` (or 0) which returns ``<br>``
98+instead of ``<br />``.
99 
100 linenumbers
101 ~~~~~~~~~~~