Skip to content

Instantly share code, notes, and snippets.

@b1
Created May 7, 2014 15:01
Show Gist options
  • Save b1/3c541c5157617204dea1 to your computer and use it in GitHub Desktop.
Save b1/3c541c5157617204dea1 to your computer and use it in GitHub Desktop.
slugify
#!/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