Skip to content

Instantly share code, notes, and snippets.

@atelic
Created February 1, 2016 15:19
Show Gist options
  • Save atelic/b11b3f5086027ae08870 to your computer and use it in GitHub Desktop.
Save atelic/b11b3f5086027ae08870 to your computer and use it in GitHub Desktop.
import re
import unicodedata
def slugify(value):
"""
Convert spaces to hyphens., remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
From Django's filter: https://github.com/django/django/blob/60586dd7379b295b72d8af4e03423c286913b5e8/django/utils/text.py#L415
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = 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