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