##// END OF EJS Templates
templater: extract helper to just evaluate template expression...
Yuya Nishihara -
r34327:e60c6019 default
parent child Browse files
Show More
@@ -298,11 +298,17 b' def findsymbolicname(arg):'
298 else:
298 else:
299 return None
299 return None
300
300
301 def evalfuncarg(context, mapping, arg):
301 def evalrawexp(context, mapping, arg):
302 """Evaluate given argument as a bare template object which may require
303 further processing (such as folding generator of strings)"""
302 func, data = arg
304 func, data = arg
303 # func() may return string, generator of strings or arbitrary object such
305 return func(context, mapping, data)
304 # as date tuple, but filter does not want generator.
306
305 thing = func(context, mapping, data)
307 def evalfuncarg(context, mapping, arg):
308 """Evaluate given argument as value type"""
309 thing = evalrawexp(context, mapping, arg)
310 # evalrawexp() may return string, generator of strings or arbitrary object
311 # such as date tuple, but filter does not want generator.
306 if isinstance(thing, types.GeneratorType):
312 if isinstance(thing, types.GeneratorType):
307 thing = stringify(thing)
313 thing = stringify(thing)
308 return thing
314 return thing
@@ -331,8 +337,7 b' def evalinteger(context, mapping, arg, e'
331 raise error.ParseError(err)
337 raise error.ParseError(err)
332
338
333 def evalstring(context, mapping, arg):
339 def evalstring(context, mapping, arg):
334 func, data = arg
340 return stringify(evalrawexp(context, mapping, arg))
335 return stringify(func(context, mapping, data))
336
341
337 def evalstringliteral(context, mapping, arg):
342 def evalstringliteral(context, mapping, arg):
338 """Evaluate given argument as string template, but returns symbol name
343 """Evaluate given argument as string template, but returns symbol name
@@ -380,8 +385,8 b' def buildtemplate(exp, context):'
380 return (runtemplate, ctmpl)
385 return (runtemplate, ctmpl)
381
386
382 def runtemplate(context, mapping, template):
387 def runtemplate(context, mapping, template):
383 for func, data in template:
388 for arg in template:
384 yield func(context, mapping, data)
389 yield evalrawexp(context, mapping, arg)
385
390
386 def buildfilter(exp, context):
391 def buildfilter(exp, context):
387 n = getsymbol(exp[2])
392 n = getsymbol(exp[2])
@@ -415,15 +420,15 b' def buildmap(exp, context):'
415 return (runmap, (darg, targ))
420 return (runmap, (darg, targ))
416
421
417 def runmap(context, mapping, data):
422 def runmap(context, mapping, data):
418 (func, data), (tfunc, tdata) = data
423 darg, targ = data
419 d = func(context, mapping, data)
424 d = evalrawexp(context, mapping, darg)
420 if util.safehasattr(d, 'itermaps'):
425 if util.safehasattr(d, 'itermaps'):
421 diter = d.itermaps()
426 diter = d.itermaps()
422 else:
427 else:
423 try:
428 try:
424 diter = iter(d)
429 diter = iter(d)
425 except TypeError:
430 except TypeError:
426 sym = findsymbolicname((func, data))
431 sym = findsymbolicname(darg)
427 if sym:
432 if sym:
428 raise error.ParseError(_("keyword '%s' is not iterable") % sym)
433 raise error.ParseError(_("keyword '%s' is not iterable") % sym)
429 else:
434 else:
@@ -435,7 +440,7 b' def runmap(context, mapping, data):'
435 if isinstance(v, dict):
440 if isinstance(v, dict):
436 lm.update(v)
441 lm.update(v)
437 lm['originalnode'] = mapping.get('node')
442 lm['originalnode'] = mapping.get('node')
438 yield tfunc(context, lm, tdata)
443 yield evalrawexp(context, lm, targ)
439 else:
444 else:
440 # v is not an iterable of dicts, this happen when 'key'
445 # v is not an iterable of dicts, this happen when 'key'
441 # has been fully expanded already and format is useless.
446 # has been fully expanded already and format is useless.
@@ -718,9 +723,9 b' def if_(context, mapping, args):'
718
723
719 test = evalboolean(context, mapping, args[0])
724 test = evalboolean(context, mapping, args[0])
720 if test:
725 if test:
721 yield args[1][0](context, mapping, args[1][1])
726 yield evalrawexp(context, mapping, args[1])
722 elif len(args) == 3:
727 elif len(args) == 3:
723 yield args[2][0](context, mapping, args[2][1])
728 yield evalrawexp(context, mapping, args[2])
724
729
725 @templatefunc('ifcontains(needle, haystack, then[, else])')
730 @templatefunc('ifcontains(needle, haystack, then[, else])')
726 def ifcontains(context, mapping, args):
731 def ifcontains(context, mapping, args):
@@ -734,9 +739,9 b' def ifcontains(context, mapping, args):'
734 haystack = evalfuncarg(context, mapping, args[1])
739 haystack = evalfuncarg(context, mapping, args[1])
735
740
736 if needle in haystack:
741 if needle in haystack:
737 yield args[2][0](context, mapping, args[2][1])
742 yield evalrawexp(context, mapping, args[2])
738 elif len(args) == 4:
743 elif len(args) == 4:
739 yield args[3][0](context, mapping, args[3][1])
744 yield evalrawexp(context, mapping, args[3])
740
745
741 @templatefunc('ifeq(expr1, expr2, then[, else])')
746 @templatefunc('ifeq(expr1, expr2, then[, else])')
742 def ifeq(context, mapping, args):
747 def ifeq(context, mapping, args):
@@ -749,9 +754,9 b' def ifeq(context, mapping, args):'
749 test = evalstring(context, mapping, args[0])
754 test = evalstring(context, mapping, args[0])
750 match = evalstring(context, mapping, args[1])
755 match = evalstring(context, mapping, args[1])
751 if test == match:
756 if test == match:
752 yield args[2][0](context, mapping, args[2][1])
757 yield evalrawexp(context, mapping, args[2])
753 elif len(args) == 4:
758 elif len(args) == 4:
754 yield args[3][0](context, mapping, args[3][1])
759 yield evalrawexp(context, mapping, args[3])
755
760
756 @templatefunc('join(list, sep)')
761 @templatefunc('join(list, sep)')
757 def join(context, mapping, args):
762 def join(context, mapping, args):
@@ -760,7 +765,9 b' def join(context, mapping, args):'
760 # i18n: "join" is a keyword
765 # i18n: "join" is a keyword
761 raise error.ParseError(_("join expects one or two arguments"))
766 raise error.ParseError(_("join expects one or two arguments"))
762
767
763 joinset = args[0][0](context, mapping, args[0][1])
768 # TODO: perhaps this should be evalfuncarg(), but it can't because hgweb
769 # abuses generator as a keyword that returns a list of dicts.
770 joinset = evalrawexp(context, mapping, args[0])
764 if util.safehasattr(joinset, 'itermaps'):
771 if util.safehasattr(joinset, 'itermaps'):
765 jf = joinset.joinfmt
772 jf = joinset.joinfmt
766 joinset = [jf(x) for x in joinset.itermaps()]
773 joinset = [jf(x) for x in joinset.itermaps()]
General Comments 0
You need to be logged in to leave comments. Login now