Added ztfy.workflow.tal package to handle text/html conversions from TALES
authorThierry Florac <tflorac@ulthar.net>
Tue, 02 Mar 2010 23:32:58 +0100
changeset 30 3c237f16b603
parent 29 035058514c2d
child 31 f7cd6dbc6dd8
Added ztfy.workflow.tal package to handle text/html conversions from TALES
ztfy/utils/configure.zcml
ztfy/utils/tal/__init__.py
ztfy/utils/tal/configure.zcml
ztfy/utils/tal/html.py
ztfy/utils/tal/interfaces.py
ztfy/utils/tal/text.py
--- a/ztfy/utils/configure.zcml	Tue Dec 22 22:59:37 2009 +0100
+++ b/ztfy/utils/configure.zcml	Tue Mar 02 23:32:58 2010 +0100
@@ -8,4 +8,6 @@
 	<adapter
 		factory=".timezone.tzinfo" />
 
+	<include package=".tal" />
+
 </configure>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/__init__.py	Tue Mar 02 23:32:58 2010 +0100
@@ -0,0 +1,1 @@
+#
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/configure.zcml	Tue Mar 02 23:32:58 2010 +0100
@@ -0,0 +1,23 @@
+<configure
+	xmlns="http://namespaces.zope.org/zope"
+	i18n_domain="ztfy.utils">
+
+	<adapter
+		name="start"
+		factory=".text.TextStartTalesAdapter"
+		provides="zope.traversing.interfaces.IPathAdapter"
+		for="*" />
+
+	<adapter
+		name="text"
+		factory=".text.TextOutputTalesAdapter"
+		provides="zope.traversing.interfaces.IPathAdapter"
+		for="*" />
+
+	<adapter
+		name="html"
+		factory=".html.HTMLTalesAdapter"
+		provides="zope.traversing.interfaces.IPathAdapter"
+		for="*" />
+
+</configure>
\ No newline at end of file
--- /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()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/interfaces.py	Tue Mar 02 23:32:58 2010 +0100
@@ -0,0 +1,63 @@
+### -*- 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
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.interface import Interface
+
+# import local packages
+
+
+class ITextStartTalesAPI(Interface):
+    """'start' TALES namespace interface"""
+
+    def __getattr__(attr):
+        """Get first characters of adapted text, without cutting words"""
+
+
+class ITextOutputTalesAPI(Interface):
+    """'text' TALES namespace interface"""
+
+    def js():
+        """Convert adapted text to a JavaScript compatible output"""
+
+    def noquotes():
+        """Remove double quotes from adapted text"""
+
+    def breaks():
+        """Replace '|' by newlines in adapted text"""
+
+
+class IHTMLTalesAPI(Interface):
+    """'html' TALES namespace interface"""
+
+    def totext():
+        """Convert adapted HTML content to text"""
+
+    def text():
+        """Convert adapted text to HTML"""
+
+    def stx():
+        """Convert adapted StructuredText to HTML"""
+
+    def rest():
+        """Convert adapted reStructuredText to HTML"""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/tal/text.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 ITextStartTalesAPI, ITextOutputTalesAPI
+
+# import Zope3 packages
+from zope.interface import implements
+
+# import local packages
+from ztfy.utils.text import textStart
+
+
+class TextStartTalesAdapter(object):
+
+    implements(ITextStartTalesAPI, ITALESFunctionNamespace)
+
+    def __init__(self, context):
+        self.context = context
+
+    def setEngine(self, engine):
+        self.request = engine.vars['request']
+
+    def __getattr__(self, attr):
+        try:
+            if attr.find(',') > 0:
+                length, max = (int(x) for x in attr.split(','))
+            else:
+                length = int(attr)
+                max = 0
+            return textStart(self.context, length, max)
+        except:
+            return ''
+
+
+class TextOutputTalesAdapter(object):
+
+    implements(ITextOutputTalesAPI, ITALESFunctionNamespace)
+
+    def __init__(self, context):
+        self.context = context
+
+    def setEngine(self, engine):
+        self.request = engine.vars['request']
+
+    def js(self):
+        return self.context.replace("'", "&#039;").replace("\n", "&#013;")
+
+    def noquotes(self):
+        return self.context.replace('"', '')
+
+    def breaks(self):
+        return self.context.replace('|', '\n')