Ticket #14516: 14516.diff
File 14516.diff, 3.3 KB (added by , 14 years ago) |
---|
-
django/utils/html.py
165 165 text = trailing_empty_content_re.sub('', text) 166 166 return text 167 167 clean_html = allow_lazy(clean_html, unicode) 168 169 def remove_tags(value, tags): 170 """Returns the given HTML with given tags removed.""" 171 tags = [re.escape(tag) for tag in tags.split()] 172 tags_re = u'(%s)' % u'|'.join(tags) 173 starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U) 174 endtag_re = re.compile(u'</%s>' % tags_re) 175 value = starttag_re.sub(u'', value) 176 value = endtag_re.sub(u'', value) 177 return value 178 remove_tags = allow_lazy(remove_tags, unicode) 179 180 def slugify(value): 181 """ 182 Normalizes given string, converts to lowercase, removes non-alpha characters, 183 and converts spaces to hyphens. 184 """ 185 import unicodedata 186 value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') 187 value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) 188 return mark_safe(re.sub('[-\s]+', '-', value)) 189 slugify = allow_lazy(slugify, unicode) -
django/template/defaultfilters.py
221 221 Normalizes string, converts to lowercase, removes non-alpha characters, 222 222 and converts spaces to hyphens. 223 223 """ 224 import unicodedata 225 value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') 226 value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) 227 return mark_safe(re.sub('[-\s]+', '-', value)) 224 from django.utils.html import slugify 225 return slugify(value) 228 226 slugify.is_safe = True 229 227 slugify = stringfilter(slugify) 230 228 … … 453 451 454 452 def removetags(value, tags): 455 453 """Removes a space separated list of [X]HTML tags from the output.""" 456 tags = [re.escape(tag) for tag in tags.split()] 457 tags_re = u'(%s)' % u'|'.join(tags) 458 starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U) 459 endtag_re = re.compile(u'</%s>' % tags_re) 460 value = starttag_re.sub(u'', value) 461 value = endtag_re.sub(u'', value) 462 return value 454 from django.utils.html import remove_tags 455 return remove_tags(value, tags) 463 456 removetags.is_safe = True 464 457 removetags = stringfilter(removetags) 465 458 -
tests/regressiontests/utils/html.py
109 109 ) 110 110 for value, output in items: 111 111 self.check_output(f, value, output) 112 113 def test_slugify(self): 114 f = html.slugify 115 items = ( 116 (u'Hello, World!', 'hello-world'), 117 (u'spam & eggs', 'spam-eggs'), 118 ) 119 for value, output in items: 120 self.check_output(f, value, output) 121 122 def test_remove_tags(self): 123 f = html.remove_tags 124 items = ( 125 ("<b><i>Yes</i></b>", "b i", "Yes"), 126 ("<a>x</a> <p><b>y</b></p>", "a b", "x <p>y</p>"), 127 ) 128 for value, tags, output in items: 129 self.assertEquals(f(value, tags), output) 130 No newline at end of file