diff options
| author | Val Neekman <val@neekware.com> | 2018-09-02 12:36:52 -0400 |
|---|---|---|
| committer | Val Neekman <val@neekware.com> | 2018-09-02 12:36:52 -0400 |
| commit | beca50eaff20ca20652df782354a8f184c1eaf4c (patch) | |
| tree | f0afd8a85c1069d5d1f1922cdf5fb45bd0c93841 /slugify/slugify.py | |
| parent | 4c1a344652dfef32f786a96d94b351f4277da964 (diff) | |
| download | python-slugify-1.2.6.tar.gz | |
release 1.2.6, case sensitive slug support1.2.6
Diffstat (limited to 'slugify/slugify.py')
| -rw-r--r-- | slugify/slugify.py | 21 |
1 files changed, 15 insertions, 6 deletions
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 |
