Skip to content

Commit 750c737

Browse files
vrbaskizun33k
authored andcommitted
Added possibility to include custom regex as allowed characters (un33k#36)
Added possibility to allow custom regex as allowed characters.
1 parent 3f7ce87 commit 750c737

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

slugify/slugify.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def smart_truncate(string, max_length=0, word_boundaries=False, separator=' ', s
7171

7272

7373
def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False,
74-
separator='-', save_order=False, stopwords=()):
74+
separator='-', save_order=False, stopwords=(), allowed_characters=None):
7575
"""
7676
Make a slug from the given text.
7777
:param text (str): initial text
@@ -131,7 +131,11 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
131131

132132
# replace unwanted characters
133133
text = NUMBERS_PATTERN.sub('', text)
134-
text = ALLOWED_CHARS_PATTERN.sub('-', text)
134+
print(text)
135+
if allowed_characters:
136+
text = re.sub(allowed_characters, '-', text)
137+
else:
138+
text = ALLOWED_CHARS_PATTERN.sub('-', text)
135139

136140
# remove redundant -
137141
text = DUPLICATE_DASH_PATTERN.sub('-', text).strip('-')

test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ def test_numbers_and_symbols(self):
167167
r = slugify(txt)
168168
self.assertEqual(r, '1000-reasons-you-are-1')
169169

170+
def test_underscore(self):
171+
txt = "___This is a test___"
172+
r = slugify(txt, allowed_characters=r'[^-a-z0-9_]+')
173+
self.assertEqual(r, "___this-is-a-test___")
174+
170175

171176
class TestUtils(unittest.TestCase):
172177

0 commit comments

Comments
 (0)