ztfy/utils/tal/text.py
changeset 30 3c237f16b603
child 73 96079b5bdc1f
equal deleted inserted replaced
29:035058514c2d 30:3c237f16b603
       
     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 interfaces import ITextStartTalesAPI, ITextOutputTalesAPI
       
    25 
       
    26 # import Zope3 packages
       
    27 from zope.interface import implements
       
    28 
       
    29 # import local packages
       
    30 from ztfy.utils.text import textStart
       
    31 
       
    32 
       
    33 class TextStartTalesAdapter(object):
       
    34 
       
    35     implements(ITextStartTalesAPI, ITALESFunctionNamespace)
       
    36 
       
    37     def __init__(self, context):
       
    38         self.context = context
       
    39 
       
    40     def setEngine(self, engine):
       
    41         self.request = engine.vars['request']
       
    42 
       
    43     def __getattr__(self, attr):
       
    44         try:
       
    45             if attr.find(',') > 0:
       
    46                 length, max = (int(x) for x in attr.split(','))
       
    47             else:
       
    48                 length = int(attr)
       
    49                 max = 0
       
    50             return textStart(self.context, length, max)
       
    51         except:
       
    52             return ''
       
    53 
       
    54 
       
    55 class TextOutputTalesAdapter(object):
       
    56 
       
    57     implements(ITextOutputTalesAPI, ITALESFunctionNamespace)
       
    58 
       
    59     def __init__(self, context):
       
    60         self.context = context
       
    61 
       
    62     def setEngine(self, engine):
       
    63         self.request = engine.vars['request']
       
    64 
       
    65     def js(self):
       
    66         return self.context.replace("'", "&#039;").replace("\n", "&#013;")
       
    67 
       
    68     def noquotes(self):
       
    69         return self.context.replace('"', '')
       
    70 
       
    71     def breaks(self):
       
    72         return self.context.replace('|', '\n')