From 402a099e4e5c218f5054e354a24d2c3e8c81104c Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Sun, 10 Jul 2016 13:58:30 +0000 Subject: [PATCH] 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 --- ietf/utils/templatetags/textfilters.py | 18 ++++++++++++++++++ ietf/utils/text.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 ietf/utils/templatetags/textfilters.py create mode 100644 ietf/utils/text.py diff --git a/ietf/utils/templatetags/textfilters.py b/ietf/utils/templatetags/textfilters.py new file mode 100644 index 000000000..aa60db362 --- /dev/null +++ b/ietf/utils/templatetags/textfilters.py @@ -0,0 +1,18 @@ +from __future__ import unicode_literals + +from django.template.base import Library +from django.template.defaultfilters import stringfilter + +from ietf.utils.text import xslugify as _xslugify + +register = Library() + +@register.filter(is_safe=True) +@stringfilter +def xslugify(value): + """ + Converts to ASCII. Converts spaces to hyphens. Removes characters that + aren't alphanumerics, underscores, slashes, or hyphens. Converts to + lowercase. Also strips leading and trailing whitespace. + """ + return _xslugify(value) diff --git a/ietf/utils/text.py b/ietf/utils/text.py new file mode 100644 index 000000000..50811af37 --- /dev/null +++ b/ietf/utils/text.py @@ -0,0 +1,20 @@ +from __future__ import unicode_literals + +import re +import unicodedata + +from django.utils.functional import allow_lazy +from django.utils import six +from django.utils.safestring import mark_safe + +def xslugify(value): + """ + Converts to ASCII. Converts spaces to hyphens. Removes characters that + aren't alphanumerics, underscores, slash, or hyphens. Converts to + lowercase. Also strips leading and trailing whitespace. + (I.e., does the same as slugify, but also converts slashes to dashes.) + """ + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') + value = re.sub('[^\w\s/-]', '', value).strip().lower() + return mark_safe(re.sub('[-\s/]+', '-', value)) +xslugify = allow_lazy(xslugify, six.text_type)