Created
February 26, 2012 10:40
-
-
Save rmoch/1915957 to your computer and use it in GitHub Desktop.
slugify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def asciify(unistr): | |
"""Returns an ascii string converting accented chars to normal ones. | |
Unconvertible chars are just removed. | |
>>> asciify(u'Ééüçñøà') | |
Eeucna | |
""" | |
return unicodedata.normalize('NFKD', unicode(unistr)).encode('ascii', 'ignore') | |
def slugify(value): | |
""" | |
Normalizes unicode string, converts to lowercase, removes non-alpha characters, | |
and converts spaces to hyphens. | |
""" | |
value = asciify(value) | |
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) | |
return mark_safe(re.sub('[-\s]+', '-', value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment