[Checkins] SVN: lovely.tal/trunk/ initial checkin

Manfred Schwendinger manfred.schwendiger at lovelysystems.com
Mon Dec 10 06:04:17 EST 2007


Log message for revision 82216:
  initial checkin

Changed:
  A   lovely.tal/trunk/
  A   lovely.tal/trunk/CHANGES.txt
  A   lovely.tal/trunk/LICENSE.txt
  A   lovely.tal/trunk/bootstrap.py
  A   lovely.tal/trunk/buildout.cfg
  A   lovely.tal/trunk/setup.py
  A   lovely.tal/trunk/src/
  A   lovely.tal/trunk/src/lovely/
  A   lovely.tal/trunk/src/lovely/__init__.py
  A   lovely.tal/trunk/src/lovely/tal/
  A   lovely.tal/trunk/src/lovely/tal/LICENSE.txt
  A   lovely.tal/trunk/src/lovely/tal/README.txt
  A   lovely.tal/trunk/src/lovely/tal/SETUP.cfg
  A   lovely.tal/trunk/src/lovely/tal/__init__.py
  A   lovely.tal/trunk/src/lovely/tal/configure.zcml
  A   lovely.tal/trunk/src/lovely/tal/lovely.tal-configure.zcml
  A   lovely.tal/trunk/src/lovely/tal/sample.pt
  A   lovely.tal/trunk/src/lovely/tal/tests.py
  A   lovely.tal/trunk/src/lovely/tal/textformatter.py

-=-
Added: lovely.tal/trunk/CHANGES.txt
===================================================================
--- lovely.tal/trunk/CHANGES.txt	                        (rev 0)
+++ lovely.tal/trunk/CHANGES.txt	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,13 @@
+==========
+lovely.tal
+==========
+
+2007/08/29 0.2.1:
+=================
+ - prepare checkin on zope.org
+
+2007/08/29 0.2.0:
+=================
+
+ - increased version number to get rid of "dev" revision
+ - changed buildout evironment


Property changes on: lovely.tal/trunk/CHANGES.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/LICENSE.txt
===================================================================
--- lovely.tal/trunk/LICENSE.txt	                        (rev 0)
+++ lovely.tal/trunk/LICENSE.txt	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,42 @@
+ZPL 2.1
+Zope Public License (ZPL) Version 2.1
+
+A copyright notice accompanies this license document that identifies
+the copyright holders.
+
+This license has been certified as open source. It has also been
+designated as GPL compatible by the Free Software Foundation (FSF).
+
+Redistribution and use in source and binary forms,
+with or without modification, are permitted provided that the following
+conditions are met:
+
+Redistributions in source code must retain the accompanying copyright
+notice, this list of conditions, and the following disclaimer.
+Redistributions in binary form must reproduce the accompanying
+copyright notice, this list of conditions, and the following
+disclaimer in the documentation and/or other materials provided
+with the distribution.
+Names of the copyright holders must not be used to endorse or
+promote products derived from this software without prior written
+permission from the copyright holders.
+The right to distribute this software or to use it for any purpose
+does not give you the right to use Servicemarks (sm) or Trademarks (tm)
+of the copyright holders. Use of them is covered by separate
+agreement with the copyright holders.
+If any files are modified, you must cause the modified files to
+carry prominent notices stating that you changed the files and
+the date of any change.
+
+Disclaimer
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Property changes on: lovely.tal/trunk/LICENSE.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/bootstrap.py
===================================================================
--- lovely.tal/trunk/bootstrap.py	                        (rev 0)
+++ lovely.tal/trunk/bootstrap.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,47 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+import os, shutil, sys, tempfile, urllib2
+
+tmpeggs = tempfile.mkdtemp()
+
+ez = {}
+exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                     ).read() in ez
+ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+
+import pkg_resources
+
+cmd = 'from setuptools.command.easy_install import main; main()'
+if sys.platform == 'win32':
+    cmd = '"%s"' % cmd # work around spawn lamosity on windows
+
+ws = pkg_resources.working_set
+assert os.spawnle(
+    os.P_WAIT, sys.executable, sys.executable,
+    '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
+    dict(os.environ,
+         PYTHONPATH=
+         ws.find(pkg_resources.Requirement.parse('setuptools')).location
+         ),
+    ) == 0
+
+ws.add_entry(tmpeggs)
+ws.require('zc.buildout')
+import zc.buildout.buildout
+zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
+shutil.rmtree(tmpeggs)


Property changes on: lovely.tal/trunk/bootstrap.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/buildout.cfg
===================================================================
--- lovely.tal/trunk/buildout.cfg	                        (rev 0)
+++ lovely.tal/trunk/buildout.cfg	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,8 @@
+[buildout]
+develop = .
+parts = test
+find-links = https://download.lovelysystems.com/eggs/lovely
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = lovely.tal [test]
\ No newline at end of file

Added: lovely.tal/trunk/setup.py
===================================================================
--- lovely.tal/trunk/setup.py	                        (rev 0)
+++ lovely.tal/trunk/setup.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,43 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+__docformat__ = "reStructuredText"
+
+
+from setuptools import setup, find_packages
+
+setup(
+    name = 'lovely.tal',
+    version = '0.2.1',
+    author = "Lovely Systems",
+    author_email = "office at lovelysystems.com",
+    license = "ZPL. see LICENSE.txt",
+    keywords = "tal zope zope3",
+    url = 'svn://svn.zope.org/repos/main/lovely.tal',
+    zip_safe = False,
+    packages = find_packages('src'),
+    include_package_data = True,
+    package_dir = {'':'src'},
+    namespace_packages = ['lovely',],
+    install_requires = ['setuptools',
+                        'zope.tales',
+                        ],
+    extras_require = dict(
+        test = ['zope.app.testing',
+                'zope.testing',]),
+    )
+


Property changes on: lovely.tal/trunk/setup.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/__init__.py
===================================================================
--- lovely.tal/trunk/src/lovely/__init__.py	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/__init__.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,24 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+__docformat__ = "reStructuredText"
+
+
+try:
+    __import__('pkg_resources').declare_namespace(__name__)
+except ImportError:
+    pass


Property changes on: lovely.tal/trunk/src/lovely/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/LICENSE.txt
===================================================================
--- lovely.tal/trunk/src/lovely/tal/LICENSE.txt	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/LICENSE.txt	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,42 @@
+ZPL 2.1
+Zope Public License (ZPL) Version 2.1
+
+A copyright notice accompanies this license document that identifies
+the copyright holders.
+
+This license has been certified as open source. It has also been
+designated as GPL compatible by the Free Software Foundation (FSF).
+
+Redistribution and use in source and binary forms,
+with or without modification, are permitted provided that the following
+conditions are met:
+
+Redistributions in source code must retain the accompanying copyright
+notice, this list of conditions, and the following disclaimer.
+Redistributions in binary form must reproduce the accompanying
+copyright notice, this list of conditions, and the following
+disclaimer in the documentation and/or other materials provided
+with the distribution.
+Names of the copyright holders must not be used to endorse or
+promote products derived from this software without prior written
+permission from the copyright holders.
+The right to distribute this software or to use it for any purpose
+does not give you the right to use Servicemarks (sm) or Trademarks (tm)
+of the copyright holders. Use of them is covered by separate
+agreement with the copyright holders.
+If any files are modified, you must cause the modified files to
+carry prominent notices stating that you changed the files and
+the date of any change.
+
+Disclaimer
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Property changes on: lovely.tal/trunk/src/lovely/tal/LICENSE.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/README.txt
===================================================================
--- lovely.tal/trunk/src/lovely/tal/README.txt	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/README.txt	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,206 @@
+==========
+lovely tal
+==========
+
+the lovely tal package is meant to contain new tal:expressions
+
+
+
+TextFormatter
+-------------
+
+   option replace: takes a list of tuples, which characters or strings should be
+                   replaced by what, e.g. 
+                   "replace python:[(origChar, repChar), (origChar2, repChar2), ...]"
+                   
+   option allow:   takes a list of html-tags which shall be allowed in the string
+                   e.g. "allow python:['a', 'br', 'ul', 'li']"
+                   if this option is not set, the string is restricted to
+                   contain no html-tags, therefor the < and > are replaced 
+                   by &lt;, &gt;
+                   
+   option allow-all: allow all html-tags in the string
+   					 e. g. "allow-all: 'True'"
+   
+   option break-string: force the string to break after a given number of characters
+   						e.g. "break-string python:25" breaks the string after 
+   						a sequence of 25 characters not containing a linebreak
+   						
+   option cut: cuts a string to the given length
+   option attach: works only together with option 'cut', 
+                  attaches the given string to the expression, if this is longer than
+                  number of characters given in option 'cut'
+   						
+<span tal:define="replace python:[('\n', '<br />')];
+                  allow python:['a', 'br'];
+                  break-string python:25;
+                  cut python 25;
+                  attach '...'"
+      tal:content="structure textFormatter: view/description">Description</span>
+      
+      
+
+Lets see if the TextFormatter does what we want him to.
+
+We have to fake a context object to call the textformatter
+
+  >>> class Context(object):
+  ...     vars = {}
+  ...     def __init__(self, vars):
+  ...         self.vars = vars
+
+  >>> from lovely.tal.textformatter import TextFormatter
+  >>> from zope.tales.expressions import simpleTraverse
+  >>> from zope.app.pagetemplate.engine import TrustedZopeEngine
+  >>> tf = TextFormatter('textFormatter', 'view/title', TrustedZopeEngine(), simpleTraverse)
+  >>> context = Context({})
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a>', context)
+  '&lt;a href="#" name="foolink"&gt;foolink&lt;/a&gt;'
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>', context)
+  '&lt;a href="#" name="foolink"&gt;foolink&lt;/a&gt;&lt;br /&gt;&lt;form action="."&gt;&lt;input type="text" /&gt;&lt;/form&gt;'
+  
+if we provide an empty context, the textformatter translates all html-tags to &lt; &gt;
+
+Option 'allow'
+==============
+
+We can allow certain html-tags in the text
+
+  >>> context = Context({'allow':['a']})
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>', context)
+  '<a href="#" name="foolink">foolink</a>&lt;br /&gt;&lt;form action="."&gt;&lt;input type="text" /&gt;&lt;/form&gt;'
+  
+  >>> context = Context({'allow':['a', 'br']})
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>', context)
+  '<a href="#" name="foolink">foolink</a><br />&lt;form action="."&gt;&lt;input type="text" /&gt;&lt;/form&gt;'
+  
+  >>> context = Context({'allow':['a', 'br', 'form']})
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>', context)
+  '<a href="#" name="foolink">foolink</a><br /><form action=".">&lt;input type="text" /&gt;</form>'
+
+In the above example, still the content of the form tag is translated 
+ 
+Lets try to write dirty html
+
+  >>> context = Context({'allow':['a', 'br', 'form']})
+  >>> tf._doFormat('< a href="#" name="foolink">foolink</ a><br/>< form action="."><input type="text" /></form >', context)
+  '&lt; a href="#" name="foolink"&gt;foolink&lt;/ a&gt;<br/>&lt; form action="."&gt;&lt;input type="text" /&gt;&lt;/form &gt;'
+
+Since the a-tag and the form-tag are not valid html, they are translated, although we declared them to be allowed  
+We get the same result if we do not allow them
+
+  >>> context = Context({'allow':['br']})
+  >>> tf._doFormat('< a href="#" name="foolink">foolink</ a><br/>< form action="."><input type="text" /></form >', context)
+  '&lt; a href="#" name="foolink"&gt;foolink&lt;/ a&gt;<br/>&lt; form action="."&gt;&lt;input type="text" /&gt;&lt;/form &gt;'
+
+
+Option 'allow-all'
+==================
+ 
+We can allow all html-tags
+
+  >>> context = Context({'allow-all':True})
+  >>> tf._doFormat('<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>', context)
+  '<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>'
+
+
+Option 'replace'
+================
+
+We can replace characters or strings, e.g. we would like to replace the '\n' character by '<br />'
+to display the text properly.
+
+  >>> context = Context({'replace':[('\n', '<br />')]})
+  >>> tf._doFormat('das Schwein, \n das aus der Wueste kam', context)
+  'das Schwein, <br /> das aus der Wueste kam'
+  
+we can also replace strings
+
+  >>> context = Context({'replace':[('\n', '<br />'), ('Schwein', 'Kamel')]})
+  >>> tf._doFormat('das Schwein, \n das aus der Wueste kam', context)
+  'das Kamel, <br /> das aus der Wueste kam'
+ 
+
+Option 'break-string'
+=====================
+
+Another option is to break strings after a given number of characters n, in case there
+was no break or '\s' in the last n characters.
+
+  >>> context = Context({'break-string':8})
+  >>> tf._doFormat('das Schwein, das aus der Wueste kam', context)
+  'das<br />Schwein,<br />das aus<br />der<br />Wueste<br />kam'
+  
+  >>> context = Context({'break-string':8})
+  >>> tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context)
+  'ein<br />superlan<br />gerstrin<br />g mit<br />ein paar<br />kurzen<br />strings'
+
+Also multi line text works.
+
+  >>> context = Context({'break-string':40})
+  >>> res = tf._doFormat("""
+  ... ein superlangerstring mit ein paar kurzen strings.
+  ... 
+  ... - another line
+  ... 
+  ... - another long string which needs to break
+  ... and this needs to break twice because it is longer than 80 characters, hopefully it works
+  ... """, context)
+  >>> print res.replace('<br />', '\n')
+  <BLANKLINE>
+  ein superlangerstring mit ein paar
+  kurzen strings.
+  <BLANKLINE>
+  - another line
+  <BLANKLINE>
+  - another long string which needs to
+  break
+  and this needs to break twice because it
+  is longer than 80 characters, hopefully
+  it works
+  <BLANKLINE>
+
+  >>> context = Context({'break-string':20, 'allow':['br']})
+  >>> text = u'eins zwei drei vier fuenf sechs sieben,<br />'
+  >>> text += u'in der Schule wird geschrieben,<br />'
+  >>> text += u'in der Schule wird gelacht,<br />'
+  >>> text += u'bis der Lehrer pitschpatsch macht!'
+  >>> res = tf._doFormat(text, context)
+  >>> print res.replace('<br />', '\n')
+  eins zwei drei vier
+  fuenf sechs sieben,
+  in der Schule wird
+  geschrieben,
+  in der Schule wird
+  gelacht,
+  bis der Lehrer
+  pitschpatsch macht!
+
+the formatter considers tags as not to be part of the text, that means that
+breaks aren't made inside tags (<...>)
+
+  >>> context = Context({'break-string':8, 'allow':['a']})
+  >>> tf._doFormat('working at <a href="www.lovelysystems.com" name="lovelysystems">lovelysystems</a> is great!', context)
+  'working<br />at<br /><a href="www.lovelysystems.com" name="lovelysystems">lovelysy<br />stems</a> is<br />great!'
+  
+
+Option 'cut'
+============  
+
+We can also cut strings to a given length
+
+  >>> context = Context({'cut':20})
+  >>> rendered = tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context)
+  >>> len(rendered) == 20
+  True
+  
+
+Option 'attach'
+===============
+
+and attach a string to the expression
+
+  >>> context = Context({'cut':20, 'attach':'...'})
+  >>> tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context)
+  'ein superlangerstrin...'
+ 


Property changes on: lovely.tal/trunk/src/lovely/tal/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/SETUP.cfg
===================================================================
--- lovely.tal/trunk/src/lovely/tal/SETUP.cfg	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/SETUP.cfg	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  lovely.tal-*.zcml
+</data-files>

Added: lovely.tal/trunk/src/lovely/tal/__init__.py
===================================================================
--- lovely.tal/trunk/src/lovely/tal/__init__.py	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/__init__.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""


Property changes on: lovely.tal/trunk/src/lovely/tal/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/configure.zcml
===================================================================
--- lovely.tal/trunk/src/lovely/tal/configure.zcml	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/configure.zcml	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,10 @@
+<configure
+	    xmlns:tales="http://namespaces.zope.org/tales">
+	 
+  <!-- registering the new tal-expression textFormatter -->
+  <tales:expressiontype
+      name="textFormatter"
+      handler=".textformatter.TextFormatter"
+      />   
+	    
+</configure>
\ No newline at end of file


Property changes on: lovely.tal/trunk/src/lovely/tal/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/lovely.tal-configure.zcml
===================================================================
--- lovely.tal/trunk/src/lovely/tal/lovely.tal-configure.zcml	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/lovely.tal-configure.zcml	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1 @@
+<include package="lovely.tal" />


Property changes on: lovely.tal/trunk/src/lovely/tal/lovely.tal-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/sample.pt
===================================================================
--- lovely.tal/trunk/src/lovely/tal/sample.pt	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/sample.pt	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,27 @@
+<!-- here is a sample of how to use textFormatter -->
+
+<!--
+   option replace: takes a list of tuples, which characters or strings shoul be
+                   replaced by what, e.g. 
+                   "replace python:[(origChar, repChar), (origChar2, repChar2), ...]"
+                   
+   option allow:   takes a list of html-tags which shall be allowed in the string
+                   e.g. "allow python:['a', 'br', 'ul', 'li']"
+                   if this option is not set, the string is restricted to
+                   contain no html-tags, therefor the < and > are replaced 
+                   by &lt;, &gt;
+                   
+   option allow-all: allow all html-tags in the string
+   					 e. g. "allow-all: 'True'"
+   
+   option break-string: force the string to break after a given number of characters
+   						e.g. "break-string python:25" breaks the string after 
+   						a sequence of 25 characters not containing a linebreak
+                   
+
+-->
+
+<span tal:define="replace python:[('\n', '<br />')];
+                  allow python:['a', 'br'];
+                  break-string python:25"
+      tal:content="structure textFormatter: view/description">Description</span>
\ No newline at end of file


Property changes on: lovely.tal/trunk/src/lovely/tal/sample.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/tests.py
===================================================================
--- lovely.tal/trunk/src/lovely/tal/tests.py	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/tests.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,44 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import doctest
+import unittest
+from zope.app.testing import setup
+from zope.testing.doctestunit import DocFileSuite
+
+def setUp(test):
+    setup.placefulSetUp()
+
+def tearDown(test):
+    setup.placefulTearDown()
+
+def test_suite():
+
+   suite = unittest.TestSuite(
+        (
+        DocFileSuite('README.txt',
+                     setUp=setUp,
+                     tearDown=tearDown,
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
+        ))
+
+   return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: lovely.tal/trunk/src/lovely/tal/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: lovely.tal/trunk/src/lovely/tal/textformatter.py
===================================================================
--- lovely.tal/trunk/src/lovely/tal/textformatter.py	                        (rev 0)
+++ lovely.tal/trunk/src/lovely/tal/textformatter.py	2007-12-10 11:04:16 UTC (rev 82216)
@@ -0,0 +1,153 @@
+##############################################################################
+#
+# Copyright (c) 2007 Lovely Systems and Contributors.
+# 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.
+#
+##############################################################################
+"""
+$Id$
+"""
+import re
+from zope.tales.expressions import PathExpr
+
+
+class TextFormatter(PathExpr):
+
+
+    def __call__(self, context):
+
+        rendered = super(TextFormatter, self).__call__(context)
+        return self._doFormat(rendered, context)
+
+    def _doFormat(self, rendered, context):
+
+        if rendered is None:
+            return rendered
+
+        allowAll = ('allow-all' in context.vars)
+
+        if 'replace' in context.vars:
+            rendered = self._replace(rendered, context)
+
+        if 'allow' in context.vars and not allowAll:
+            rendered = self._allow(rendered, context)
+
+        if not allowAll:
+            rendered = rendered.replace('<', '&lt;')
+            rendered = rendered.replace('>', '&gt;')
+        rendered = rendered.replace('__LOWER__', '<')
+        rendered = rendered.replace('__BIGGER__', '>')
+
+        if 'break-string' in context.vars:
+            rendered = self._breakString(rendered, context)
+
+        if 'cut' in context.vars:
+            rendered = self._cut(rendered, context)
+
+        return rendered
+
+    def _replace(self, rendered, context):
+        replace = context.vars['replace']
+        for rep in replace:
+            repOrig = rep[0]
+            repNew = rep[1]
+            repNew = repNew.replace('<', '__LOWER__')
+            repNew = repNew.replace('>', '__BIGGER__')
+            rendered = rendered.replace(repOrig, repNew)
+        return rendered
+
+    def _allow(self, rendered, context):
+        allow = context.vars['allow']
+        for al in allow:
+
+            # find tags with opening and closing tags like <a href="bla">foo</a>
+            reg = re.compile('<%s[^>]*>' %al, re.VERBOSE)
+            found = reg.findall(rendered)
+            for f in found:
+                f_rep = f.replace('<', '__LOWER__')
+                f_rep = f_rep.replace('>', '__BIGGER__')
+                rendered = rendered.replace(f, f_rep)
+
+            reg = re.compile('</%s>' %al, re.VERBOSE)
+            found = reg.findall(rendered)
+            for f in found:
+                f_rep = f.replace('<', '__LOWER__')
+                f_rep = f_rep.replace('>', '__BIGGER__')
+                rendered = rendered.replace(f, f_rep)
+
+
+            #find tags with no closing tag like <input type="submit" value="bla" />
+            reg = re.compile('<%s[^>]*/>' %al, re.VERBOSE)
+            found = reg.findall(rendered)
+            for f in found:
+                f_rep = f.replace('<', '__LOWER__')
+                f_rep = f_rep.replace('>', '__BIGGER__')
+                rendered = rendered.replace(f, f_rep)
+        return rendered
+
+    def _breakString(self, rendered, context):
+        lines = []
+        for line in re.split('<[b|B][r|R].?/>|\n', rendered):
+            lines.append(self._breakLine(line, context))
+        return '<br />'.join(lines)
+
+    def _breakLine(self, rendered, context):
+        realText = rendered
+        rendered = re.sub('<.*?>', '', rendered)
+        br = baseBr = context.vars['break-string']
+        while (br < len(rendered)):
+            start = br-baseBr
+            end = br
+            split = re.split('\s', rendered[start:end])
+            if len(split) > 1 and not re.search('\s', rendered[end]):
+                # at least one \s has been found
+                rep = rendered[start:end]
+                idx = rep.rfind(split[-1])
+                start = idx + start
+                realText = self._insert('<br />', realText, start)
+                br = start + baseBr
+
+            else:
+                # no break in the last baseBr characters
+                realText = self._insert('<br />', realText, br)
+                #rendered = rendered[:br] + '<br />' + rendered[br:]
+                br = br + baseBr
+
+        realText = re.sub('<br />\s', '<br />', realText)
+        realText = re.sub('\s<br />', '<br />', realText)
+        return realText
+
+    def _insert(self, expr, realText, position):
+        tags = list(re.finditer('<.*?>', realText))
+        if tags == []:
+            return realText[:position] + expr + realText[position:]
+        for tag in tags:
+            if not position > tag.start(): # a tag is before the position
+                break
+            position = position + len(tag.group())
+
+        realText = realText[:position] + expr + realText[position:]
+        return realText
+
+    def _cut(self, rendered, context):
+        cut = context.vars['cut']
+        if len(rendered) <= cut:
+            return rendered
+        rendered = rendered[:cut]
+        if 'attach' in context.vars:
+            rendered = self._attach(rendered, context)
+        return rendered
+
+    def _attach(self, rendered, context):
+        attach = context.vars['attach']
+        if attach is not None:
+            return rendered + attach
+        return rendered
+


Property changes on: lovely.tal/trunk/src/lovely/tal/textformatter.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list