summaryrefslogtreecommitdiff
path: root/slugify
diff options
context:
space:
mode:
authorVal Neekman <val@neekware.com>2013-02-13 10:42:21 -0500
committerVal Neekman <val@neekware.com>2013-02-13 10:42:21 -0500
commita4c78fab0157d7da3ed77ca8b2f98cd0dbac5bf2 (patch)
tree81919d92988a7cca26fa147bbf4dd4849a186594 /slugify
parent19587b7674691f059c5058fdbb858284afea0e4b (diff)
downloadpython-slugify-a4c78fab0157d7da3ed77ca8b2f98cd0dbac5bf2.tar.gz
added truncation0.0.3
Diffstat (limited to 'slugify')
-rw-r--r--slugify/__init__.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/slugify/__init__.py b/slugify/__init__.py
index fe634bf..8d0295a 100644
--- a/slugify/__init__.py
+++ b/slugify/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-__version__ = '0.0.2'
+__version__ = '0.0.3'
__all__ = ['slugify']
@@ -23,7 +23,22 @@ REPLACE1_REXP = re.compile(r'[\']+')
REPLACE2_REXP = re.compile(r'[^-a-z0-9]+')
REMOVE_REXP = re.compile('-{2,}')
-def slugify(text, entities=True, decimal=True, hexadecimal=True):
+def smart_truncate(text, max_length=0, word_boundaries=False):
+ if not max_length or len(text) < max_length:
+ return text
+
+ if not word_boundaries:
+ return text[:max_length].strip('-')
+
+ truncated = ''
+ for i, word in enumerate(text.split('-')):
+ if not word: continue
+ if len(truncated) + len(word) + i <= max_length:
+ truncated += '{0}{1}'.format('-' if i else '', word)
+ return truncated.strip('-')
+
+
+def slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False):
""" Make a slug from the given text """
# text to unicode
@@ -64,4 +79,12 @@ def slugify(text, entities=True, decimal=True, hexadecimal=True):
# remove redundant -
text = REMOVE_REXP.sub('-', text).strip('-')
+ # smart truncate if requested
+ if max_length > 0:
+ text = smart_truncate(text, max_length, word_boundary)
+
return text
+
+
+
+