diff mercurial/templater.py @ 17636:ce18dbcd91c8

templater: add if/ifeq conditionals
author Matt Mackall <mpm@selenic.com>
date Mon, 24 Sep 2012 15:26:56 -0500
parents 8804e3cb51bd
children 02d2166ef5f1
line wrap: on
line diff
--- a/mercurial/templater.py	Mon Sep 24 15:26:17 2012 -0500
+++ b/mercurial/templater.py	Mon Sep 24 15:26:56 2012 -0500
@@ -227,6 +227,31 @@
     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 @@
     }
 
 funcs = {
+    "if": if_,
+    "ifeq": ifeq,
     "join": join,
     "sub": sub,
 }