ztfy/utils/tal/html.py
changeset 30 3c237f16b603
child 41 da1558b96f0a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/html.py	Tue Mar 02 23:32:58 2010 +0100
@@ -0,0 +1,72 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+from zope.tales.interfaces import ITALESFunctionNamespace
+
+# import local interfaces
+from interfaces import IHTMLTalesAPI
+
+# import Zope3 packages
+from zope.app import zapi
+from zope.app.renderer.plaintext import PlainTextToHTMLRenderer
+from zope.app.renderer.rest import ReStructuredTextToHTMLRenderer
+from zope.app.renderer.stx import StructuredTextToHTMLRenderer
+from zope.component import createObject
+from zope.interface import implements
+
+# import local packages
+from ztfy.utils.html import htmlToText
+
+
+class HTMLTalesAdapter(object):
+
+    implements(IHTMLTalesAPI, ITALESFunctionNamespace)
+
+    def __init__(self, context):
+        self.context = context
+
+    def setEngine(self, engine):
+        self.request = engine.vars['request']
+
+    def totext(self):
+        if not self.context:
+            return u''
+        return htmlToText(self.context)
+
+    def text(self):
+        if not self.context:
+            return u''
+        formatter = createObject('zope.source.plaintext', self.context)
+        renderer = PlainTextToHTMLRenderer(formatter, self.request)
+        return renderer.render()
+
+    def stx(self):
+        if not self.context:
+            return u''
+        formatter = createObject('zope.source.stx', self.context)
+        renderer = StructuredTextToHTMLRenderer(formatter, self.request)
+        return renderer.render()
+
+    def rest(self):
+        if not self.context:
+            return u''
+        formatter = createObject('zope.source.rest', self.context)
+        renderer = ReStructuredTextToHTMLRenderer(formatter, self.request)
+        return renderer.render()