# HG changeset patch # User Thierry Florac # Date 1267569178 -3600 # Node ID 3c237f16b603dc62a9d858aa7344c03f720c0b09 # Parent 035058514c2d3988e25d3938bb8afedc62dca9d4 Added ztfy.workflow.tal package to handle text/html conversions from TALES diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/configure.zcml --- 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 @@ + + diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/tal/__init__.py --- /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 @@ +# diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/tal/configure.zcml --- /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 @@ + + + + + + + + + \ No newline at end of file diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/tal/html.py --- /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 +# 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() diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/tal/interfaces.py --- /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 +# 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""" diff -r 035058514c2d -r 3c237f16b603 ztfy/utils/tal/text.py --- /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 +# 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("'", "'").replace("\n", " ") + + def noquotes(self): + return self.context.replace('"', '') + + def breaks(self): + return self.context.replace('|', '\n')