# HG changeset patch # User Adrien Di Mascio # Date 1237988484 -3600 # Node ID 81a383cdda5c38a7135cf680a96b554376bf4653 # Parent 4b920f836567f4b12f578400a98a15e2a1c9bf28 text_cut() accepts a gotoperiod parameter diff -r 4b920f836567 -r 81a383cdda5c 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 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):