text_cut() accepts a gotoperiod parameter
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 25 Mar 2009 14:41:24 +0100
changeset 1157 81a383cdda5c
parent 1156 4b920f836567
child 1158 98f83f1588d7
text_cut() accepts a gotoperiod parameter
common/uilib.py
--- a/common/uilib.py	Wed Mar 18 16:27:49 2009 +0100
+++ b/common/uilib.py	Wed Mar 25 14:41:24 2009 +0100
@@ -180,21 +180,27 @@
             if add_ellipsis:
                 return text + u'...'
             return text
-        
-def text_cut(text, nbwords=30):
+
+def text_cut(text, nbwords=30, gotoperiod=True):
     """from the given plain text, return a text with at least <nbwords> words,
     trying to go to the end of the current sentence.
 
+    :param nbwords: the minimum number of words required
+    :param gotoperiod: specifies if the function should try to go to
+                       the first period after the cut (i.e. finish
+                       the sentence if possible)
+
     Note that spaces are normalized.
     """
     if text is None:
         return u''
     words = text.split()
     text = u' '.join(words) # normalize spaces
-    minlength = len(' '.join(words[:nbwords]))
-    textlength = text.find('.', minlength) + 1
-    if textlength == 0: # no point found
-        textlength = minlength 
+    textlength = minlength = len(' '.join(words[:nbwords]))
+    if gotoperiod:
+        textlength = text.find('.', minlength) + 1
+        if textlength == 0: # no period found
+            textlength = minlength
     return text[:textlength]
 
 def cut(text, length):