Index: django/utils/html.py =================================================================== --- django/utils/html.py (revision 14551) +++ django/utils/html.py (working copy) @@ -165,3 +165,25 @@ text = trailing_empty_content_re.sub('', text) return text clean_html = allow_lazy(clean_html, unicode) + +def remove_tags(value, tags): + """Returns the given HTML with given tags removed.""" + tags = [re.escape(tag) for tag in tags.split()] + tags_re = u'(%s)' % u'|'.join(tags) + starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U) + endtag_re = re.compile(u'%s>' % tags_re) + value = starttag_re.sub(u'', value) + value = endtag_re.sub(u'', value) + return value +remove_tags = allow_lazy(remove_tags, unicode) + +def slugify(value): + """ + Normalizes given string, converts to lowercase, removes non-alpha characters, + and converts spaces to hyphens. + """ + import unicodedata + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') + value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) + return mark_safe(re.sub('[-\s]+', '-', value)) +slugify = allow_lazy(slugify, unicode) Index: django/template/defaultfilters.py =================================================================== --- django/template/defaultfilters.py (revision 14551) +++ django/template/defaultfilters.py (working copy) @@ -221,10 +221,8 @@ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. """ - import unicodedata - value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') - value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) - return mark_safe(re.sub('[-\s]+', '-', value)) + from django.utils.html import slugify + return slugify(value) slugify.is_safe = True slugify = stringfilter(slugify) @@ -453,13 +451,8 @@ def removetags(value, tags): """Removes a space separated list of [X]HTML tags from the output.""" - tags = [re.escape(tag) for tag in tags.split()] - tags_re = u'(%s)' % u'|'.join(tags) - starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U) - endtag_re = re.compile(u'%s>' % tags_re) - value = starttag_re.sub(u'', value) - value = endtag_re.sub(u'', value) - return value + from django.utils.html import remove_tags + return remove_tags(value, tags) removetags.is_safe = True removetags = stringfilter(removetags) Index: tests/regressiontests/utils/html.py =================================================================== --- tests/regressiontests/utils/html.py (revision 14551) +++ tests/regressiontests/utils/html.py (working copy) @@ -109,3 +109,21 @@ ) for value, output in items: self.check_output(f, value, output) + + def test_slugify(self): + f = html.slugify + items = ( + (u'Hello, World!', 'hello-world'), + (u'spam & eggs', 'spam-eggs'), + ) + for value, output in items: + self.check_output(f, value, output) + + def test_remove_tags(self): + f = html.remove_tags + items = ( + ("Yes", "b i", "Yes"), + ("x
y
", "a b", "xy
"), + ) + for value, tags, output in items: + self.assertEquals(f(value, tags), output) \ No newline at end of file