source: products/quintagroup.pagetemplateshrink/trunk/quintagroup/pagetemplateshrink/__init__.py @ 1552

Last change on this file since 1552 was 1546, checked in by chervol, 14 years ago

added missing import

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1from zope.tal.htmltalparser import HTMLTALParser
2from zope.tal.talgenerator import TALGenerator
3from zope.tal.talparser import TALParser
4from zope.pagetemplate import pagetemplate
5
6import re
7expression = re.compile('[ \n\f\r]+(?=</?(?:me)?tal)')
8expression2 = re.compile('\n\s*\n+(?= *<)')
9
10def cook(self):
11    """Compile the TAL and METAL statments.
12
13    Cooking must not fail due to compilation errors in templates.
14    """
15    engine = self.pt_getEngine()
16    source_file = self.pt_source_file()
17    if self.content_type == 'text/html':
18        gen = TALGenerator(engine, xml=0, source_file=source_file)
19        parser = HTMLTALParser(gen)
20    else:
21        gen = TALGenerator(engine, source_file=source_file)
22        parser = TALParser(gen)
23
24    self._v_errors = ()
25    try:
26        #### the patch
27        text = self._text
28        text = expression.sub('', text)
29        text = expression2.sub('', text)
30        parser.parseString(text)
31        #parser.parseString(self._text)
32        self._v_program, self._v_macros = parser.getCode()
33    except:
34        self._v_errors = ["Compilation failed",
35                          "%s: %s" % sys.exc_info()[:2]]
36    self._v_warnings = parser.getWarnings()
37    self._v_cooked = 1
38
39pagetemplate.PageTemplate._old_cook = pagetemplate.PageTemplate._cook
40pagetemplate.PageTemplate._cook = cook
Note: See TracBrowser for help on using the repository browser.