src/ztfy/utils/tal/html.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 73 96079b5bdc1f
equal deleted inserted replaced
147:044dc196ec8a 148:d3668ecd9137
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 __docformat__ = "restructuredtext"
       
    17 
       
    18 # import standard packages
       
    19 
       
    20 # import Zope3 interfaces
       
    21 from zope.tales.interfaces import ITALESFunctionNamespace
       
    22 
       
    23 # import local interfaces
       
    24 from ztfy.utils.tal.interfaces import IHTMLTalesAPI
       
    25 
       
    26 # import Zope3 packages
       
    27 from zope.interface import implements
       
    28 
       
    29 # import local packages
       
    30 from ztfy.utils.html import htmlToText
       
    31 from ztfy.utils.text import textToHTML, Renderer
       
    32 
       
    33 
       
    34 class HTMLTalesAdapter(object):
       
    35 
       
    36     implements(IHTMLTalesAPI, ITALESFunctionNamespace)
       
    37 
       
    38     def __init__(self, context):
       
    39         self.context = context
       
    40 
       
    41     def setEngine(self, engine):
       
    42         self.request = engine.vars['request']
       
    43 
       
    44     def renderer(self):
       
    45         return Renderer(self.context)
       
    46 
       
    47     def totext(self):
       
    48         if not self.context:
       
    49             return u''
       
    50         return htmlToText(self.context)
       
    51 
       
    52     def text(self):
       
    53         if not self.context:
       
    54             return u''
       
    55         return textToHTML(self.context, 'zope.source.plaintext')
       
    56 
       
    57     def stx(self):
       
    58         if not self.context:
       
    59             return u''
       
    60         return textToHTML(self.context, 'zope.source.stx')
       
    61 
       
    62     def rest(self):
       
    63         if not self.context:
       
    64             return u''
       
    65         return textToHTML(self.context, 'zope.source.rest')