comparison mercurial/templater.py @ 30049:f18cc848b48e

templater: use "needle" and "haystack" as (meta-)variables for ifcontains() It wasn't immediately clear if it's supposed to look for "search" in "thing" or "thing" in "search".
author Anton Shestakov <av6@dwimlabs.net>
date Sat, 01 Oct 2016 09:55:32 +0800
parents e83f89d3b1f7
children bd1f043d1ea3
comparison
equal deleted inserted replaced
30048:91a3c58ecf93 30049:f18cc848b48e
594 if test: 594 if test:
595 yield args[1][0](context, mapping, args[1][1]) 595 yield args[1][0](context, mapping, args[1][1])
596 elif len(args) == 3: 596 elif len(args) == 3:
597 yield args[2][0](context, mapping, args[2][1]) 597 yield args[2][0](context, mapping, args[2][1])
598 598
599 @templatefunc('ifcontains(search, thing, then[, else])') 599 @templatefunc('ifcontains(needle, haystack, then[, else])')
600 def ifcontains(context, mapping, args): 600 def ifcontains(context, mapping, args):
601 """Conditionally execute based 601 """Conditionally execute based
602 on whether the item "search" is in "thing".""" 602 on whether the item "needle" is in "haystack"."""
603 if not (3 <= len(args) <= 4): 603 if not (3 <= len(args) <= 4):
604 # i18n: "ifcontains" is a keyword 604 # i18n: "ifcontains" is a keyword
605 raise error.ParseError(_("ifcontains expects three or four arguments")) 605 raise error.ParseError(_("ifcontains expects three or four arguments"))
606 606
607 item = evalstring(context, mapping, args[0]) 607 needle = evalstring(context, mapping, args[0])
608 items = evalfuncarg(context, mapping, args[1]) 608 haystack = evalfuncarg(context, mapping, args[1])
609 609
610 if item in items: 610 if needle in haystack:
611 yield args[2][0](context, mapping, args[2][1]) 611 yield args[2][0](context, mapping, args[2][1])
612 elif len(args) == 4: 612 elif len(args) == 4:
613 yield args[3][0](context, mapping, args[3][1]) 613 yield args[3][0](context, mapping, args[3][1])
614 614
615 @templatefunc('ifeq(expr1, expr2, then[, else])') 615 @templatefunc('ifeq(expr1, expr2, then[, else])')