summaryrefslogtreecommitdiff
path: root/slugify
diff options
context:
space:
mode:
authory.selyutina <y.selyutina@rambler-co.ru>2015-02-18 11:07:52 +0300
committery.selyutina <y.selyutina@rambler-co.ru>2015-02-18 11:17:28 +0300
commit96fec754cc7448ac0e76697e034c26b263b73ea9 (patch)
tree74b0d8e3273df57fb65eab34d50444ca7b3c2f4d /slugify
parent6e1793fcda103bc1cae7f96295c56574be7880cc (diff)
downloadpython-slugify-96fec754cc7448ac0e76697e034c26b263b73ea9.tar.gz
Add save_order parameter.
Fix count next length. Add breaks.
Diffstat (limited to 'slugify')
-rw-r--r--slugify/slugify.py38
1 files changed, 31 insertions, 7 deletions
diff --git a/slugify/slugify.py b/slugify/slugify.py
index ba15274..efccb23 100644
--- a/slugify/slugify.py
+++ b/slugify/slugify.py
@@ -23,8 +23,15 @@ REPLACE2_REXP = re.compile(r'[^-a-z0-9]+')
REMOVE_REXP = re.compile('-{2,}')
-def smart_truncate(string, max_length=0, word_boundaries=False, separator=' '):
- """ Truncate a string """
+def smart_truncate(string, max_length=0, word_boundaries=False, save_order=False, separator=' '):
+ """Truncate a string.
+ :param string (str): string for modification
+ :param max_length (int): output string length
+ :param word_boundaries (bool):
+ :param save_order (bool): if True then word order output string is like input string
+ :param separator (str): separator between words
+ :return:
+ """
string = string.strip(separator)
@@ -43,16 +50,33 @@ def smart_truncate(string, max_length=0, word_boundaries=False, separator=' '):
truncated = ''
for word in string.split(separator):
if word:
- next_len = len(truncated) + len(word) + len(separator)
- if next_len <= max_length:
+ next_len = len(truncated) + len(word)
+ if next_len < max_length:
truncated += '{0}{1}'.format(word, separator)
+ elif next_len == max_length:
+ truncated += '{0}'.format(word)
+ break
+ else:
+ if save_order:
+ break
if not truncated:
truncated = string[:max_length]
return truncated.strip(separator)
-def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-'):
- """ Make a slug from the given text """
+def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, save_order=False,
+ separator='-'):
+ """Make a slug from the given text.
+ :param text (str): initial text
+ :param entities (bool):
+ :param decimal (bool):
+ :param hexadecimal (bool):
+ :param max_length (int): output string length
+ :param word_boundary (bool):
+ :param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order
+ :param separator (str): separator between words
+ :return (str):
+ """
# text to unicode
if not isinstance(text, types.UnicodeType):
@@ -97,7 +121,7 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, w
# smart truncate if requested
if max_length > 0:
- text = smart_truncate(text, max_length, word_boundary, '-')
+ text = smart_truncate(text, max_length, word_boundary, save_order, '-')
if separator != '-':
text = text.replace('-', separator)