Skip to content

Instantly share code, notes, and snippets.

@rmoch
Created February 26, 2012 10:40
Show Gist options
  • Save rmoch/1915957 to your computer and use it in GitHub Desktop.
Save rmoch/1915957 to your computer and use it in GitHub Desktop.
slugify
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