Skip to content

Commit f390f36

Browse files
committedJul 11, 2015
Merge pull request #1886 from getnikola/fix-1885
Fix #1885 -- always return unicode in slugify
2 parents 695c98b + 6de860b commit f390f36

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed
 

Diff for: ‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Always return unicode in slugify (Issue #1885)
78
* Remove logging handlers (Issue #1797)
89
* Add ``-d``, ``--detach`` option to ``nikola serve`` (Issue #1871)
910
* Use provided teaser format (``*_READ_MORE_LINK``) with custom teaser text

Diff for: ‎nikola/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,9 @@ def slugify(value, force=False):
729729
if USE_SLUGIFY or force:
730730
# This is the standard state of slugify, which actually does some work.
731731
# It is the preferred style, especially for Western languages.
732-
value = unidecode(value)
733-
value = str(_slugify_strip_re.sub('', value).strip().lower())
734-
return _slugify_hyphenate_re.sub('-', value)
732+
value = unicode_str(unidecode(value))
733+
value = _slugify_strip_re.sub('', value, re.UNICODE).strip().lower()
734+
return _slugify_hyphenate_re.sub('-', value, re.UNICODE)
735735
else:
736736
# This is the “disarmed” state of slugify, which lets the user
737737
# have any character they please (be it regular ASCII with spaces,
@@ -741,7 +741,7 @@ def slugify(value, force=False):
741741
# We still replace some characters, though. In particular, we need
742742
# to replace ? and #, which should not appear in URLs, and some
743743
# Windows-unsafe characters. This list might be even longer.
744-
rc = '/\\?#"\'\r\n\t*:<>|"'
744+
rc = '/\\?#"\'\r\n\t*:<>|'
745745

746746
for c in rc:
747747
value = value.replace(c, '-')

Diff for: ‎tests/test_slugify.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
3+
u"""Test slugify."""
4+
5+
from __future__ import unicode_literals
6+
import nikola.utils
7+
8+
9+
def test_ascii():
10+
"""Test an ASCII-only string."""
11+
o = nikola.utils.slugify(u'hello')
12+
assert o == u'hello'
13+
assert isinstance(o, nikola.utils.unicode_str)
14+
15+
16+
def test_ascii_dash():
17+
"""Test an ASCII string, with dashes."""
18+
o = nikola.utils.slugify(u'hello-world')
19+
assert o == u'hello-world'
20+
assert isinstance(o, nikola.utils.unicode_str)
21+
22+
23+
def test_ascii_fancy():
24+
"""Test an ASCII string, with fancy characters."""
25+
o = nikola.utils.slugify(u'The quick brown fox jumps over the lazy dog!-123.456')
26+
assert o == u'the-quick-brown-fox-jumps-over-the-lazy-dog-123456'
27+
assert isinstance(o, nikola.utils.unicode_str)
28+
29+
30+
def test_pl():
31+
"""Test a string with Polish diacritical characters."""
32+
o = nikola.utils.slugify(u'zażółćgęśląjaźń')
33+
assert o == u'zazolcgeslajazn'
34+
assert isinstance(o, nikola.utils.unicode_str)
35+
36+
37+
def test_pl_dash():
38+
"""Test a string with Polish diacritical characters and dashes."""
39+
o = nikola.utils.slugify(u'zażółć-gęślą-jaźń')
40+
assert o == u'zazolc-gesla-jazn'
41+
42+
43+
def test_pl_fancy():
44+
"""Test a string with Polish diacritical characters and fancy characters."""
45+
o = nikola.utils.slugify(u'Zażółć gęślą jaźń!-123.456')
46+
assert o == u'zazolc-gesla-jazn-123456'
47+
assert isinstance(o, nikola.utils.unicode_str)
48+
49+
50+
def test_disarmed():
51+
"""Test disarmed slugify."""
52+
nikola.utils.USE_SLUGIFY = False
53+
o = nikola.utils.slugify(u'Zażółć gęślą jaźń!-123.456')
54+
assert o == u'Zażółć gęślą jaźń!-123.456'
55+
assert isinstance(o, nikola.utils.unicode_str)
56+
nikola.utils.USE_SLUGIFY = True
57+
58+
59+
def test_disarmed_weird():
60+
"""Test disarmed slugify with banned characters."""
61+
nikola.utils.USE_SLUGIFY = False
62+
o = nikola.utils.slugify(u'Zażółć gęślą jaźń!-123.456 "Hello World"?#H<e>l/l\\o:W\'o\rr*l\td|!\n')
63+
assert o == u'Zażółć gęślą jaźń!-123.456 -Hello World---H-e-l-l-o-W-o-r-l-d-!-'
64+
assert isinstance(o, nikola.utils.unicode_str)
65+
nikola.utils.USE_SLUGIFY = True

0 commit comments

Comments
 (0)
Please sign in to comment.