Created
July 10, 2013 15:34
-
-
Save jperras/5967327 to your computer and use it in GitHub Desktop.
Generate an ASCII-only slug based off of a unicode string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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