##// END OF EJS Templates
templater: take any string literals as template, but not for rawstring (BC)...
Yuya Nishihara -
r25596:c1975809 default
parent child Browse files
Show More
@@ -47,6 +47,10 b' Also, for any expression that returns a '
47 47
48 48 - expr % "{template}"
49 49
50 As seen in the above example, "{template}" is interpreted as a template.
51 To prevent it from being interpreted, you can use an escape character "\{"
52 or a raw string prefix, "r'...'".
53
50 54 Some sample command line templates:
51 55
52 56 - Format lists, e.g. files::
@@ -22,7 +22,7 b' elements = {'
22 22 ")": (0, None, None),
23 23 "integer": (0, ("integer",), None),
24 24 "symbol": (0, ("symbol",), None),
25 "string": (0, ("string",), None),
25 "string": (0, ("template",), None),
26 26 "rawstring": (0, ("rawstring",), None),
27 27 "end": (0, None, None),
28 28 }
@@ -144,7 +144,9 b' def getfilter(exp, context):'
144 144 return context._filters[f]
145 145
146 146 def gettemplate(exp, context):
147 if exp[0] == 'string' or exp[0] == 'rawstring':
147 if exp[0] == 'template':
148 return compiletemplate(exp[1], context)
149 if exp[0] == 'rawstring':
148 150 return compiletemplate(exp[1], context, strtoken=exp[0])
149 151 if exp[0] == 'symbol':
150 152 return context._load(exp[1])
@@ -174,6 +176,12 b' def runsymbol(context, mapping, key):'
174 176 v = list(v)
175 177 return v
176 178
179 def buildtemplate(exp, context):
180 ctmpl = compiletemplate(exp[1], context)
181 if len(ctmpl) == 1:
182 return ctmpl[0] # fast path for string with no template fragment
183 return (runtemplate, ctmpl)
184
177 185 def runtemplate(context, mapping, template):
178 186 for func, data in template:
179 187 yield func(context, mapping, data)
@@ -362,7 +370,7 b' def get(context, mapping, args):'
362 370
363 371 def _evalifliteral(arg, context, mapping):
364 372 # get back to token tag to reinterpret string as template
365 strtoken = {runstring: 'string', runrawstring: 'rawstring'}.get(arg[0])
373 strtoken = {runrawstring: 'rawstring'}.get(arg[0])
366 374 if strtoken:
367 375 yield runtemplate(context, mapping,
368 376 compiletemplate(arg[1], context, strtoken))
@@ -606,6 +614,7 b' exprmethods = {'
606 614 "string": lambda e, c: (runstring, e[1]),
607 615 "rawstring": lambda e, c: (runrawstring, e[1]),
608 616 "symbol": lambda e, c: (runsymbol, e[1]),
617 "template": buildtemplate,
609 618 "group": lambda e, c: compileexp(e[1], c, exprmethods),
610 619 # ".": buildmember,
611 620 "|": buildfilter,
@@ -2803,6 +2803,15 b' unless explicit symbol is expected:'
2803 2803 hg: parse error: expected a symbol, got 'integer'
2804 2804 [255]
2805 2805
2806 Test string literal:
2807
2808 $ hg log -Ra -r0 -T '{"string with no template fragment"}\n'
2809 string with no template fragment
2810 $ hg log -Ra -r0 -T '{"template: {rev}"}\n'
2811 template: 0
2812 $ hg log -Ra -r0 -T '{r"rawstring: {rev}"}\n'
2813 rawstring: {rev}
2814
2806 2815 Test string escaping:
2807 2816
2808 2817 $ hg log -R latesttag -r 0 --template '>\n<>\\n<{if(rev, "[>\n<>\\n<]")}>\n<>\\n<\n'
General Comments 0
You need to be logged in to leave comments. Login now