Code

Ticket #4573: django-linebreaks-html-v5.diff

File django-linebreaks-html-v5.diff, 5.4 KB (added by Johan Bergström <bugs@…>, 7 years ago)

Thank you both for feedback. Here's the same patch with the <br/> behaviour reverted.

Line 
1Index: django/utils/html.py
2===================================================================
3--- django/utils/html.py        (revision 5883)
4+++ django/utils/html.py        (working copy)
5@@ -30,12 +30,20 @@
6     return force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
7 escape = allow_lazy(escape, unicode)
8 
9-def linebreaks(value):
10-    "Convert newlines into <p> and <br />s."
11+def linebreaks(value, paragraph=True, xhtml=True):
12+    """
13+    Converts newlines into paragraphs (<p>) containing line breaks (<br/>, <br>)
14+    """
15+    br = '<br />'
16+    if not xhtml:
17+        br = '<br>'
18     value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
19-    paras = re.split('\n{2,}', value)
20-    paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
21-    return u'\n\n'.join(paras)
22+    if paragraph:
23+        paras = re.split('\n{2,}', value)
24+        paras = [u'<p>%s</p>' % p.strip().replace('\n', br) for p in paras]
25+        return u'\n\n'.join(paras)
26+    else:
27+        return  value.strip().replace('\n', br)
28 linebreaks = allow_lazy(linebreaks, unicode)
29 
30 def strip_tags(value):
31Index: django/template/defaultfilters.py
32===================================================================
33--- django/template/defaultfilters.py   (revision 5883)
34+++ django/template/defaultfilters.py   (working copy)
35@@ -253,15 +253,26 @@
36     return escape(value)
37 escape = stringfilter(escape)
38 
39-def linebreaks(value):
40-    "Converts newlines into <p> and <br />s"
41+def linebreaks(value , arg=""):
42+    """
43+    Converts newlines into paragraphs and linebreaks. Accepts 'html' as
44+    optional parameter; this will return html compliant newlines (<br>)
45+    instead of the default xhtml (<br />).
46+    """
47     from django.utils.html import linebreaks
48-    return linebreaks(value)
49+    xhtml = (arg != 'html')
50+    return linebreaks(value, paragraph=True, xhtml=xhtml)
51 linebreaks = stringfilter(linebreaks)
52 
53-def linebreaksbr(value):
54-    "Converts newlines into <br />s"
55-    return value.replace('\n', '<br />')
56+def linebreaksbr(value, arg=""):
57+    """
58+    Converts newlines into linebreaks. Accepts 'html' as optional parameter;
59+    this will return html compliant newlines (<br>) instead of the default
60+    xhtml (<br />).
61+    """
62+    from django.utils.html import linebreaks
63+    xhtml = (arg != 'html')
64+    return linebreaks(value, paragraph=False, xhtml=xhtml)
65 linebreaksbr = stringfilter(linebreaksbr)
66 
67 def removetags(value, tags):
68Index: tests/regressiontests/utils/tests.py
69===================================================================
70--- tests/regressiontests/utils/tests.py        (revision 5883)
71+++ tests/regressiontests/utils/tests.py        (working copy)
72@@ -39,13 +39,14 @@
73     def test_linebreaks(self):
74         f = html.linebreaks
75         items = (
76-            ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
77-            ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
78-            ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"),
79-            ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
80+            (("para1\n\npara2\r\rpara3", True, True ), "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
81+            (("para1\n\npara2\r\rpara3", False, False ), "para1<br><br>para2<br><br>para3"),
82+            (("para1\nsub1\rsub2\n\npara2", True, True ), "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"),
83+            (("para1\r\n\r\npara2\rsub1\r\rpara4", False, True ), "para1<br /><br />para2<br />sub1<br /><br />para4"),
84+            (("para1\tmore\n\npara2", True, False), "<p>para1\tmore</p>\n\n<p>para2</p>"),
85         )
86         for value, output in items:
87-            self.check_output(f, value, output)
88+            self.assertEqual(f(value[0],paragraph=value[1],xhtml=value[2]),output)
89 
90     def test_strip_tags(self):
91         f = html.strip_tags
92Index: tests/regressiontests/defaultfilters/tests.py
93===================================================================
94--- tests/regressiontests/defaultfilters/tests.py       (revision 5883)
95+++ tests/regressiontests/defaultfilters/tests.py       (working copy)
96@@ -202,6 +202,9 @@
97 >>> linebreaks(u'line 1\nline 2')
98 u'<p>line 1<br />line 2</p>'
99 
100+>>> linebreaks(u'line 1\nline 2', "html")
101+u'<p>line 1<br>line 2</p>'
102+
103 >>> removetags(u'some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img')
104 u'some <b>html</b> with alert("You smell") disallowed  tags'
105 
106@@ -467,6 +470,12 @@
107 u'<p>123</p>'
108 >>> linebreaksbr(123)
109 u'123'
110+>>> linebreaksbr('123\n123')
111+u'123<br />123'
112+>>> linebreaksbr('123\n123',"bad syntax")
113+u'123<br />123'
114+>>> linebreaksbr('123\n123',"html")
115+u'123<br>123'
116 >>> removetags(123, 'a')
117 u'123'
118 >>> striptags(123)
119Index: docs/templates.txt
120===================================================================
121--- docs/templates.txt  (revision 5883)
122+++ docs/templates.txt  (working copy)
123@@ -1118,13 +1118,18 @@
124 linebreaks
125 ~~~~~~~~~~
126 
127-Converts newlines into ``<p>`` and ``<br />`` tags.
128+Converts newlines into paragraphs ``<p>`` containing line breaks ``<br />``,
129+``<br>``.
130 
131+**Argument:** ``'html'`` will result in html compliant output.
132+
133 linebreaksbr
134 ~~~~~~~~~~~~
135 
136-Converts newlines into ``<br />`` tags.
137+Converts newlines into ``<br />``, ``<br>`` tags.
138 
139+**Argument:** ``'html'`` will result in html compliant output.
140+
141 linenumbers
142 ~~~~~~~~~~~
143