summaryrefslogtreecommitdiffstats
path: root/askbot/utils/slug.py
blob: 1c8010c82e69d3d38d216d5c55fccf70173fd979 (plain)
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
27
28
29
30
31
32
33
"""defines the method for building slugs
slugification may be disabled with a live setting "USE_ASCII_ONLY_SLUGS"

the setting was added just in case - if people actually
want to see unicode characters in the slug. If this is the choice
slug will be simply equal to the input text
"""
from unidecode import unidecode
from django.template import defaultfilters
from django.conf import settings
import re

def slugify(input_text, max_length=50):
    """custom slugify function that
    removes diacritic modifiers from the characters
    """
    if getattr(settings, 'ALLOW_UNICODE_SLUGS', False) == False:
        if input_text == '':
            return input_text
        slug = defaultfilters.slugify(unidecode(input_text))
        while len(slug) > max_length:
            # try to shorten word by word until len(slug) <= max_length
            temp = slug[:slug.rfind('-')]
            if len(temp) > 0:
                slug = temp
            else:
                #we have nothing left, do not apply the last crop,
                #apply the cut-off directly
                slug = slug[:max_length]
                break
        return slug
    else:
        return re.sub(r'\s+', '-', input_text.strip().lower())