# HG changeset patch # User Thierry Florac # Date 1542214573 -3600 # Node ID f37f19b7fff3f37c938f26ef0e3d33214f5f6fde # Parent 1506cc3bc32e9dee8ba1b7330ce01d4dedab08bb# Parent 5ef130497172871b5e5a022f46dbf030fa35a5b3 Added "truncate" TALES extension diff -r 1506cc3bc32e -r f37f19b7fff3 src/pyams_utils/text.py --- a/src/pyams_utils/text.py Wed Nov 14 17:52:55 2018 +0100 +++ b/src/pyams_utils/text.py Wed Nov 14 17:56:13 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 @@ -55,6 +55,22 @@ return text +@adapter_config(name='truncate', context=(Interface, Interface, Interface), provides=ITALESExtension) +class TruncateCharsTalesExtension(ContextRequestViewAdapter): + """extension:truncate(value, length, max) TALES expression + + Truncates a sentence if it is longer than the specified 'length' characters. + Truncated strings will end with an 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) + + @adapter_config(name='raw', context=(str, IRequest), provides=IHTMLRenderer) class BaseHTMLRenderer(ContextRequestAdapter): """Raw text HTML renderer