Ticket #4573: django-linebreaks-html-v5.diff
File django-linebreaks-html-v5.diff, 5.4 KB (added by , 17 years ago) |
---|
-
django/utils/html.py
30 30 return force_unicode(html).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''') 31 31 escape = allow_lazy(escape, unicode) 32 32 33 def linebreaks(value): 34 "Convert newlines into <p> and <br />s." 33 def linebreaks(value, paragraph=True, xhtml=True): 34 """ 35 Converts newlines into paragraphs (<p>) containing line breaks (<br/>, <br>) 36 """ 37 br = '<br />' 38 if not xhtml: 39 br = '<br>' 35 40 value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines 36 paras = re.split('\n{2,}', value) 37 paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras] 38 return u'\n\n'.join(paras) 41 if paragraph: 42 paras = re.split('\n{2,}', value) 43 paras = [u'<p>%s</p>' % p.strip().replace('\n', br) for p in paras] 44 return u'\n\n'.join(paras) 45 else: 46 return value.strip().replace('\n', br) 39 47 linebreaks = allow_lazy(linebreaks, unicode) 40 48 41 49 def strip_tags(value): -
django/template/defaultfilters.py
253 253 return escape(value) 254 254 escape = stringfilter(escape) 255 255 256 def linebreaks(value): 257 "Converts newlines into <p> and <br />s" 256 def linebreaks(value , arg=""): 257 """ 258 Converts newlines into paragraphs and linebreaks. Accepts 'html' as 259 optional parameter; this will return html compliant newlines (<br>) 260 instead of the default xhtml (<br />). 261 """ 258 262 from django.utils.html import linebreaks 259 return linebreaks(value) 263 xhtml = (arg != 'html') 264 return linebreaks(value, paragraph=True, xhtml=xhtml) 260 265 linebreaks = stringfilter(linebreaks) 261 266 262 def linebreaksbr(value): 263 "Converts newlines into <br />s" 264 return value.replace('\n', '<br />') 267 def linebreaksbr(value, arg=""): 268 """ 269 Converts newlines into linebreaks. Accepts 'html' as optional parameter; 270 this will return html compliant newlines (<br>) instead of the default 271 xhtml (<br />). 272 """ 273 from django.utils.html import linebreaks 274 xhtml = (arg != 'html') 275 return linebreaks(value, paragraph=False, xhtml=xhtml) 265 276 linebreaksbr = stringfilter(linebreaksbr) 266 277 267 278 def removetags(value, tags): -
tests/regressiontests/utils/tests.py
39 39 def test_linebreaks(self): 40 40 f = html.linebreaks 41 41 items = ( 42 ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"), 43 ("para1\nsub1\rsub2\n\npara2", "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"), 44 ("para1\r\n\r\npara2\rsub1\r\rpara4", "<p>para1</p>\n\n<p>para2<br />sub1</p>\n\n<p>para4</p>"), 45 ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"), 42 (("para1\n\npara2\r\rpara3", True, True ), "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"), 43 (("para1\n\npara2\r\rpara3", False, False ), "para1<br><br>para2<br><br>para3"), 44 (("para1\nsub1\rsub2\n\npara2", True, True ), "<p>para1<br />sub1<br />sub2</p>\n\n<p>para2</p>"), 45 (("para1\r\n\r\npara2\rsub1\r\rpara4", False, True ), "para1<br /><br />para2<br />sub1<br /><br />para4"), 46 (("para1\tmore\n\npara2", True, False), "<p>para1\tmore</p>\n\n<p>para2</p>"), 46 47 ) 47 48 for value, output in items: 48 self. check_output(f, value,output)49 self.assertEqual(f(value[0],paragraph=value[1],xhtml=value[2]),output) 49 50 50 51 def test_strip_tags(self): 51 52 f = html.strip_tags -
tests/regressiontests/defaultfilters/tests.py
202 202 >>> linebreaks(u'line 1\nline 2') 203 203 u'<p>line 1<br />line 2</p>' 204 204 205 >>> linebreaks(u'line 1\nline 2', "html") 206 u'<p>line 1<br>line 2</p>' 207 205 208 >>> removetags(u'some <b>html</b> with <script>alert("You smell")</script> disallowed <img /> tags', 'script img') 206 209 u'some <b>html</b> with alert("You smell") disallowed tags' 207 210 … … 467 470 u'<p>123</p>' 468 471 >>> linebreaksbr(123) 469 472 u'123' 473 >>> linebreaksbr('123\n123') 474 u'123<br />123' 475 >>> linebreaksbr('123\n123',"bad syntax") 476 u'123<br />123' 477 >>> linebreaksbr('123\n123',"html") 478 u'123<br>123' 470 479 >>> removetags(123, 'a') 471 480 u'123' 472 481 >>> striptags(123) -
docs/templates.txt
1118 1118 linebreaks 1119 1119 ~~~~~~~~~~ 1120 1120 1121 Converts newlines into ``<p>`` and ``<br />`` tags. 1121 Converts newlines into paragraphs ``<p>`` containing line breaks ``<br />``, 1122 ``<br>``. 1122 1123 1124 **Argument:** ``'html'`` will result in html compliant output. 1125 1123 1126 linebreaksbr 1124 1127 ~~~~~~~~~~~~ 1125 1128 1126 Converts newlines into ``<br />`` tags.1129 Converts newlines into ``<br />``, ``<br>`` tags. 1127 1130 1131 **Argument:** ``'html'`` will result in html compliant output. 1132 1128 1133 linenumbers 1129 1134 ~~~~~~~~~~~ 1130 1135