common/uilib.py
changeset 1157 81a383cdda5c
parent 927 bfcc610c3d5e
child 1263 01152fffd593
--- 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):