# HG changeset patch # User Matt Mackall # Date 2012-09-24 20:26:56 # Node ID ce18dbcd91c8d307079a49d3ba2aaf4a3e76c30b # Parent 8804e3cb51bdff3bd41f5fcf1b1628d828b12a3a templater: add if/ifeq conditionals diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -227,6 +227,31 @@ def sub(context, mapping, args): src = stringify(args[2][0](context, mapping, args[2][1])) yield re.sub(pat, rpl, src) +def if_(context, mapping, args): + if not (2 <= len(args) <= 3): + raise error.ParseError(_("if expects two or three arguments")) + + test = stringify(args[0][0](context, mapping, args[0][1])) + if test: + t = stringify(args[1][0](context, mapping, args[1][1])) + yield runtemplate(context, mapping, compiletemplate(t, context)) + elif len(args) == 3: + t = stringify(args[2][0](context, mapping, args[2][1])) + yield runtemplate(context, mapping, compiletemplate(t, context)) + +def ifeq(context, mapping, args): + if not (3 <= len(args) <= 4): + raise error.ParseError(_("ifeq expects three or four arguments")) + + test = stringify(args[0][0](context, mapping, args[0][1])) + match = stringify(args[1][0](context, mapping, args[1][1])) + if test == match: + t = stringify(args[2][0](context, mapping, args[2][1])) + yield runtemplate(context, mapping, compiletemplate(t, context)) + elif len(args) == 4: + t = stringify(args[3][0](context, mapping, args[3][1])) + yield runtemplate(context, mapping, compiletemplate(t, context)) + methods = { "string": lambda e, c: (runstring, e[1]), "symbol": lambda e, c: (runsymbol, e[1]), @@ -238,6 +263,8 @@ methods = { } funcs = { + "if": if_, + "ifeq": ifeq, "join": join, "sub": sub, }