Ticket #4573: django-linebreaks.diff
File django-linebreaks.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/utils/html.py
28 28 html = str(html) 29 29 return html.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''') 30 30 31 def linebreaks(value): 32 "Converts newlines into <p> and <br />s" 31 def linebreaks(value, paragraph=True, xhtml=True): 32 "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)" 33 br = '<br />' 34 if not xhtml: 35 br = '<br>' 36 33 37 value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines 34 paras = re.split('\n{2,}', value)35 paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]36 return '\n\n'.join(paras)37 38 39 if paragraph: 40 paras = re.split('\n{2,}', value) 41 paras = ['<p>%s</p>' % p.strip().replace('\n', br) for p in paras] 42 return '\n\n'.join(paras) 43 else: 44 return value.strip().replace('\n', br) 45 38 46 def strip_tags(value): 39 47 "Returns the given HTML with all tags stripped" 40 48 return re.sub(r'<[^>]*?>', '', value) -
django/template/defaultfilters.py
259 259 return escape(value) 260 260 escape = stringfilter(escape) 261 261 262 def linebreaks(value ):263 "Converts newlines into <p> and <br />s"262 def linebreaks(value, arg=True): 263 "Converts newlines into paragraphs (<p>) containing line breaks (<br />, <br>)" 264 264 from django.utils.html import linebreaks 265 return linebreaks(value) 265 if arg is "html" or arg is False: 266 arg = False 267 return linebreaks(value, paragraph=True, xhtml=arg) 266 268 linebreaks = stringfilter(linebreaks) 267 269 268 def linebreaksbr(value): 269 "Converts newlines into <br />s" 270 return value.replace('\n', '<br />') 270 def linebreaksbr(value, arg=True): 271 "Converts newlines into line breaks (<br />, <br>)" 272 from django.utils.html import linebreaks 273 if arg is "html" or arg is False: 274 arg = False 275 return linebreaks(value, paragraph=False, xhtml=arg) 271 276 linebreaksbr = stringfilter(linebreaksbr) 272 277 273 278 def removetags(value, tags): -
tests/regressiontests/defaultfilters/tests.py
440 440 '123' 441 441 >>> linebreaks(123) 442 442 '<p>123</p>' 443 >>> linebreaks('hello\nworld', 'html') 444 '<p>hello<br>world</p>' 443 445 >>> linebreaksbr(123) 444 446 '123' 447 >>> linebreaksbr('hello\nworld!') 448 'hello<br />world' 449 >>> linebreaksbr('hello\nworld!', 'html') 450 'hello<br>world' 445 451 >>> removetags(123, 'a') 446 452 '123' 447 453 >>> striptags(123) -
docs/templates.txt
1059 1059 ~~~~~~~~~~ 1060 1060 1061 1061 Converts newlines into ``<p>`` and ``<br />`` tags. 1062 Takes an optional argument - ``html`` (or 0) which returns ``<br>`` 1063 instead of ``<br />``. 1062 1064 1063 1065 linebreaksbr 1064 1066 ~~~~~~~~~~~~ 1065 1067 1066 1068 Converts newlines into ``<br />`` tags. 1069 Takes an optional argument - ``html`` (or 0) which returns ``<br>`` 1070 instead of ``<br />``. 1067 1071 1068 1072 linenumbers 1069 1073 ~~~~~~~~~~~