Mercurial > hg-stable
changeset 1899:888d298ddb91
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.
author | Vadim Gelfer <vadim.gelfer@gmail.com> |
---|---|
date | Sun, 26 Feb 2006 20:53:37 -0800 |
parents | e517189f168d |
children | f2815605186e |
files | mercurial/templater.py |
diffstat | 1 files changed, 14 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- 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)