Created
May 7, 2014 15:01
-
-
Save b1/3c541c5157617204dea1 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
#!/bin/env python2 | |
# encoding: utf-8 | |
import re | |
try: | |
from unidecode import unidecode | |
except ImportError: | |
Exception("pip install unidecode") | |
def slugify(s, maxlen=255): | |
s = s.lower() | |
s = unidecode(s) | |
s = s.replace('/', '-') | |
s = re.sub(r'[^a-z0-9- ]+', ' ', s) | |
s = re.sub(r'\s+', '-', s) | |
s = re.sub(r'-+', '-', s) | |
s = s.strip('-') | |
return s[:maxlen] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment