summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVal Neekman <un33kvu@gmail.com>2015-02-26 16:51:41 -0500
committerVal Neekman <un33kvu@gmail.com>2015-02-26 16:51:41 -0500
commitf64f1a3e84a73d54def5f89130c07a7b0814424e (patch)
treeb81793ff4b98a2c762711abf91905bb6da6ba920
parent9a8f8ddb86363d65687daf2d48d2070a2fe97e24 (diff)
parent967725fbdeaaa6c5b2a54dafdadfccee663768fb (diff)
downloadpython-slugify-f64f1a3e84a73d54def5f89130c07a7b0814424e.tar.gz
Merge pull request #9 from dioexul/master
Add parameter 'save_order'.
-rw-r--r--slugify/slugify.py36
-rw-r--r--test.py17
2 files changed, 47 insertions, 6 deletions
diff --git a/slugify/slugify.py b/slugify/slugify.py
index bb2eb3b..aae4622 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, separator=' ', save_order=False):
+ """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 of output string is like input string
+ :param separator (str): separator between words
+ :return:
+ """
string = string.strip(separator)
@@ -44,15 +51,32 @@ def smart_truncate(string, max_length=0, word_boundaries=False, separator=' '):
for word in string.split(separator):
if word:
next_len = len(truncated) + len(word)
- if next_len <= max_length:
+ 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,
+ separator='-', save_order=False):
+ """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)
diff --git a/test.py b/test.py
index b1d4ca9..7471997 100644
--- a/test.py
+++ b/test.py
@@ -80,5 +80,22 @@ class TestSequenceFunctions(unittest.TestCase):
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")
+ txt = 'one two three four five'
+ r = slugify(txt, max_length=13, word_boundary=True, save_order=True)
+ self.assertEqual(r, "one-two-three")
+
+ txt = 'one two three four five'
+ r = slugify(txt, max_length=13, word_boundary=True, save_order=False)
+ self.assertEqual(r, "one-two-three")
+
+ txt = 'one two three four five'
+ r = slugify(txt, max_length=12, word_boundary=True, save_order=False)
+ self.assertEqual(r, "one-two-four")
+
+ txt = 'one two three four five'
+ r = slugify(txt, max_length=12, word_boundary=True, save_order=True)
+ self.assertEqual(r, "one-two")
+
+
if __name__ == '__main__':
unittest.main() \ No newline at end of file