############################################################################## # # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. # # This software is subject to the provisions of the Zope Public License, # Version 2.0 (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 # ############################################################################## # Modified for Roundup: # # 1. changed imports to import from roundup.cgi # 2. removed use of ExtensionClass # 3. removed use of ComputedAttribute """Page Template module HTML- and XML-based template objects using TAL, TALES, and METAL. """ __version__='$Revision: 1.5 $'[11:-2] import sys from roundup.cgi.TAL.TALParser import TALParser from roundup.cgi.TAL.HTMLTALParser import HTMLTALParser from roundup.cgi.TAL.TALGenerator import TALGenerator # Do not use cStringIO here! It's not unicode aware. :( from roundup.cgi.TAL.TALInterpreter import TALInterpreter, FasterStringIO from Expressions import getEngine class PageTemplate: "Page Templates using TAL, TALES, and METAL" content_type = 'text/html' expand = 0 _v_errors = () _v_warnings = () _v_program = None _v_macros = None _v_cooked = 0 id = '(unknown)' _text = '' _error_start = '') if errend >= 0: text = text[errend + 4:] if self._text != text: self._text = text self._cook() def read(self): self._cook_check() if not self._v_errors: if not self.expand: return self._text try: return self.pt_render(source=1) except: return ('%s\n Macro expansion failed\n %s\n-->\n%s' % (self._error_start, "%s: %s" % sys.exc_info()[:2], self._text) ) return ('%s\n %s\n-->\n%s' % (self._error_start, '\n '.join(self._v_errors), self._text)) def _cook_check(self): if not self._v_cooked: self._cook() def _cook(self): """Compile the TAL and METAL statments. Cooking must not fail due to compilation errors in templates. """ source_file = self.pt_source_file() if self.html(): gen = TALGenerator(getEngine(), xml=0, source_file=source_file) parser = HTMLTALParser(gen) else: gen = TALGenerator(getEngine(), source_file=source_file) parser = TALParser(gen) self._v_errors = () try: parser.parseString(self._text) self._v_program, self._v_macros = parser.getCode() except: self._v_errors = ["Compilation failed", "%s: %s" % sys.exc_info()[:2]] self._v_warnings = parser.getWarnings() self._v_cooked = 1 def html(self): if not hasattr(getattr(self, 'aq_base', self), 'is_html'): return self.content_type == 'text/html' return self.is_html class _ModuleImporter: def __getitem__(self, module): mod = __import__(module) path = module.split('.') for name in path[1:]: mod = getattr(mod, name) return mod ModuleImporter = _ModuleImporter() class PTRuntimeError(RuntimeError): '''The Page Template has template errors that prevent it from rendering.''' pass class PageTemplateTracebackSupplement: #__implements__ = ITracebackSupplement def __init__(self, pt): self.object = pt w = pt.pt_warnings() e = pt.pt_errors() if e: w = list(w) + list(e) self.warnings = w