##// END OF EJS Templates
templater: factor out thin helper that evaluates argument as string...
Yuya Nishihara -
r28348:ccedb17a default
parent child Browse files
Show More
@@ -489,16 +489,14 b' def templatelabel(context, mapping, args'
489 489 # etc. don't need to be quoted
490 490 mapping.update(dict([(k, k) for k in _effects]))
491 491
492 thing = args[1][0](context, mapping, args[1][1])
493 thing = templater.stringify(thing)
492 thing = templater.evalstring(context, mapping, args[1])
494 493
495 494 # apparently, repo could be a string that is the favicon?
496 495 repo = mapping.get('repo', '')
497 496 if isinstance(repo, str):
498 497 return thing
499 498
500 label = args[0][0](context, mapping, args[0][1])
501 label = templater.stringify(label)
499 label = templater.evalstring(context, mapping, args[0])
502 500
503 501 return repo.ui.label(thing, label)
504 502
@@ -227,6 +227,10 b' def evalinteger(context, mapping, arg, e'
227 227 except (TypeError, ValueError):
228 228 raise error.ParseError(err)
229 229
230 def evalstring(context, mapping, arg):
231 func, data = arg
232 return stringify(func(context, mapping, data))
233
230 234 def runinteger(context, mapping, data):
231 235 return int(data)
232 236
@@ -339,7 +343,7 b' def date(context, mapping, args):'
339 343 date = evalfuncarg(context, mapping, args[0])
340 344 fmt = None
341 345 if len(args) == 2:
342 fmt = stringify(args[1][0](context, mapping, args[1][1]))
346 fmt = evalstring(context, mapping, args[1])
343 347 try:
344 348 if fmt is None:
345 349 return util.datestr(date)
@@ -358,7 +362,7 b' def diff(context, mapping, args):'
358 362
359 363 def getpatterns(i):
360 364 if i < len(args):
361 s = stringify(args[i][0](context, mapping, args[i][1])).strip()
365 s = evalstring(context, mapping, args[i]).strip()
362 366 if s:
363 367 return [s]
364 368 return []
@@ -375,7 +379,7 b' def fill(context, mapping, args):'
375 379 # i18n: "fill" is a keyword
376 380 raise error.ParseError(_("fill expects one to four arguments"))
377 381
378 text = stringify(args[0][0](context, mapping, args[0][1]))
382 text = evalstring(context, mapping, args[0])
379 383 width = 76
380 384 initindent = ''
381 385 hangindent = ''
@@ -384,8 +388,8 b' def fill(context, mapping, args):'
384 388 # i18n: "fill" is a keyword
385 389 _("fill expects an integer width"))
386 390 try:
387 initindent = stringify(args[2][0](context, mapping, args[2][1]))
388 hangindent = stringify(args[3][0](context, mapping, args[3][1]))
391 initindent = evalstring(context, mapping, args[2])
392 hangindent = evalstring(context, mapping, args[3])
389 393 except IndexError:
390 394 pass
391 395
@@ -402,12 +406,12 b' def pad(context, mapping, args):'
402 406 # i18n: "pad" is a keyword
403 407 _("pad() expects an integer width"))
404 408
405 text = stringify(args[0][0](context, mapping, args[0][1]))
409 text = evalstring(context, mapping, args[0])
406 410
407 411 right = False
408 412 fillchar = ' '
409 413 if len(args) > 2:
410 fillchar = stringify(args[2][0](context, mapping, args[2][1]))
414 fillchar = evalstring(context, mapping, args[2])
411 415 if len(args) > 3:
412 416 right = util.parsebool(args[3][1])
413 417
@@ -425,11 +429,11 b' def indent(context, mapping, args):'
425 429 # i18n: "indent" is a keyword
426 430 raise error.ParseError(_("indent() expects two or three arguments"))
427 431
428 text = stringify(args[0][0](context, mapping, args[0][1]))
429 indent = stringify(args[1][0](context, mapping, args[1][1]))
432 text = evalstring(context, mapping, args[0])
433 indent = evalstring(context, mapping, args[1])
430 434
431 435 if len(args) == 3:
432 firstline = stringify(args[2][0](context, mapping, args[2][1]))
436 firstline = evalstring(context, mapping, args[2])
433 437 else:
434 438 firstline = indent
435 439
@@ -459,7 +463,7 b' def if_(context, mapping, args):'
459 463 # i18n: "if" is a keyword
460 464 raise error.ParseError(_("if expects two or three arguments"))
461 465
462 test = stringify(args[0][0](context, mapping, args[0][1]))
466 test = evalstring(context, mapping, args[0])
463 467 if test:
464 468 yield args[1][0](context, mapping, args[1][1])
465 469 elif len(args) == 3:
@@ -472,7 +476,7 b' def ifcontains(context, mapping, args):'
472 476 # i18n: "ifcontains" is a keyword
473 477 raise error.ParseError(_("ifcontains expects three or four arguments"))
474 478
475 item = stringify(args[0][0](context, mapping, args[0][1]))
479 item = evalstring(context, mapping, args[0])
476 480 items = evalfuncarg(context, mapping, args[1])
477 481
478 482 if item in items:
@@ -487,8 +491,8 b' def ifeq(context, mapping, args):'
487 491 # i18n: "ifeq" is a keyword
488 492 raise error.ParseError(_("ifeq expects three or four arguments"))
489 493
490 test = stringify(args[0][0](context, mapping, args[0][1]))
491 match = stringify(args[1][0](context, mapping, args[1][1]))
494 test = evalstring(context, mapping, args[0])
495 match = evalstring(context, mapping, args[1])
492 496 if test == match:
493 497 yield args[2][0](context, mapping, args[2][1])
494 498 elif len(args) == 4:
@@ -507,7 +511,7 b' def join(context, mapping, args):'
507 511
508 512 joiner = " "
509 513 if len(args) > 1:
510 joiner = stringify(args[1][0](context, mapping, args[1][1]))
514 joiner = evalstring(context, mapping, args[1])
511 515
512 516 first = True
513 517 for x in joinset:
@@ -537,7 +541,7 b' def latesttag(context, mapping, args):'
537 541
538 542 pattern = None
539 543 if len(args) == 1:
540 pattern = stringify(args[0][0](context, mapping, args[0][1]))
544 pattern = evalstring(context, mapping, args[0])
541 545
542 546 return templatekw.showlatesttags(pattern, **mapping)
543 547
@@ -576,7 +580,7 b' def revset(context, mapping, args):'
576 580 # i18n: "revset" is a keyword
577 581 raise error.ParseError(_("revset expects one or more arguments"))
578 582
579 raw = stringify(args[0][0](context, mapping, args[0][1]))
583 raw = evalstring(context, mapping, args[0])
580 584 ctx = mapping['ctx']
581 585 repo = ctx.repo()
582 586
@@ -605,8 +609,8 b' def rstdoc(context, mapping, args):'
605 609 # i18n: "rstdoc" is a keyword
606 610 raise error.ParseError(_("rstdoc expects two arguments"))
607 611
608 text = stringify(args[0][0](context, mapping, args[0][1]))
609 style = stringify(args[1][0](context, mapping, args[1][1]))
612 text = evalstring(context, mapping, args[0])
613 style = evalstring(context, mapping, args[1])
610 614
611 615 return minirst.format(text, style=style, keep=['verbose'])
612 616
@@ -617,7 +621,7 b' def shortest(context, mapping, args):'
617 621 # i18n: "shortest" is a keyword
618 622 raise error.ParseError(_("shortest() expects one or two arguments"))
619 623
620 node = stringify(args[0][0](context, mapping, args[0][1]))
624 node = evalstring(context, mapping, args[0])
621 625
622 626 minlength = 4
623 627 if len(args) > 1:
@@ -671,9 +675,9 b' def strip(context, mapping, args):'
671 675 # i18n: "strip" is a keyword
672 676 raise error.ParseError(_("strip expects one or two arguments"))
673 677
674 text = stringify(args[0][0](context, mapping, args[0][1]))
678 text = evalstring(context, mapping, args[0])
675 679 if len(args) == 2:
676 chars = stringify(args[1][0](context, mapping, args[1][1]))
680 chars = evalstring(context, mapping, args[1])
677 681 return text.strip(chars)
678 682 return text.strip()
679 683
@@ -684,9 +688,9 b' def sub(context, mapping, args):'
684 688 # i18n: "sub" is a keyword
685 689 raise error.ParseError(_("sub expects three arguments"))
686 690
687 pat = stringify(args[0][0](context, mapping, args[0][1]))
688 rpl = stringify(args[1][0](context, mapping, args[1][1]))
689 src = stringify(args[2][0](context, mapping, args[2][1]))
691 pat = evalstring(context, mapping, args[0])
692 rpl = evalstring(context, mapping, args[1])
693 src = evalstring(context, mapping, args[2])
690 694 try:
691 695 patre = re.compile(pat)
692 696 except re.error:
@@ -705,8 +709,8 b' def startswith(context, mapping, args):'
705 709 # i18n: "startswith" is a keyword
706 710 raise error.ParseError(_("startswith expects two arguments"))
707 711
708 patn = stringify(args[0][0](context, mapping, args[0][1]))
709 text = stringify(args[1][0](context, mapping, args[1][1]))
712 patn = evalstring(context, mapping, args[0])
713 text = evalstring(context, mapping, args[1])
710 714 if text.startswith(patn):
711 715 return text
712 716 return ''
@@ -722,9 +726,9 b' def word(context, mapping, args):'
722 726 num = evalinteger(context, mapping, args[0],
723 727 # i18n: "word" is a keyword
724 728 _("word expects an integer index"))
725 text = stringify(args[1][0](context, mapping, args[1][1]))
729 text = evalstring(context, mapping, args[1])
726 730 if len(args) == 3:
727 splitter = stringify(args[2][0](context, mapping, args[2][1]))
731 splitter = evalstring(context, mapping, args[2])
728 732 else:
729 733 splitter = None
730 734
General Comments 0
You need to be logged in to leave comments. Login now