Add customizable renderer in text and TAL APIs
authorThierry Florac <tflorac@ulthar.net>
Sat, 17 Apr 2010 02:16:51 +0200
changeset 52 6a542c8e1b96
parent 51 c032ae51be17
child 53 e2c28fc82285
Add customizable renderer in text and TAL APIs
ztfy/utils/tal/html.py
ztfy/utils/tal/interfaces.py
ztfy/utils/text.py
--- a/ztfy/utils/tal/html.py	Sat Apr 10 16:31:08 2010 +0200
+++ b/ztfy/utils/tal/html.py	Sat Apr 17 02:16:51 2010 +0200
@@ -28,7 +28,7 @@
 
 # import local packages
 from ztfy.utils.html import htmlToText
-from ztfy.utils.text import textToHTML
+from ztfy.utils.text import textToHTML, Renderer
 
 
 class HTMLTalesAdapter(object):
@@ -41,6 +41,9 @@
     def setEngine(self, engine):
         self.request = engine.vars['request']
 
+    def renderer(self):
+        return Renderer(self.context)
+
     def totext(self):
         if not self.context:
             return u''
--- a/ztfy/utils/tal/interfaces.py	Sat Apr 10 16:31:08 2010 +0200
+++ b/ztfy/utils/tal/interfaces.py	Sat Apr 17 02:16:51 2010 +0200
@@ -50,6 +50,9 @@
 class IHTMLTalesAPI(Interface):
     """'html' TALES namespace interface"""
 
+    def renderer():
+        """Get text to HTML renderer"""
+
     def totext():
         """Convert adapted HTML content to text"""
 
--- a/ztfy/utils/text.py	Sat Apr 10 16:31:08 2010 +0200
+++ b/ztfy/utils/text.py	Sat Apr 17 02:16:51 2010 +0200
@@ -56,3 +56,14 @@
     formatter = createObject(renderer, text)
     renderer = zapi.queryMultiAdapter((formatter, request), name=u'')
     return renderer.render()
+
+
+class Renderer(object):
+
+    def __init__(self, context):
+        self.context = context
+
+    def render(self, renderer, request=None):
+        if not self.context:
+            return u''
+        return textToHTML(self.context, renderer, request)