Skip to content

Commit 402a099

Browse files
committed
Added a modified slugify function and template filter, which converts slashes to dashes instead of eliding them. This is necessary in order to be able to distinguish the slugified room names like 'Schinkel I/II' from 'Schinkel III'.
- Legacy-Id: 11589
1 parent bc54874 commit 402a099

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import unicode_literals
2+
3+
from django.template.base import Library
4+
from django.template.defaultfilters import stringfilter
5+
6+
from ietf.utils.text import xslugify as _xslugify
7+
8+
register = Library()
9+
10+
@register.filter(is_safe=True)
11+
@stringfilter
12+
def xslugify(value):
13+
"""
14+
Converts to ASCII. Converts spaces to hyphens. Removes characters that
15+
aren't alphanumerics, underscores, slashes, or hyphens. Converts to
16+
lowercase. Also strips leading and trailing whitespace.
17+
"""
18+
return _xslugify(value)

ietf/utils/text.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import unicode_literals
2+
3+
import re
4+
import unicodedata
5+
6+
from django.utils.functional import allow_lazy
7+
from django.utils import six
8+
from django.utils.safestring import mark_safe
9+
10+
def xslugify(value):
11+
"""
12+
Converts to ASCII. Converts spaces to hyphens. Removes characters that
13+
aren't alphanumerics, underscores, slash, or hyphens. Converts to
14+
lowercase. Also strips leading and trailing whitespace.
15+
(I.e., does the same as slugify, but also converts slashes to dashes.)
16+
"""
17+
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
18+
value = re.sub('[^\w\s/-]', '', value).strip().lower()
19+
return mark_safe(re.sub('[-\s/]+', '-', value))
20+
xslugify = allow_lazy(xslugify, six.text_type)

0 commit comments

Comments
 (0)