diff options
author | dioexul <dioexul@gmail.com> | 2015-02-22 21:21:27 +0300 |
---|---|---|
committer | dioexul <dioexul@gmail.com> | 2015-02-22 21:21:27 +0300 |
commit | 8a521b0283f45b8d1bf4e991c51f49e9ff3fc82f (patch) | |
tree | 236e7edf314e6c52c13c65839b642c163b273c3a | |
parent | 07155c2001580b0ec70b40f24a475baef6238aac (diff) | |
download | python-slugify-8a521b0283f45b8d1bf4e991c51f49e9ff3fc82f.tar.gz |
Add parameter 'save_order'.
-rw-r--r-- | slugify/slugify.py | 36 | ||||
-rw-r--r-- | test.py | 17 |
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) @@ -80,5 +80,22 @@ class TestSequenceFunctions(unittest.TestCase): r = slugify(txt) self.assertEqual(r, "this-is-a-test") + txt = 'Тестирование полных слов без замены' + r = slugify(txt, max_length=20, word_boundary=True, save_order=False) + self.assertEqual(r, "testirovanie-polnykh") + + txt = 'Тестирование полных слов без замены' + r = slugify(txt, max_length=19, word_boundary=True, save_order=False) + self.assertEqual(r, "testirovanie-slov") + + txt = 'Тестирование полных слов без замены' + r = slugify(txt, max_length=20, word_boundary=True, save_order=True) + self.assertEqual(r, "testirovanie-polnykh") + + txt = 'Тестирование полных слов без замены' + r = slugify(txt, max_length=19, word_boundary=True, save_order=True) + self.assertEqual(r, "testirovanie") + + if __name__ == '__main__': unittest.main()
\ No newline at end of file |