Allow text substitutions when generating slugs

The `slugify()` function used by Pelican is in general very good at
coming up with something both readable and URL-safe. However, there are
a few specific cases where it causes conflicts. One that I've run into
is using the strings `C++` and `C` as tags, both of which transform to
the slug `c`. This commit adds an optional `SLUG_SUBSTITUTIONS` setting
which is a list of 2-tuples of substitutions to be carried out
case-insensitively just prior to stripping out non-alphanumeric
characters. This allows cases like `C++` to be transformed to `CPP` or
similar. This can also improve the readability of slugs.
This commit is contained in:
Andy Pearce 2013-06-14 15:54:06 +01:00
commit 39518e15ef
6 changed files with 28 additions and 8 deletions

View file

@ -15,10 +15,10 @@ class URLWrapper(object):
def __init__(self, name, settings):
# next 2 lines are redundant with the setter of the name property
# but are here for clarity
self._name = name
self.slug = slugify(name)
self.name = name
self.settings = settings
self._name = name
self.slug = slugify(name, self.settings.get('SLUG_SUBSTITUTIONS', ()))
self.name = name
@property
def name(self):
@ -27,7 +27,7 @@ class URLWrapper(object):
@name.setter
def name(self, name):
self._name = name
self.slug = slugify(name)
self.slug = slugify(name, self.settings.get('SLUG_SUBSTITUTIONS', ()))
def as_dict(self):
d = self.__dict__
@ -41,7 +41,8 @@ class URLWrapper(object):
return self.slug
def _normalize_key(self, key):
return six.text_type(slugify(key))
subs = self.settings.get('SLUG_SUBSTITUTIONS', ())
return six.text_type(slugify(key, subs))
def __eq__(self, other):
return self._key() == self._normalize_key(other)