1
2
3
4
5
6
7
8
9
10
11
12
13 import cgi
14 import re
15 import cStringIO
16 from Products.PageTemplates.Expressions import getEngine
17 from zope.tal.htmltalparser import HTMLTALParser
18 from zope.tal.talgenerator import TALGenerator
19 from zope.tales.engine import Engine
20 from zope.tal.talinterpreter import TALInterpreter
21 from DateTime import DateTime
22
23 _compiled = {}
24
27
28
30 """Perform a TALES eval on the express using context.
31 """
32 compiled = talesCompile(express)
33 contextDict = { 'here':context,
34 'nothing':None,
35 'now': DateTime(),
36 }
37 if isinstance(extra, dict):
38 contextDict.update(extra)
39 res = compiled(getEngine().getContext(contextDict))
40 if isinstance(res, Exception):
41 raise res
42 return res
43
45 compiled = _compiled.get(express, None)
46 if not compiled:
47 _compiled[express] = compiled = getEngine().compile(express)
48 return compiled
49
50 TAG = re.compile(r'(<tal[^<>]>)')
51 TPLBLOCK = re.compile(r'\$\{(.*?)\}')
52
54 """
55 Need this to escape quotes and <> in expressions
56 """
57 interior = cgi.escape(match.groups()[0], True)
58 return '<tal:block content="%s"/>' % interior
59
60 -def talEval(expression, context, extra=None):
61 """
62 Perform a TAL eval on the expression.
63 """
64
65
66
67 isTales = '<tal' not in expression and '${python:' not in expression
68 if isTales:
69 return talesEvalStr(expression, context, extra)
70
71
72
73 chunks = TAG.split(expression)
74 modified = []
75 for chunk in chunks:
76 if chunk.startswith('<tal'):
77 modified.append(chunk)
78 else:
79 modified.append(TPLBLOCK.sub(_chunk_repl, chunk))
80 expression = ''.join(modified)
81
82
83 gen = TALGenerator(Engine, xml=0)
84 parser = HTMLTALParser(gen)
85 parser.parseString(expression)
86 program, macros = parser.getCode()
87 output = cStringIO.StringIO()
88 context = Engine.getContext(context)
89 TALInterpreter(program, macros, context, output, tal=True)()
90 return output.getvalue()
91