Added "br" TALES expression to convert characters to HTML breaks
authorThierry Florac <thierry.florac@onf.fr>
Fri, 06 Jul 2018 09:47:39 +0200
changeset 215 0910e92af5e7
parent 214 e01d3a0db652
child 216 8a6c250fe655
Added "br" TALES expression to convert characters to HTML breaks
src/pyams_utils/text.py
--- a/src/pyams_utils/text.py	Wed Jul 04 09:05:30 2018 +0200
+++ b/src/pyams_utils/text.py	Fri Jul 06 09:47:39 2018 +0200
@@ -152,3 +152,18 @@
         terms = [SimpleTerm(name, name, translate(adapt.title).label)
                  for name, adapt in registry.getAdapters(('', request), IHTMLRenderer)]
         super(RenderersVocabulary, self).__init__(terms)
+
+
+@adapter_config(name='br', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class BrTalesExtension(ContextRequestViewAdapter):
+    """extension:br(value, class) TALES expression
+
+    This expression can be used to context a given character ('|' by default) into HTML
+    breaks with given CSS class.
+    """
+
+    def render(self, context=None, css_class='', character='|'):
+        if context is None:
+            context = self.context
+        br = '<br {0} />'.format('class="{0}"'.format(css_class) if css_class else '')
+        return context.replace(character, br)