summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVal Neekman <val@neekware.com>2018-09-02 12:36:52 -0400
committerVal Neekman <val@neekware.com>2018-09-02 12:36:52 -0400
commitbeca50eaff20ca20652df782354a8f184c1eaf4c (patch)
treef0afd8a85c1069d5d1f1922cdf5fb45bd0c93841
parent4c1a344652dfef32f786a96d94b351f4277da964 (diff)
downloadpython-slugify-beca50eaff20ca20652df782354a8f184c1eaf4c.tar.gz
release 1.2.6, case sensitive slug support1.2.6
-rw-r--r--.vscode/settings.json3
-rw-r--r--CHANGELOG.md3
-rw-r--r--README.rst9
-rwxr-xr-xpycodestyle.sh4
-rw-r--r--slugify/__init__.py2
-rw-r--r--slugify/slugify.py21
-rw-r--r--test.py5
7 files changed, 38 insertions, 9 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..20d15cb
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+ "python.linting.pylintEnabled": false
+} \ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3ccebfe..84f3436 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,6 @@
+## 1.2.6
+ - Add support for case sensitive slugs (@s-m-e)
+
## 1.2.5
- Add support for using text-unidecode (@bolkedebruin)
- Switch to pycodestyle instead of pep8
diff --git a/README.rst b/README.rst
index 9604965..dbdeb52 100644
--- a/README.rst
+++ b/README.rst
@@ -162,6 +162,10 @@ How to use
r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')
+ txt = 'thIs Has a stopword Stopword'
+ r = slugify(txt, stopwords=['Stopword'], lowercase=False)
+ self.assertEqual(r, 'thIs-Has-a-stopword')
+
txt = 'foo &amp; bar'
r = slugify(txt)
self.assertEqual(r, 'foo-bar')
@@ -176,6 +180,8 @@ How to use
r = slugify(txt, separator='_', regex_pattern=regex_pattern)
self.assertNotEqual(r, "_this_is_a_test_")
+For more examples, have a look at the (`TEST`_) file.
+
Running the tests
-----------------
@@ -189,7 +195,7 @@ To run the tests against the current environment:
License
-------
-Released under a (`MIT`_) license.
+Released under a (`TEST`_) license.
**Note:**
@@ -222,3 +228,4 @@ X.Y.Z Version
.. _MIT: https://github.com/un33k/python-slugify/blob/master/LICENSE
+.. _TEST: https://github.com/un33k/python-slugify/blob/master/test.py
diff --git a/pycodestyle.sh b/pycodestyle.sh
index ab6766e..cd6122d 100755
--- a/pycodestyle.sh
+++ b/pycodestyle.sh
@@ -7,5 +7,7 @@
# -- E261 at least two spaces before inline comment
# -- E225 missing whitespace around operator
# -- E501 line too long
+# Ignoring warning codes
+# -- W605 invalid escape sequence '\d'
-pycodestyle --ignore=E128,E261,E225,E501 slugify test.py setup.py
+pycodestyle --ignore=E128,E261,E225,E501,W605 slugify test.py setup.py
diff --git a/slugify/__init__.py b/slugify/__init__.py
index 02393a1..1a02a3e 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__ = '1.2.5'
+__version__ = '1.2.6'
diff --git a/slugify/slugify.py b/slugify/slugify.py
index 99afb7f..192bbd3 100644
--- a/slugify/slugify.py
+++ b/slugify/slugify.py
@@ -26,6 +26,7 @@ DECIMAL_PATTERN = re.compile('&#(\d+);')
HEX_PATTERN = re.compile('&#x([\da-fA-F]+);')
QUOTE_PATTERN = re.compile(r'[\']+')
ALLOWED_CHARS_PATTERN = re.compile(r'[^-a-z0-9]+')
+ALLOWED_CHARS_PATTERN_WITH_UPPERCASE = re.compile(r'[^-a-zA-Z0-9]+')
DUPLICATE_DASH_PATTERN = re.compile('-{2,}')
NUMBERS_PATTERN = re.compile('(?<=\d),(?=\d)')
DEFAULT_SEPARATOR = '-'
@@ -74,7 +75,7 @@ def smart_truncate(string, max_length=0, word_boundaries=False, separator=' ', s
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):
+ separator=DEFAULT_SEPARATOR, save_order=False, stopwords=(), regex_pattern=None, lowercase=True):
"""
Make a slug from the given text.
:param text (str): initial text
@@ -87,6 +88,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
:param separator (str): separator between words
: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
:return (str):
"""
@@ -127,8 +129,9 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
if sys.version_info < (3,):
text = text.encode('ascii', 'ignore')
- # make the text lowercase
- text = text.lower()
+ # make the text lowercase (optional)
+ if lowercase:
+ text = text.lower()
# remove generated quotes -- post-process
text = QUOTE_PATTERN.sub('', text)
@@ -137,7 +140,10 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
text = NUMBERS_PATTERN.sub('', text)
# replace all other unwanted characters
- pattern = regex_pattern or ALLOWED_CHARS_PATTERN
+ if lowercase:
+ pattern = regex_pattern or ALLOWED_CHARS_PATTERN
+ else:
+ pattern = regex_pattern or ALLOWED_CHARS_PATTERN_WITH_UPPERCASE
text = re.sub(pattern, DEFAULT_SEPARATOR, text)
# remove redundant
@@ -145,8 +151,11 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
# remove stopwords
if stopwords:
- stopwords_lower = [s.lower() for s in stopwords]
- words = [w for w in text.split(DEFAULT_SEPARATOR) if w not in stopwords_lower]
+ if lowercase:
+ stopwords_lower = [s.lower() for s in stopwords]
+ words = [w for w in text.split(DEFAULT_SEPARATOR) if w not in stopwords_lower]
+ else:
+ words = [w for w in text.split(DEFAULT_SEPARATOR) if w not in stopwords]
text = DEFAULT_SEPARATOR.join(words)
# smart truncate if requested
diff --git a/test.py b/test.py
index 3d37a0b..9ff9ec0 100644
--- a/test.py
+++ b/test.py
@@ -113,6 +113,11 @@ class TestSlugification(unittest.TestCase):
r = slugify(txt, stopwords=['stopword'])
self.assertEqual(r, 'this-has-a')
+ def test_stopword_removal_casesensitive(self):
+ txt = 'thIs Has a stopword Stopword'
+ r = slugify(txt, stopwords=['Stopword'], lowercase=False)
+ self.assertEqual(r, 'thIs-Has-a-stopword')
+
def test_multiple_stopword_occurances(self):
txt = 'the quick brown fox jumps over the lazy dog'
r = slugify(txt, stopwords=['the'])