Ticket #4573: django-linebreaks.patch

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

Includes patch, docs and tests.

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