Created
October 14, 2012 06:55
-
-
Save imom0/3887669 to your computer and use it in GitHub Desktop.
slugify
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
def slugify(value): | |
""" | |
Normalizes string, converts to lowercase, removes non-alpha characters, | |
and converts spaces to hyphens. | |
Took from django sources. | |
""" | |
value = Markup(value).striptags() | |
if type(value) == unicode: | |
import unicodedata | |
from unidecode import unidecode | |
value = unicode(unidecode(value)) | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') | |
value = unicode(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