summaryrefslogtreecommitdiff
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
parent6e1793fcda103bc1cae7f96295c56574be7880cc (diff)
downloadpython-slugify-96fec754cc7448ac0e76697e034c26b263b73ea9.tar.gz
Add save_order parameter.
Fix count next length. Add breaks.
-rw-r--r--slugify/slugify.py38
-rw-r--r--test.py24
2 files changed, 47 insertions, 15 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)
diff --git a/test.py b/test.py
index a66b263..90ad251 100644
--- a/test.py
+++ b/test.py
@@ -53,14 +53,6 @@ class TestSequenceFunctions(unittest.TestCase):
self.assertEqual(r, "jaja-lol-a")
txt = 'jaja---lol-méméméoo--a'
- r = slugify(txt, max_length=19, word_boundary=True)
- self.assertEqual(r, "jaja-lol-mememeoo")
-
- txt = 'jaja---lol-méméméoo--a'
- r = slugify(txt, max_length=20, 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")
@@ -76,5 +68,21 @@ 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()