# HG changeset patch # User Damien Correia # Date 1542207177 -3600 # Node ID 5ef130497172871b5e5a022f46dbf030fa35a5b3 # Parent b9398ca282c75115c872c35f9521fcda7f1b199f Created new tales Extention to ellipsis to long sentence diff -r b9398ca282c7 -r 5ef130497172 src/pyams_utils/text.py --- a/src/pyams_utils/text.py Fri Nov 09 11:55:14 2018 +0100 +++ b/src/pyams_utils/text.py Wed Nov 14 15:52:57 2018 +0100 @@ -28,9 +28,9 @@ def get_text_start(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 this value - + :param str text: initial text :param integer length: maximum length of resulting text :param integer max: if > 0, *text* is shortened only if remaining text is longer than max @@ -185,3 +185,18 @@ if end_tag: elements[-1] = '<{0}>{1}'.format(end_tag, elements[-1]) return br.join(elements) + +@adapter_config(name='truncatechars', context=(Interface, Interface, Interface), provides=ITALESExtension) +class TruncateCharsTalesExtension(ContextRequestViewAdapter): + """extension:truncatechars(value, length, max) TALES expression + + Truncates a sentence if it is longer than the specified 'length' of characters. + Truncated strings will end with a ellipsis character (“…”) + see also 'get_text_start' + """ + + @staticmethod + def render(value, length=50, max=0): + if not value: + return '' + return get_text_start(value, length, max=max)