# HG changeset patch # User Thierry Florac # Date 1267569267 -3600 # Node ID 266139614a46d2a814b9e2f0495869f156b75051 # Parent f7cd6dbc6dd8eb4b9d804bae7c3a8b5e0e5545ca Added converters from text, StructuredText and reStructuredText to HTML diff -r f7cd6dbc6dd8 -r 266139614a46 ztfy/utils/text.py --- /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 +# 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