diff options
| author | Val Neekman <un33kvu@gmail.com> | 2019-01-03 18:03:36 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-03 18:03:36 -0500 |
| commit | 1340320455f7201117ac3970c0facf5d1e0f8767 (patch) | |
| tree | 72f41588ccc9ae7281220f12fba1f6aff2d5801f | |
| parent | 683364ea0f4637364b84486ef24124ece0e0597c (diff) | |
| download | python-slugify-2.0.1.tar.gz | |
Add replacements option (#67)2.0.1
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | slugify/__init__.py | 2 | ||||
| -rw-r--r-- | slugify/slugify.py | 14 | ||||
| -rw-r--r-- | test.py | 9 |
5 files changed, 30 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index cd80bf1..e2eed12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.0.1 + - Add replacements option e.g. [['|', 'or'], ['%', 'percent'], ['-', '_']] (@andriyor) + ## 2.0.0 - Fix alternative dependency installation @@ -98,6 +98,10 @@ How to use r = slugify(txt, separator='_', regex_pattern=regex_pattern) self.assertNotEqual(r, "_this_is_a_test_") + txt = '10 | 20 %' + r = slugify(txt, replacements=[['|', 'or'], ['%', 'percent']]) + self.assertEqual(r, "10-or-20-percent") + ``` For more examples, have a look at the [test.py](test.py) file. diff --git a/slugify/__init__.py b/slugify/__init__.py index 81849b9..7358b99 100644 --- a/slugify/__init__.py +++ b/slugify/__init__.py @@ -3,4 +3,4 @@ from .slugify import * __author__ = 'Val Neekman @ Neekware Inc. [@vneekman]' __description__ = 'A Python slugify application that also handles Unicode' -__version__ = '2.0.0' +__version__ = '2.0.1' diff --git a/slugify/slugify.py b/slugify/slugify.py index ccd33a8..59e9672 100644 --- a/slugify/slugify.py +++ b/slugify/slugify.py @@ -75,7 +75,8 @@ def smart_truncate(string, max_length=0, word_boundary=False, separator=' ', sav def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, - separator=DEFAULT_SEPARATOR, save_order=False, stopwords=(), regex_pattern=None, lowercase=True): + separator=DEFAULT_SEPARATOR, save_order=False, stopwords=(), regex_pattern=None, lowercase=True, + replacements=()): """ Make a slug from the given text. :param text (str): initial text @@ -89,9 +90,15 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w :param stopwords (iterable): words to discount :param regex_pattern (str): regex pattern for allowed characters :param lowercase (bool): activate case sensitivity by setting it to False + :param replacements (iterable): list of replacement rules e.g. [['|', 'or'], ['%', 'percent']] :return (str): """ + # user-specific replacements + if replacements: + for old, new in replacements: + text = text.replace(old, new) + # ensure text is unicode if not isinstance(text, _unicode_type): text = _unicode(text, 'utf-8', 'ignore') @@ -158,6 +165,11 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w words = [w for w in text.split(DEFAULT_SEPARATOR) if w not in stopwords] text = DEFAULT_SEPARATOR.join(words) + # finalize user-specific replacements + if replacements: + for old, new in replacements: + text = text.replace(old, new) + # smart truncate if requested if max_length > 0: text = smart_truncate(text, max_length, word_boundary, DEFAULT_SEPARATOR, save_order) @@ -189,6 +189,15 @@ class TestSlugification(unittest.TestCase): r = slugify(txt, separator='_', regex_pattern=regex_pattern) self.assertNotEqual(r, "_this_is_a_test_") + def test_replacements(self): + txt = '10 | 20 %' + r = slugify(txt, replacements=[['|', 'or'], ['%', 'percent']]) + self.assertEqual(r, "10-or-20-percent") + + txt = 'I ♥ 🦄' + r = slugify(txt, replacements=[['♥', 'amour'], ['🦄', 'licorne']]) + self.assertEqual(r, "i-amour-licorne") + class TestUtils(unittest.TestCase): |
