src/pyams_utils/text.py
branchdev-dc
changeset 278 5ef130497172
parent 236 b6d7d1a1feed
child 280 f37f19b7fff3
equal deleted inserted replaced
251:b9398ca282c7 278:5ef130497172
    26 from pyams_utils.vocabulary import vocabulary_config
    26 from pyams_utils.vocabulary import vocabulary_config
    27 
    27 
    28 
    28 
    29 def get_text_start(text, length, max=0):
    29 def get_text_start(text, length, max=0):
    30     """Get first words of given text with maximum given length
    30     """Get first words of given text with maximum given length
    31     
    31 
    32     If *max* is specified, text is shortened only if remaining text is longer this value
    32     If *max* is specified, text is shortened only if remaining text is longer this value
    33     
    33 
    34     :param str text: initial text
    34     :param str text: initial text
    35     :param integer length: maximum length of resulting text
    35     :param integer length: maximum length of resulting text
    36     :param integer max: if > 0, *text* is shortened only if remaining text is longer than max
    36     :param integer max: if > 0, *text* is shortened only if remaining text is longer than max
    37 
    37 
    38     >>> from pyams_utils.text import get_text_start
    38     >>> from pyams_utils.text import get_text_start
   183         if start_tag:
   183         if start_tag:
   184             elements[0] = '<{0}>{1}</{0}>'.format(start_tag, elements[0])
   184             elements[0] = '<{0}>{1}</{0}>'.format(start_tag, elements[0])
   185         if end_tag:
   185         if end_tag:
   186             elements[-1] = '<{0}>{1}</{0}>'.format(end_tag, elements[-1])
   186             elements[-1] = '<{0}>{1}</{0}>'.format(end_tag, elements[-1])
   187         return br.join(elements)
   187         return br.join(elements)
       
   188 
       
   189 @adapter_config(name='truncatechars', context=(Interface, Interface, Interface), provides=ITALESExtension)
       
   190 class TruncateCharsTalesExtension(ContextRequestViewAdapter):
       
   191     """extension:truncatechars(value, length, max) TALES expression
       
   192 
       
   193     Truncates a sentence if it is longer than the specified 'length' of characters.
       
   194     Truncated strings will end with a ellipsis character (“…”)
       
   195     see also 'get_text_start'
       
   196     """
       
   197 
       
   198     @staticmethod
       
   199     def render(value, length=50, max=0):
       
   200         if not value:
       
   201             return ''
       
   202         return get_text_start(value, length, max=max)