diff options
author | dioexul <dioexul@gmail.com> | 2015-02-21 19:17:40 +0300 |
---|---|---|
committer | dioexul <dioexul@gmail.com> | 2015-02-21 19:17:40 +0300 |
commit | 07155c2001580b0ec70b40f24a475baef6238aac (patch) | |
tree | 4d291123301f3b252ffa7fe93bb53cd7fb84e494 | |
parent | 3d75734751b36ae14feab0d84f5d670f1d6225e3 (diff) | |
download | python-slugify-07155c2001580b0ec70b40f24a475baef6238aac.tar.gz |
Fix bug (len of output string).
-rw-r--r-- | slugify/slugify.py | 38 | ||||
-rw-r--r-- | test.py | 30 |
2 files changed, 20 insertions, 48 deletions
diff --git a/slugify/slugify.py b/slugify/slugify.py index 9314154..bb2eb3b 100644 --- a/slugify/slugify.py +++ b/slugify/slugify.py @@ -23,15 +23,8 @@ REPLACE2_REXP = re.compile(r'[^-a-z0-9]+') REMOVE_REXP = re.compile('-{2,}') -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 output string is like input string - :param separator (str): separator between words - :return: - """ +def smart_truncate(string, max_length=0, word_boundaries=False, separator=' '): + """ Truncate a string """ string = string.strip(separator) @@ -51,32 +44,15 @@ def smart_truncate(string, max_length=0, word_boundaries=False, separator=' ', s 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='-', 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): - """ +def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-'): + """ Make a slug from the given text """ # text to unicode if not isinstance(text, types.UnicodeType): @@ -121,7 +97,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, '-', save_order) + text = smart_truncate(text, max_length, word_boundary, '-') if separator != '-': text = text.replace('-', separator) @@ -134,4 +110,4 @@ def main(): print "Usage %s TEXT TO SLUGIFY" % sys.argv[0] return text = ' '.join(sys.argv[1:]) - print slugify(text) + print slugify(text)
\ No newline at end of file @@ -53,6 +53,18 @@ class TestSequenceFunctions(unittest.TestCase): self.assertEqual(r, "jaja-lol-a") txt = 'jaja---lol-méméméoo--a' + r = slugify(txt, max_length=17, word_boundary=True) + self.assertEqual(r, "jaja-lol-mememeoo") + + txt = 'jaja---lol-méméméoo--a' + r = slugify(txt, max_length=18, word_boundary=True) + self.assertEqual(r, "jaja-lol-mememeoo") + + txt = 'jaja---lol-méméméoo--a' + r = slugify(txt, max_length=19, word_boundary=True) + self.assertEqual(r, "jaja-lol-mememeoo-a") + + txt = 'jaja---lol-méméméoo--a' r = slugify(txt, max_length=20, word_boundary=True, separator=".") self.assertEqual(r, "jaja.lol.mememeoo.a") @@ -68,21 +80,5 @@ 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() + unittest.main()
\ No newline at end of file |