From 3c35d5ea5801f2a7d806449c681bbedc11c8c3e1 Mon Sep 17 00:00:00 2001 From: micw Date: Wed, 12 Apr 2017 04:51:07 +0200 Subject: [PATCH] Fix/slugify with german umlaut ss (#7029) * more tests for slugify * Fix german umlauts in slugify * Update __init__.py --- homeassistant/util/__init__.py | 11 +++++++++-- tests/util/test_init.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/util/__init__.py b/homeassistant/util/__init__.py index 6d77f67161d..1186892b512 100644 --- a/homeassistant/util/__init__.py +++ b/homeassistant/util/__init__.py @@ -22,6 +22,9 @@ U = TypeVar('U') RE_SANITIZE_FILENAME = re.compile(r'(~|\.\.|/|\\)') RE_SANITIZE_PATH = re.compile(r'(~|\.(\.)+)') RE_SLUGIFY = re.compile(r'[^a-z0-9_]+') +TBL_SLUGIFY = { + ord('ß'): 'ss' +} def sanitize_filename(filename: str) -> str: @@ -36,9 +39,13 @@ def sanitize_path(path: str) -> str: def slugify(text: str) -> str: """Slugify a given text.""" - text = normalize('NFKD', text).lower().replace(" ", "_") + text = normalize('NFKD', text) + text = text.lower() + text = text.replace(" ", "_") + text = text.translate(TBL_SLUGIFY) + text = RE_SLUGIFY.sub("", text) - return RE_SLUGIFY.sub("", text) + return text def repr_helper(inp: Any) -> str: diff --git a/tests/util/test_init.py b/tests/util/test_init.py index 3ccb72920d4..ba8415d597f 100644 --- a/tests/util/test_init.py +++ b/tests/util/test_init.py @@ -37,6 +37,7 @@ class TestUtil(unittest.TestCase): util.slugify("greg_phone - exp_wayp1")) self.assertEqual("we_are_we_are_a_test_calendar", util.slugify("We are, we are, a... Test Calendar")) + self.assertEqual("test_aouss_aou", util.slugify("Tèst_äöüß_ÄÖÜ")) def test_repr_helper(self): """Test repr_helper."""