Ticket #14516: 14516.diff

File 14516.diff, 3.3 KB (added by ik, 13 years ago)
  • django/utils/html.py

     
    165165    text = trailing_empty_content_re.sub('', text)
    166166    return text
    167167clean_html = allow_lazy(clean_html, unicode)
     168
     169def 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
     178remove_tags = allow_lazy(remove_tags, unicode)
     179
     180def 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))
     189slugify = allow_lazy(slugify, unicode)
  • django/template/defaultfilters.py

     
    221221    Normalizes string, converts to lowercase, removes non-alpha characters,
    222222    and converts spaces to hyphens.
    223223    """
    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)
    228226slugify.is_safe = True
    229227slugify = stringfilter(slugify)
    230228
     
    453451
    454452def removetags(value, tags):
    455453    """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)
    463456removetags.is_safe = True
    464457removetags = stringfilter(removetags)
    465458
  • tests/regressiontests/utils/html.py

     
    109109        )
    110110        for value, output in items:
    111111            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
Back to Top