# HG changeset patch # User Vadim Gelfer # Date 1141016017 28800 # Node ID 888d298ddb9147dea392143c3c05f4e34f8afcd7 # Parent e517189f168dac9047882e20b2307d02ef860ac0 many small changes to templater. get string code to parse escapes. uses eval now, should parse strings itself soon. let caller check if fragment is defined using "in". make templatepath take optional file name. diff -r e517189f168d -r 888d298ddb91 mercurial/templater.py --- a/mercurial/templater.py Sun Feb 26 13:11:53 2006 -0800 +++ b/mercurial/templater.py Sun Feb 26 20:53:37 2006 -0800 @@ -10,9 +10,9 @@ self.defaults = defaults for l in file(mapfile): - m = re.match(r'(\S+)\s*=\s*"(.*)"$', l) + m = re.match(r'(\S+)\s*=\s*(".*"|\'.*\')$', l) if m: - self.cache[m.group(1)] = m.group(2) + self.cache[m.group(1)] = eval(m.group(2)) else: m = re.match(r'(\S+)\s*=\s*(\S+)', l) if m: @@ -20,6 +20,9 @@ else: raise LookupError(_("unknown map entry '%s'") % l) + def __contains__(self, key): + return key in self.cache + def __call__(self, t, **map): m = self.defaults.copy() m.update(map) @@ -31,7 +34,9 @@ def template(self, tmpl, filters={}, **map): while tmpl: - m = re.search(r"#([a-zA-Z0-9]+)((%[a-zA-Z0-9]+)*)((\|[a-zA-Z0-9]+)*)#", tmpl) + m = re.search(r"#([a-zA-Z_][a-zA-Z0-9_]*)" + r"((%[a-zA-Z_][a-zA-Z0-9_]*)*)" + r"((\|[a-zA-Z_][a-zA-Z0-9_]*)*)#", tmpl) if m: yield tmpl[:m.start(0)] v = map.get(m.group(1), "") @@ -106,8 +111,10 @@ "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), } -def templatepath(): - for f in "templates", "../templates": - p = os.path.join(os.path.dirname(__file__), f) - if os.path.isdir(p): +def templatepath(name=None): + for f in 'templates', '../templates': + fl = f.split('/') + if name: fl.append(name) + p = os.path.join(os.path.dirname(__file__), *fl) + if (name and os.path.exists(p)) or os.path.isdir(p): return os.path.normpath(p)