Skip to content

Instantly share code, notes, and snippets.

@jperras
Created July 10, 2013 15:34
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 jperras/5967327 to your computer and use it in GitHub Desktop.
Save jperras/5967327 to your computer and use it in GitHub Desktop.
Generate an ASCII-only slug based off of a unicode string.
from unidecode import unidecode
# Regular expression to match most "punctuation"
punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
def slugify(text, delimiter=u'-'):
"""Generates an ASCII-only slug."""
result = []
for word in punctuation_re.split(text.lower()):
if isinstance(word, unicode):
word = unidecode(word)
result.extend(word.split())
return unicode(delimiter.join(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment