Created
December 8, 2016 14:00
-
-
Save gergelypolonkai/1866fd363f75f4da5f86103952e387f6 to your computer and use it in GitHub Desktop.
Slugify in Python 3
This file contains 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
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