Issue #2 new

Why is slug generation not included?

Bryson Holland
created an issue

As the core package includes numerous other helpers, why not include a slug generation module as well? It seems that it would be complimentary to the slug validator, in that you can either create your own slugs and validate them using wheezy's validator, or generate valid ones using an included method.

Comments (2)

  1. Bryson Holland reporter

    Perhaps a helper in wheezy.routing would be more appropriate than a helper in wheezy.core but the inclusion of slug generation seems logical to me either way.

  2. Andriy Kornatskyy repo owner

    There are various way how to transliterate unicode to acsii character set... you can do that with builtin unicodedata.normalize but that doesn't cover everything or rely on 3rd party packages like translitcodec or unidecode.

    import sys
    import re
    from unicodedata import normalize
    if sys.version_info[0] >= 3:
        u = lambda s: s
        u = lambda s: unicode(s, "unicode_escape")
    RE_SLUG_STRIP = re.compile('[^\w\s-]+')
    RE_SLUG_REPLACE = re.compile('\s+')
    def slug(value):
            >>> str(slug(u(' Abc ! ')))
            >>> str(slug(u(' A ~ b < c > ')))
            >>> str(slug(u('pr\xf6va')))
            >>> str(slug(u('\u0442\u0435\u0441\u0442')))
        value = normalize('NFKD', value).encode(
            'ascii', 'ignore').decode('ascii').lower()
        return RE_SLUG_REPLACE.sub('-', RE_SLUG_STRIP.sub('', value).strip())
    if __name__ == "__main__":
        import doctest

    Transliterate before the call:

    >>> from unidecode import unidecode
    >>> str(slug(unidecode(u('\u0442\u0435\u0441\u0442')).decode('ascii')))
  3. Log in to comment