Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Djang slugify a string to Twitter hashtag format
import re
import unicodedata
from django.utils.encoding import smart_unicode as smart_text
except ImportError:
from django.utils.encoding import smart_text
SLUG_OK = '_'
def slug_twitter(s, ok=SLUG_OK, lower=True, spaces=False):
Slugify a string to Twitter hashtag format
rv = []
for c in unicodedata.normalize('NFKC', smart_text(s)):
cat = unicodedata.category(c)[0]
if cat in 'LN' or c in ok:
if cat == 'Z': # space
rv.append(' ')
new = ''.join(rv).strip()
if not spaces:
new = re.sub('[-\s]+', '_', new)
return new.lower() if lower else new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment