Skip to content

Commit

Permalink
adding a unicode-friendly slugify
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Balogh committed Jul 15, 2010
1 parent dea2580 commit e59a7de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions apps/amo/tests/test_utils.py
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from nose.tools import eq_

from amo.utils import slugify


def test_slugify():
check = lambda x, y: eq_(slugify(x), y)
s = [('xx x - "#$@ x', 'xx-x-x'),
(u'Bän...g (bang)', u'bäng-bang')]
for val, expected in s:
yield check, val, expected
9 changes: 9 additions & 0 deletions apps/amo/utils.py
@@ -1,6 +1,7 @@
import itertools
import operator
import random
import re
import time
import urllib
import urlparse
Expand Down Expand Up @@ -194,3 +195,11 @@ def randslice(qs, limit, exclude=None):
if exclude is not None:
slice_ = [o for o in slice_ if o.pk != exclude][:limit - 1]
return slice_


slug_re = re.compile('[^\w\s-]', re.UNICODE)


def slugify(s):
s = slug_re.sub('', unicode(s)).strip().lower()
return re.sub('[-\s]+', '-', s)

0 comments on commit e59a7de

Please sign in to comment.