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 !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')


@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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.