comparison mercurial/templater.py @ 20518:1e43f15a647f

template: add ifcontains template function Adds a template function with the signature 'ifcontains(item, set, then[, else])'. It can be used to do things like '{ifcontains('.hgignore', file_mods, label(...), ...)}' to color commits that edit the .hgignore file. A future patch will add the revset() function which will combine with ifcontains to allow us to color commits if they are in the revset.
author Durham Goode <durham@fb.com>
date Tue, 11 Feb 2014 21:10:00 -0800
parents 6f3fb6a974e0
children cda9d2b6beab
comparison
equal deleted inserted replaced
20517:2158e8f3cbd2 20518:1e43f15a647f
299 if test: 299 if test:
300 yield _evalifliteral(args[1], context, mapping) 300 yield _evalifliteral(args[1], context, mapping)
301 elif len(args) == 3: 301 elif len(args) == 3:
302 yield _evalifliteral(args[2], context, mapping) 302 yield _evalifliteral(args[2], context, mapping)
303 303
304 def ifcontains(context, mapping, args):
305 if not (3 <= len(args) <= 4):
306 # i18n: "ifcontains" is a keyword
307 raise error.ParseError(_("ifcontains expects three or four arguments"))
308
309 item = stringify(args[0][0](context, mapping, args[0][1]))
310 items = args[1][0](context, mapping, args[1][1])
311
312 if item in items:
313 yield _evalifliteral(args[2], context, mapping)
314 elif len(args) == 4:
315 yield _evalifliteral(args[3], context, mapping)
316
304 def ifeq(context, mapping, args): 317 def ifeq(context, mapping, args):
305 if not (3 <= len(args) <= 4): 318 if not (3 <= len(args) <= 4):
306 # i18n: "ifeq" is a keyword 319 # i18n: "ifeq" is a keyword
307 raise error.ParseError(_("ifeq expects three or four arguments")) 320 raise error.ParseError(_("ifeq expects three or four arguments"))
308 321
434 funcs = { 447 funcs = {
435 "date": date, 448 "date": date,
436 "fill": fill, 449 "fill": fill,
437 "get": get, 450 "get": get,
438 "if": if_, 451 "if": if_,
452 "ifcontains": ifcontains,
439 "ifeq": ifeq, 453 "ifeq": ifeq,
440 "join": join, 454 "join": join,
441 "label": label, 455 "label": label,
442 "pad": pad, 456 "pad": pad,
443 "rstdoc": rstdoc, 457 "rstdoc": rstdoc,