diff options
Diffstat (limited to 'slugify')
| -rw-r--r-- | slugify/__init__.py | 2 | ||||
| -rw-r--r-- | slugify/slugify.py | 21 |
2 files changed, 16 insertions, 7 deletions
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 |
