def variable(self, mo): canmiss = False missabort = False key = mo.group(2) # Okay, straightforward must-be-present stuff is # somewhat lacking in features. if key[0] == '?': canmiss = True key = key[1:] elif key[0] == '!': missabort = True key = key[1:] if key and key[0] == '|': key = key[1:] ovars = key.split('|') if not ovars: raise derrors.RendErr("invalid key '%s'" % mo.group(1)) for ov in ovars: if ov in self.context and self.context[ov]: vv = self.context[ov] return httputil.quotehtml(vv) elif not key: raise derrors.RendErr("invalid key '%s'" % mo.group(1)) else: if key in self.context and self.context[key]: return httputil.quotehtml(self.context[key]) # Error if we have to have a value (normal case). if canmiss: return '' elif missabort: raise ReturnNothing("variable expansion empty") raise derrors.RendErr("key with no value: '%s'" % mo.group(1))
def validate_template(to, fail_on_error, tname): if not fail_on_error: if not (to.displayable() and to.type == "file"): return None else: return to # Errors: if not to.exists(): raise derrors.IOErr("template '%s' does not exist" % tname) if not to.displayable(): raise derrors.RendErr("template %s is not displayable" % tname) if to.type != "file": raise derrors.RendErr("template %s is not a file (is a %s)" % (tname, to.type)) # all okay, go go go. return to
def expand_tname(self, namestr): ns = namestr.split('/') # We cannot use utils.goodpath() because '...' is not a good # path. if not namestr or '' in ns: raise derrors.RendErr("badly formed template name '%s'" % namestr) # If there is no '...' operator, all we have to do is expand # variables (once; there is no nested variable expansion). if '...' not in namestr: return [exp_varpat.sub(self._exp_var, namestr)] # There's a '...'. We must expand variables in components, # then actually do expansion. ns2 = [exp_varpat.sub(self._exp_var, x) for x in ns] return self._exp_piece([''], ns2)
def include(self, mo): def _tsplit(t): tpl = t.split('|') if not tpl: raise derrors.RendErr("badly formed template '%s'" % mo.group(1)) return tpl template = mo.group(2) if template[0] == '|': # Multi-include that picks the first one to generate # content. for t in _tsplit(template[1:]): res = self.template(t) if res: return res return '' elif template[0] == "?": # If first generates content, expand all. tpl = _tsplit(template[1:]) res = self.template(tpl[0]) if res: rl = [res] for t in tpl[1:]: rl.append(self.template(t)) return ''.join(rl) else: return '' elif template[0] in ('!', '<'): # '<' behaves like '|' and '?': if we don't find # anything, we return empty. '!' errors on it. # .expand_tnames() throws away nonexistent # templates for us, so this is simple. r = self.expand_tnames(template[1:]) if r: return self.template(r[0]) if template[0] == '!': raise derrors.RendErr("Unfound template in: "+mo.group(0)) else: return '' else: # Oh look, it's a *simple* case! return self.template(template)
def template(self, template): to = self.context.model.get_template(template) if not to: raise derrors.RendErr("unknown template '%s'" % template) res = Template(to).render(self.context) # The timestamps of templates are only considered # relevant if they expand to something. This is iffy, # but we can't win either way and this way is friendlier. # (The other way kicks *everything* any time a rarely # rendered template is updated; I would rather make # Last-Modified timestamps more useful.) if res: self.context.newtime(to.timestamp()) # The final trailing newline in a file is an # implementation artifact. Because it makes things nicer # and closer to what the template 'should' look like if # the file's real text was inserted, we remove it. if res and res[-1] == '\n': return res[:-1] else: return res
def get_renderer(name): if name in reg_renderers: return reg_renderers[name] else: raise derrors.RendErr("renderer '%s' not available" % name)
def renderer(self, mo): actor = mo.group(2) if not actor.strip(): raise derrors.RendErr("badly formed renderer macro: "+mo.group(1)) rfunc = htmlrends.get_renderer(actor) return rfunc(self.context)
def _exp_var(self, mo): varname = mo.group(1) if varname not in self.context: raise derrors.RendErr("Bad variable name in template name: '%s'" % varname) return self.context[varname]
def _tsplit(t): tpl = t.split('|') if not tpl: raise derrors.RendErr("badly formed template '%s'" % mo.group(1)) return tpl