From c255a35800911f74a3d07689cc6f1296cb6660a9 Mon Sep 17 00:00:00 2001 From: Andrea Corbellini Date: Tue, 22 Sep 2015 20:52:30 +0200 Subject: [PATCH] Use unichr() instead of chr() with Python 2. --- pelican/tests/test_utils.py | 12 ++++++++++++ pelican/utils.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pelican/tests/test_utils.py b/pelican/tests/test_utils.py index a076a2c7..d967b247 100644 --- a/pelican/tests/test_utils.py +++ b/pelican/tests/test_utils.py @@ -190,6 +190,18 @@ class TestUtils(LoggedTestCase): self.assertEqual( utils.truncate_html_words("cafetiére " * 100, 20), "cafetiére " * 20 + '...') + self.assertEqual( + utils.truncate_html_words("∫dx " * 100, 20), + "∫dx " * 20 + '...') + + # Words with HTML character references inside and outside + # the ASCII range. + self.assertEqual( + utils.truncate_html_words("é " * 100, 20), + "é " * 20 + '...') + self.assertEqual( + utils.truncate_html_words("∫dx " * 100, 20), + "∫dx " * 20 + '...') def test_process_translations(self): # create a bunch of articles diff --git a/pelican/utils.py b/pelican/utils.py index 7ad0914c..697a182b 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -499,14 +499,14 @@ class _HTMLWordTruncator(HTMLParser): except KeyError: self.handle_ref('') else: - self.handle_ref(chr(codepoint)) + self.handle_ref(six.unichr(codepoint)) def handle_charref(self, name): if name.startswith('x'): codepoint = int(name[1:], 16) else: codepoint = int(name) - self.handle_ref(chr(codepoint)) + self.handle_ref(six.unichr(codepoint)) def truncate_html_words(s, num, end_text='...'):