skip to navigation
skip to content

Not Logged In

lovely.tal 0.6.0

the lovely tal enables new tal expressions

==========
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
  beeing replaced with ``< ``, ``>``

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``

option urlparse:
  parsing of http:// or www. strings to hyperlinks, got a dictonary of parameters
  e.g. urlparse python:{'rel':'nofollow','target':'_blank', allready existing
  anchor tags are extended with the parameters in the dictionary, image tags
  stay untouched in this whole parsing process

Example::

  Description

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('foolink', context)
  '<a href="#" name="foolink">foolink</a>'
  >>> tf._doFormat('foolink
' ... '
', context) '<a href="#" name="foolink">foolink</a><br /><form action="."><input type="text" /></form>' if we provide an empty context, the textformatter translates all html-tags to ``< >`` Option 'allow' ============== We can allow certain html-tags in the text:: >>> context = Context({'allow':['a']}) >>> tf._doFormat('foolink
', context) 'foolink<br /><form action="."><input type="text" /></form>' >>> context = Context({'allow':['a', 'br']}) >>> tf._doFormat('foolink
', context) 'foolink
<form action="."><input type="text" /></form>' >>> context = Context({'allow':['a', 'br', 'form']}) >>> tf._doFormat('foolink
', context) 'foolink
<input type="text" />
' 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
< form action=".">', context) '< a href="#" name="foolink">foolink</ a>
< form action="."><input type="text" /></form >' 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
< form action=".">', context) '< a href="#" name="foolink">foolink</ a>
< form action="."><input type="text" /></form >' Option 'allow-all' ================== We can allow all html-tags:: >>> context = Context({'allow-all':True}) >>> tf._doFormat('foolink
', context) 'foolink
' Option 'replace' ================ We can replace characters or strings, e.g. we would like to replace the '\n' character by '
' to display the text properly:: >>> context = Context({'replace':[('\n', '
')]}) >>> tf._doFormat('das Schwein, \n das aus der Wueste kam', context) 'das Schwein,
das aus der Wueste kam' we can also replace strings:: >>> context = Context({'replace':[('\n', '
'), ('Schwein', 'Kamel')]}) >>> tf._doFormat('das Schwein, \n das aus der Wueste kam', context) 'das Kamel,
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
Schwein,
das aus
der
Wueste
kam' >>> context = Context({'break-string':8}) >>> tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context) 'ein
superlan
gerstrin
g mit
ein paar
kurzen
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('
', '\n') 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 = Context({'break-string':20, 'allow':['br']}) >>> text = u'eins zwei drei vier fuenf sechs sieben,
' >>> text += u'in der Schule wird geschrieben,
' >>> text += u'in der Schule wird gelacht,
' >>> text += u'bis der Lehrer pitschpatsch macht!' >>> res = tf._doFormat(text, context) >>> print res.replace('
', '\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 lovelysystems is great!', context) 'working
at
lovelysy
stems
is
great!' Option 'cut' ============ We can also cut strings to a given length:: Warning: cut will not check for HTML tags and will therefore cut in the middle of a tag which will make HTML unusable. Only use for plain text. >>> context = Context({'cut':20}) >>> rendered = tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context) >>> len(rendered) 20 cut is done as the first operation. If it is combined with replace the resulting string can be longer. >>> context = Context({'cut':20, 'replace':(('ein', 'Wrong case : ein'),)}) >>> rendered = tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context) >>> len(rendered) 33 Option 'clear-html' =================== >>> context = Context({'clear-html':True}) >>> tf._doFormat('Text containing HTML', context) 'Text containing HTML' This is done before "cut". >>> context = Context({'clear-html':True, 'cut':10}) >>> tf._doFormat('Text containing HTML', context) 'Text conta' 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...' Option 'softcut' =============== the option softcut works together with cut and prevents cutting words:: >>> context = Context({'cut':20, 'attach':'…', 'softcut':True}) >>> tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context) 'ein…' >>> context = Context({'cut':25, 'attach':'…', 'softcut':True}) >>> tf._doFormat('ein superlangerstring mit ein paar kurzen strings', context) 'ein superlangerstring…' Option 'allow-scripts' ====================== the option 'allow-scripts' has to be set explicitly if you want to include scripts. >>> context = Context({'allow-all':True, 'allow-scripts':True}) >>> html = """

this is html containing a ... script. ...

""" >>> print tf._doFormat(html, context)

this is html containing a script.

if not, all scripts will be stripped out although allow-all is enabled:: >>> context = Context({'allow-all':True}) >>> html = """

this is html containing a ... script. ...

""" >>> print tf._doFormat(html, context)

this is html containing a script.

test uppercase and whitespaces:: >>> html = """

this is html containing a ... < SCRIPT ... type="text/javascript"> ... alert("i'm not allowed'); ... < /SCRIPT > script. ...

""" >>> print tf._doFormat(html, context)

this is html containing a script.

escaped scripttags:: >>> html = """

this is html containing a ... <script type="text/javascript"> ... alert("i'm not allowed'); ... </SCRIPT> script. ...

""" >>> print tf._doFormat(html, context)

this is html containing a script.

escaped scripts including whitespace and different case:: >>> html = """

this is html containing a ... <SCRIPT ... type="text/javascript"> ... alert("i'm not allowed'); ... < /SCRIPT > script. ...

""" >>> print tf._doFormat(html, context)

this is html containing a script.

Option 'urlparse' ================= parse the urls in the expression: >>> context = Context({}) >>> context = Context({'urlparse':{'rel':'nofollow','target':'_blank'},'allow':['a', 'br']}) >>> tf._doFormat('lovelysystems rocks your zope', context) 'lovelysystems rocks your zope' >>> tf._doFormat('ha ha hell yeah http://www.lovelysystems.com/ rocks your zope', context) '...http://www.lovelysystems.com/...' >>> tf._doFormat('ha ha hell yeah www.lovelysystems.com/ rocks your zope', context) '...www.lovelysystems.com/...' >>> tf._doFormat('ha ha hell yeah rocks your zope', context) '...<img src="http://www.lovelysystems.com/image.jpg" />...'
 
File Type Py Version Uploaded on Size
lovely.tal-0.6.0.tar.gz (md5) Source 2009-02-03 12KB
  • Downloads (All Versions):
  • 18 downloads in the last day
  • 113 downloads in the last week
  • 395 downloads in the last month