comparison 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
comparison
equal deleted inserted replaced
17635:8804e3cb51bd 17636:ce18dbcd91c8
225 pat = stringify(args[0][0](context, mapping, args[0][1])) 225 pat = stringify(args[0][0](context, mapping, args[0][1]))
226 rpl = stringify(args[1][0](context, mapping, args[1][1])) 226 rpl = stringify(args[1][0](context, mapping, args[1][1]))
227 src = stringify(args[2][0](context, mapping, args[2][1])) 227 src = stringify(args[2][0](context, mapping, args[2][1]))
228 yield re.sub(pat, rpl, src) 228 yield re.sub(pat, rpl, src)
229 229
230 def if_(context, mapping, args):
231 if not (2 <= len(args) <= 3):
232 raise error.ParseError(_("if expects two or three arguments"))
233
234 test = stringify(args[0][0](context, mapping, args[0][1]))
235 if test:
236 t = stringify(args[1][0](context, mapping, args[1][1]))
237 yield runtemplate(context, mapping, compiletemplate(t, context))
238 elif len(args) == 3:
239 t = stringify(args[2][0](context, mapping, args[2][1]))
240 yield runtemplate(context, mapping, compiletemplate(t, context))
241
242 def ifeq(context, mapping, args):
243 if not (3 <= len(args) <= 4):
244 raise error.ParseError(_("ifeq expects three or four arguments"))
245
246 test = stringify(args[0][0](context, mapping, args[0][1]))
247 match = stringify(args[1][0](context, mapping, args[1][1]))
248 if test == match:
249 t = stringify(args[2][0](context, mapping, args[2][1]))
250 yield runtemplate(context, mapping, compiletemplate(t, context))
251 elif len(args) == 4:
252 t = stringify(args[3][0](context, mapping, args[3][1]))
253 yield runtemplate(context, mapping, compiletemplate(t, context))
254
230 methods = { 255 methods = {
231 "string": lambda e, c: (runstring, e[1]), 256 "string": lambda e, c: (runstring, e[1]),
232 "symbol": lambda e, c: (runsymbol, e[1]), 257 "symbol": lambda e, c: (runsymbol, e[1]),
233 "group": lambda e, c: compileexp(e[1], c), 258 "group": lambda e, c: compileexp(e[1], c),
234 # ".": buildmember, 259 # ".": buildmember,
236 "%": buildmap, 261 "%": buildmap,
237 "func": buildfunc, 262 "func": buildfunc,
238 } 263 }
239 264
240 funcs = { 265 funcs = {
266 "if": if_,
267 "ifeq": ifeq,
241 "join": join, 268 "join": join,
242 "sub": sub, 269 "sub": sub,
243 } 270 }
244 271
245 # template engine 272 # template engine