--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/text.py Tue Mar 02 23:34:27 2010 +0100
@@ -0,0 +1,47 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+
+# import local interfaces
+
+# import Zope3 packages
+
+# import local packages
+
+
+def textStart(text, length, max=0):
+ """Get first words of given text with maximum given length
+
+ If @max is specified, text is shortened only if remaining text is longer than @max
+
+ @param text: initial text
+ @param length: maximum length of resulting text
+ @param max: if > 0, @text is shortened only if remaining text is longer than max
+ """
+ result = text or ''
+ if length > len(result):
+ return result
+ index = length - 1
+ text_length = len(result)
+ while (index > 0) and (result[index] != ' '):
+ index -= 1
+ if (index > 0) and (text_length > index + max):
+ return result[:index] + '…'
+ return text