From 232e0b21697bd10f0c2bba1aa4113491a8770950 Mon Sep 17 00:00:00 2001 From: Florent Gallaire Date: Thu, 9 Nov 2017 04:50:31 +0100 Subject: [PATCH] preserve_case option for category names slugify (fix #2248) --- pelican/tests/test_importer.py | 4 ++-- pelican/tools/pelican_import.py | 4 ++-- pelican/utils.py | 6 ++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pelican/tests/test_importer.py b/pelican/tests/test_importer.py index 6af59212..2d976675 100644 --- a/pelican/tests/test_importer.py +++ b/pelican/tests/test_importer.py @@ -88,7 +88,7 @@ class TestWordpressXmlImporter(unittest.TestCase): index = 0 for post in test_posts: name = post[2] - category = slugify(post[5][0]) + category = slugify(post[5][0], preserve_case=True) name += '.md' filename = os.path.join(category, name) out_name = fnames[index] @@ -164,7 +164,7 @@ class TestWordpressXmlImporter(unittest.TestCase): for post in test_posts: name = post[2] kind = post[8] - category = slugify(post[5][0]) + category = slugify(post[5][0], preserve_case=True) name += '.md' filename = os.path.join(kind, category, name) out_name = fnames[index] diff --git a/pelican/tools/pelican_import.py b/pelican/tools/pelican_import.py index 25fc45e5..7dc73134 100755 --- a/pelican/tools/pelican_import.py +++ b/pelican/tools/pelican_import.py @@ -598,7 +598,7 @@ def get_out_filename(output_path, filename, ext, kind, typename = '' kind = 'article' if dircat and (len(categories) > 0): - catname = slugify(categories[0]) + catname = slugify(categories[0], preserve_case=True) else: catname = '' out_filename = os.path.join(output_path, typename, @@ -607,7 +607,7 @@ def get_out_filename(output_path, filename, ext, kind, os.makedirs(os.path.join(output_path, typename, catname)) # option to put files in directories with categories names elif dircat and (len(categories) > 0): - catname = slugify(categories[0]) + catname = slugify(categories[0], preserve_case=True) out_filename = os.path.join(output_path, catname, filename + ext) if not os.path.isdir(os.path.join(output_path, catname)): os.mkdir(os.path.join(output_path, catname)) diff --git a/pelican/utils.py b/pelican/utils.py index ef9da23b..0d621020 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -263,7 +263,7 @@ def pelican_open(filename, mode='rb', strip_crs=(sys.platform == 'win32')): yield content -def slugify(value, substitutions=()): +def slugify(value, substitutions=(), preserve_case=False): """ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. @@ -281,7 +281,9 @@ def slugify(value, substitutions=()): if isinstance(value, six.binary_type): value = value.decode('ascii') # still unicode - value = unicodedata.normalize('NFKD', value).lower() + value = unicodedata.normalize('NFKD', value) + if not preserve_case: + value = value.lower() # backward compatible covert from 2-tuples to 3-tuples new_subs = []