Skip to content

Commit c096bcd

Browse files
committed
regex: allow=>disallow
1 parent 86d76ee commit c096bcd

6 files changed

Lines changed: 15 additions & 12 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
push:
77
branches:
88
- ci
9+
- staging
910

1011
jobs:
1112
build:

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.0.1
2+
3+
- Rework regex_pattern to mean the opposite (disallowed chars instead of allowed)
4+
- Thanks to @yyyyyyyan for the initial PR followed by the final PR by @mrezzamoradi
5+
16
## 6.0.0
27

38
- Enable github action

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def slugify(
5555
:param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order
5656
:param separator (str): separator between words
5757
:param stopwords (iterable): words to discount
58-
:param regex_pattern (str): regex pattern for allowed characters
58+
:param regex_pattern (str): regex pattern for disallowed characters
5959
:param lowercase (bool): activate case sensitivity by setting it to False
6060
:param replacements (iterable): list of replacement rules e.g. [['|', 'or'], ['%', 'percent']]
6161
:return (str): slugify text

slugify/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse_args(argv):
3131
parser.add_argument("--stopwords", nargs='+',
3232
help="Words to discount")
3333
parser.add_argument("--regex-pattern",
34-
help="Python regex pattern for allowed characters")
34+
help="Python regex pattern for disallowed characters")
3535
parser.add_argument("--no-lowercase", action='store_false', dest='lowercase', default=True,
3636
help="Activate case sensitivity")
3737
parser.add_argument("--replacements", nargs='+',

slugify/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
__url__ = 'https://github.com/un33k/python-slugify'
66
__license__ = 'MIT'
77
__copyright__ = 'Copyright 2022 Val Neekman @ Neekware Inc.'
8-
__version__ = '6.0.0'
8+
__version__ = '6.0.1'

slugify/slugify.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
2-
import unicodedata
32
import sys
3+
import typing
4+
import unicodedata
45
from html.entities import name2codepoint
56

67
try:
@@ -15,8 +16,7 @@
1516
DECIMAL_PATTERN = re.compile(r'&#(\d+);')
1617
HEX_PATTERN = re.compile(r'&#x([\da-fA-F]+);')
1718
QUOTE_PATTERN = re.compile(r'[\']+')
18-
ALLOWED_CHARS_PATTERN = re.compile(r'[^-a-z0-9]+')
19-
ALLOWED_CHARS_PATTERN_WITH_UPPERCASE = re.compile(r'[^-a-zA-Z0-9]+')
19+
DISALLOWED_CHARS_PATTERN = re.compile(r'[^-a-zA-Z0-9]+')
2020
DUPLICATE_DASH_PATTERN = re.compile(r'-{2,}')
2121
NUMBERS_PATTERN = re.compile(r'(?<=\d),(?=\d)')
2222
DEFAULT_SEPARATOR = '-'
@@ -66,7 +66,7 @@ def smart_truncate(string, max_length=0, word_boundary=False, separator=' ', sav
6666

6767
def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False,
6868
separator=DEFAULT_SEPARATOR, save_order=False, stopwords=(), regex_pattern=None, lowercase=True,
69-
replacements=()):
69+
replacements: typing.Iterable[typing.Iterable[str]] = ()):
7070
"""
7171
Make a slug from the given text.
7272
:param text (str): initial text
@@ -78,7 +78,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
7878
:param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order
7979
:param separator (str): separator between words
8080
:param stopwords (iterable): words to discount
81-
:param regex_pattern (str): regex pattern for allowed characters
81+
:param regex_pattern (str): regex pattern for disallowed characters
8282
:param lowercase (bool): activate case sensitivity by setting it to False
8383
:param replacements (iterable): list of replacement rules e.g. [['|', 'or'], ['%', 'percent']]
8484
:return (str):
@@ -137,10 +137,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
137137
text = NUMBERS_PATTERN.sub('', text)
138138

139139
# replace all other unwanted characters
140-
if lowercase:
141-
pattern = regex_pattern or ALLOWED_CHARS_PATTERN
142-
else:
143-
pattern = regex_pattern or ALLOWED_CHARS_PATTERN_WITH_UPPERCASE
140+
pattern = regex_pattern or DISALLOWED_CHARS_PATTERN
144141
text = re.sub(pattern, DEFAULT_SEPARATOR, text)
145142

146143
# remove redundant

0 commit comments

Comments
 (0)