src/ztfy/utils/tal/text.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 136 aab380e3c821
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 ITextStartTalesAPI, ITextOutputTalesAPI
       
    25 
       
    26 # import Zope3 packages
       
    27 from zope.i18n import translate
       
    28 from zope.interface import implements
       
    29 
       
    30 # import local packages
       
    31 from ztfy.utils.text import textStart
       
    32 
       
    33 
       
    34 class TextStartTalesAdapter(object):
       
    35 
       
    36     implements(ITextStartTalesAPI, 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 __getattr__(self, attr):
       
    45         try:
       
    46             if attr.find(',') > 0:
       
    47                 length, max = (int(x) for x in attr.split(','))
       
    48             else:
       
    49                 length = int(attr)
       
    50                 max = 0
       
    51             return textStart(self.context, length, max)
       
    52         except:
       
    53             return ''
       
    54 
       
    55 
       
    56 class TextOutputTalesAdapter(object):
       
    57 
       
    58     implements(ITextOutputTalesAPI, ITALESFunctionNamespace)
       
    59 
       
    60     def __init__(self, context):
       
    61         self.context = context
       
    62 
       
    63     def setEngine(self, engine):
       
    64         self.request = engine.vars['request']
       
    65 
       
    66     def js(self):
       
    67         return self.context.replace("'", "&#039;").replace("\n", "&#013;")
       
    68 
       
    69     def noquotes(self):
       
    70         return self.context.replace('"', '')
       
    71 
       
    72     def breaks(self):
       
    73         return self.context.replace('|', '\n')
       
    74 
       
    75     def translate(self):
       
    76         return translate(self.context, context=self.request)