Skip to content

Instantly share code, notes, and snippets.

@imom0
Created October 14, 2012 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imom0/3887669 to your computer and use it in GitHub Desktop.
Save imom0/3887669 to your computer and use it in GitHub Desktop.
slugify
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
Took from django sources.
"""
value = Markup(value).striptags()
if type(value) == unicode:
import unicodedata
from unidecode import unidecode
value = unicode(unidecode(value))
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
return re.sub('[-\s]+', '-', value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment