Skip to content

Commit

Permalink
Fix #1885 -- always return unicode in slugify
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jul 11, 2015
1 parent c8d97e2 commit 50f92ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions nikola/utils.py
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions tests/test_slugify.py
@@ -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.