comparison mercurial/templater.py @ 28373:9a9dd71e882c

templater: make label() take unknown symbol as color literal Instead of the mapping hack introduced by b775a2029e8d, this patch changes the way how a label symbol is evaluated. This is still hackish, but should be more predictable in that it doesn't depend on the known color effects. This change is intended to eliminate the reference to color._effects so that color.templatelabel() can be merged with templater.label().
author Yuya Nishihara <yuya@tcha.org>
date Thu, 11 Jun 2015 22:58:27 +0900
parents 7cb2f2438f85
children af3bd9d1dbc1
comparison
equal deleted inserted replaced
28372:74d03766f962 28373:9a9dd71e882c
229 229
230 def evalstring(context, mapping, arg): 230 def evalstring(context, mapping, arg):
231 func, data = arg 231 func, data = arg
232 return stringify(func(context, mapping, data)) 232 return stringify(func(context, mapping, data))
233 233
234 def evalstringliteral(context, mapping, arg):
235 """Evaluate given argument as string template, but returns symbol name
236 if it is unknown"""
237 func, data = arg
238 if func is runsymbol:
239 thing = func(context, mapping, data, default=data)
240 else:
241 thing = func(context, mapping, data)
242 return stringify(thing)
243
234 def runinteger(context, mapping, data): 244 def runinteger(context, mapping, data):
235 return int(data) 245 return int(data)
236 246
237 def runstring(context, mapping, data): 247 def runstring(context, mapping, data):
238 return data 248 return data
243 return showrecursion 253 return showrecursion
244 254
245 def _runrecursivesymbol(context, mapping, key): 255 def _runrecursivesymbol(context, mapping, key):
246 raise error.Abort(_("recursive reference '%s' in template") % key) 256 raise error.Abort(_("recursive reference '%s' in template") % key)
247 257
248 def runsymbol(context, mapping, key): 258 def runsymbol(context, mapping, key, default=''):
249 v = mapping.get(key) 259 v = mapping.get(key)
250 if v is None: 260 if v is None:
251 v = context._defaults.get(key) 261 v = context._defaults.get(key)
252 if v is None: 262 if v is None:
253 # put poison to cut recursion. we can't move this to parsing phase 263 # put poison to cut recursion. we can't move this to parsing phase
255 safemapping = mapping.copy() 265 safemapping = mapping.copy()
256 safemapping[key] = _recursivesymbolblocker(key) 266 safemapping[key] = _recursivesymbolblocker(key)
257 try: 267 try:
258 v = context.process(key, safemapping) 268 v = context.process(key, safemapping)
259 except TemplateNotFound: 269 except TemplateNotFound:
260 v = '' 270 v = default
261 if callable(v): 271 if callable(v):
262 return v(**mapping) 272 return v(**mapping)
263 return v 273 return v
264 274
265 def buildtemplate(exp, context): 275 def buildtemplate(exp, context):