Login

slugify with transliteration

Author:
jezdez
Posted:
November 26, 2010
Language:
Python
Version:
Not specified
Score:
1 (after 1 ratings)

This slugify correctly transliterates special characters using the translitcodec package from PyPI.

Make sure you've installed http://pypi.python.org/pypi/translitcodec/ before using this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import re
import translitcodec # needed to install it

from django.template import Library
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe

register = Library()

PUNCT_RE = re.compile(r'[\t !"#$%&\'()*\-/<=>[email protected]\[\\\]^_`{|},.]+')


@stringfilter
def slugify(value, delim=u'-'):
    """
    Generates an ASCII-only slug.
    
    Borrowed from http://flask.pocoo.org/snippets/5/
    """
    result = []
    for word in PUNCT_RE.split(value.lower()):
        word = word.encode('translit/long')
        if word:
            result.append(word)
    return mark_safe(unicode(delim.join(result)))
slugify.is_safe = True

More like this

  1. Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month, 1 week ago
  2. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 1 week ago
  3. codigo alto nivel by MrRocklion 4 months ago
  4. Load template from specific app by Krzysiek555 4 months, 3 weeks ago
  5. PostgreSQL JSON subqueries by dolamroth 4 months, 3 weeks ago

Comments

Please login first before commenting.