Skip to content
Permalink
Browse files

Fix #1885 -- always return unicode in slugify

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 11, 2015
1 parent c8d97e2 commit 50f92addd3fb5faa3edce8492b7af15fffc03305
Showing with 40 additions and 3 deletions.
  1. +1 −0 CHANGES.txt
  2. +3 −3 nikola/utils.py
  3. +36 −0 tests/test_slugify.py
@@ -4,6 +4,7 @@ New in master
Features
--------

* Always return unicode in slugify (Issue #1885)
* Remove logging handlers (Issue #1797)
* Add ``-d``, ``--detach`` option to ``nikola serve`` (Issue #1871)
* Use provided teaser format (``*_READ_MORE_LINK``) with custom teaser text
@@ -728,9 +728,9 @@ def slugify(value, force=False):
if USE_SLUGIFY or force:
# This is the standard state of slugify, which actually does some work.
# It is the preferred style, especially for Western languages.
value = unidecode(value)
value = str(_slugify_strip_re.sub('', value).strip().lower())
return _slugify_hyphenate_re.sub('-', value)
value = unicode_str(unidecode(value))
value = _slugify_strip_re.sub('', value, re.UNICODE).strip().lower()
return _slugify_hyphenate_re.sub('-', value, re.UNICODE)
else:
# This is the “disarmed” state of slugify, which lets the user
# have any character they please (be it regular ASCII with spaces,
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import nikola.utils

def test_ascii():
o = nikola.utils.slugify(u'abcdef')
assert o == u'abcdef'
assert isinstance(o, nikola.utils.unicode_str)

def test_ascii_dash():
o = nikola.utils.slugify(u'abc-def')
assert o == u'abc-def'
assert isinstance(o, nikola.utils.unicode_str)

def test_pl():
o = nikola.utils.slugify(u'ąbćdef')
assert o == u'abcdef'
assert isinstance(o, nikola.utils.unicode_str)

def test_pl_dash():
o = nikola.utils.slugify(u'ąbć-def')
assert o == u'abc-def'
assert isinstance(o, nikola.utils.unicode_str)

def test_disarmed():
nikola.utils.USE_SLUGIFY = False
o = nikola.utils.slugify(u'ąbć-def')
assert o == u'ąbć-def'
assert isinstance(o, nikola.utils.unicode_str)
nikola.utils.USE_SLUGIFY = True

def test_disarmed_weird():
nikola.utils.USE_SLUGIFY = False
o = nikola.utils.slugify(u'ąbć-def "Hello World"?#H<e>l/l\\o:W\'o\rr*l\td|!\n')
assert o == u'ąbć-def -Hello World---H-e-l-l-o-W-o-r-l-d-!-'
assert isinstance(o, nikola.utils.unicode_str)
nikola.utils.USE_SLUGIFY = True

0 comments on commit 50f92ad

Please sign in to comment.
You can’t perform that action at this time.