summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--README.md4
-rw-r--r--slugify/__init__.py2
-rw-r--r--slugify/slugify.py14
-rw-r--r--test.py9
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
diff --git a/README.md b/README.md
index d58daee..bc4d9e8 100644
--- a/README.md
+++ b/README.md
@@ -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)
diff --git a/test.py b/test.py
index 9ff9ec0..78b1956 100644
--- a/test.py
+++ b/test.py
@@ -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):