Skip to content

Instantly share code, notes, and snippets.

@gergelypolonkai
Created December 8, 2016 14:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gergelypolonkai/1866fd363f75f4da5f86103952e387f6 to your computer and use it in GitHub Desktop.
Save gergelypolonkai/1866fd363f75f4da5f86103952e387f6 to your computer and use it in GitHub Desktop.
Slugify in Python 3
import re
from unicodedata import normalize
_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
def slugify(text, delim='-'):
"""
Generate an ASCII-only slug.
"""
result = []
for word in _punctuation_re.split(text.lower()):
word = normalize('NFKD', word) \
.encode('ascii', 'ignore') \
.decode('utf-8')
if word:
result.append(word)
return delim.join(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment