1 | from zope.tal.htmltalparser import HTMLTALParser |
---|
2 | from zope.tal.talgenerator import TALGenerator |
---|
3 | from zope.tal.talparser import TALParser |
---|
4 | from zope.pagetemplate import pagetemplate |
---|
5 | |
---|
6 | import re |
---|
7 | expression = re.compile('[ \n\f\r]+(?=</?(?:me)?tal)') |
---|
8 | expression2 = re.compile('\n\s*\n+(?= *<)') |
---|
9 | |
---|
10 | def 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 | |
---|
39 | pagetemplate.PageTemplate._old_cook = pagetemplate.PageTemplate._cook |
---|
40 | pagetemplate.PageTemplate._cook = cook |
---|