From 33bf07efc82c00b811df02bca8c5862f95db3294 Mon Sep 17 00:00:00 2001 From: Val Neekman Date: Thu, 10 Oct 2019 18:12:07 -0400 Subject: [PATCH] add special pre translation file, more unit test, updated readme --- CHANGELOG.md | 7 +++++++ README.md | 4 ++++ slugify/__init__.py | 3 ++- slugify/special.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ test.py | 5 +++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 slugify/special.py diff --git a/CHANGELOG.md b/CHANGELOG.md index cc98d31..0e1a514 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,14 @@ +## 3.0.5 + - Add test for pre-translation (e.g German Umlaut) + - Add special char supports (optional Use) + ## 3.0.4 - Now supporting text-unidecode>=1.3 - Now supporting Unidecode>=1.1.1 +## 3.0.3 + - Remove unicode chars from file + ## 3.0.2 - Add official support of Py 3.7 diff --git a/README.md b/README.md index 20f8c47..800b7a6 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,10 @@ txt = '10 | 20 %' r = slugify(txt, replacements=[['|', 'or'], ['%', 'percent']]) self.assertEqual(r, "10-or-20-percent") +txt = 'ÜBER Über German Umlaut' +r = slugify(txt, replacements=[['Ü', 'UE'], ['ü', 'ue']]) +self.assertEqual(r, "ueber-ueber-german-umlaut") + ``` For more examples, have a look at the [test.py](test.py) file. diff --git a/slugify/__init__.py b/slugify/__init__.py index 52a07b4..0c86a4c 100644 --- a/slugify/__init__.py +++ b/slugify/__init__.py @@ -1,6 +1,7 @@ +from .special import * from .slugify import * __author__ = 'Val Neekman @ Neekware Inc. [@vneekman]' __description__ = 'A Python slugify application that also handles Unicode' -__version__ = '3.0.4' +__version__ = '3.0.5' diff --git a/slugify/special.py b/slugify/special.py new file mode 100644 index 0000000..767541a --- /dev/null +++ b/slugify/special.py @@ -0,0 +1,44 @@ +def add_uppercase_char(char_list): + """ Given a replacement char list, this adds uppercase chars to the list """ + + for item in char_list: + char, xlate = item + upper_dict = char.upper(), xlate.capitalize() + if upper_dict not in char_list and char != upper_dict[0]: + char_list.insert(0, upper_dict) + return char_list + + +# Language specific pre translations +# Source awesome-slugify + +_CYRILLIC = [ # package defaults: + (u'ё', u'e'), # io / yo + (u'я', u'ya'), # ia + (u'х', u'h'), # kh + (u'у', u'y'), # u + (u'щ', u'sch'), # shch + (u'ю', u'u'), # iu / yu +] +CYRILLIC = add_uppercase_char(_CYRILLIC) + +_GERMAN = [ # package defaults: + (u'ä', u'ae'), # a + (u'ö', u'oe'), # o + (u'ü', u'ue'), # u +] +GERMAN = add_uppercase_char(_GERMAN) + +_GREEK = [ # package defaults: + (u'χ', u'ch'), # kh + (u'Ξ', u'X'), # Ks + (u'ϒ', u'Y'), # U + (u'υ', u'y'), # u + (u'ύ', u'y'), + (u'ϋ', u'y'), + (u'ΰ', u'y'), +] +GREEK = add_uppercase_char(_GREEK) + +# Pre translations +PRE_TRANSLATIONS = CYRILLIC + GERMAN + GREEK diff --git a/test.py b/test.py index e1efe38..29f0ac7 100644 --- a/test.py +++ b/test.py @@ -223,6 +223,11 @@ def test_replacements(self): r = slugify(txt, replacements=[['♥', 'amour'], ['🦄', 'licorne']]) self.assertEqual(r, "i-amour-licorne") + def test_replacements_german_umlaut_custom(self): + txt = 'ÜBER Über German Umlaut' + r = slugify(txt, replacements=[['Ü', 'UE'], ['ü', 'ue']]) + self.assertEqual(r, "ueber-ueber-german-umlaut") + class TestUtils(unittest.TestCase):