comparison mercurial/formatter.py @ 32875:c8f2cf18b82e

formatter: load templates section like a map file Since a map file has another level to select a template (spec -> mapfile -> topic), this isn't exactly the same as how a map file works. But I believe most users would expect the new behavior. A literal template is stored as an unnamed template so that it will never conflict with the templates defined in [templates] section.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 20:14:55 +0900
parents 2ecce24dfcd3
children 8da65da039c3
comparison
equal deleted inserted replaced
32874:dddba6f3e59c 32875:c8f2cf18b82e
389 389
390 A map file defines a stand-alone template environment. If a map file 390 A map file defines a stand-alone template environment. If a map file
391 selected, all templates defined in the file will be loaded, and the 391 selected, all templates defined in the file will be loaded, and the
392 template matching the given topic will be rendered. No aliases will be 392 template matching the given topic will be rendered. No aliases will be
393 loaded from user config. 393 loaded from user config.
394
395 If no map file selected, all templates in [templates] section will be
396 available as well as aliases in [templatealias].
394 """ 397 """
395 398
396 # looks like a literal template? 399 # looks like a literal template?
397 if '{' in tmpl: 400 if '{' in tmpl:
398 return templatespec(topic, tmpl, None) 401 return templatespec('', tmpl, None)
399 402
400 # perhaps a stock style? 403 # perhaps a stock style?
401 if not os.path.split(tmpl)[0]: 404 if not os.path.split(tmpl)[0]:
402 mapname = (templater.templatepath('map-cmdline.' + tmpl) 405 mapname = (templater.templatepath('map-cmdline.' + tmpl)
403 or templater.templatepath(tmpl)) 406 or templater.templatepath(tmpl))
404 if mapname and os.path.isfile(mapname): 407 if mapname and os.path.isfile(mapname):
405 return templatespec(topic, None, mapname) 408 return templatespec(topic, None, mapname)
406 409
407 # perhaps it's a reference to [templates] 410 # perhaps it's a reference to [templates]
408 t = ui.config('templates', tmpl) 411 if ui.config('templates', tmpl):
409 if t: 412 return templatespec(tmpl, None, None)
410 return templatespec(topic, templater.unquotestring(t), None)
411 413
412 if tmpl == 'list': 414 if tmpl == 'list':
413 ui.write(_("available styles: %s\n") % templater.stylelist()) 415 ui.write(_("available styles: %s\n") % templater.stylelist())
414 raise error.Abort(_("specify a template")) 416 raise error.Abort(_("specify a template"))
415 417
418 # is it a mapfile for a style? 420 # is it a mapfile for a style?
419 if os.path.basename(tmpl).startswith("map-"): 421 if os.path.basename(tmpl).startswith("map-"):
420 return templatespec(topic, None, os.path.realpath(tmpl)) 422 return templatespec(topic, None, os.path.realpath(tmpl))
421 with util.posixfile(tmpl, 'rb') as f: 423 with util.posixfile(tmpl, 'rb') as f:
422 tmpl = f.read() 424 tmpl = f.read()
423 return templatespec(topic, tmpl, None) 425 return templatespec('', tmpl, None)
424 426
425 # constant string? 427 # constant string?
426 return templatespec(topic, tmpl, None) 428 return templatespec('', tmpl, None)
427 429
428 def loadtemplater(ui, spec, cache=None): 430 def loadtemplater(ui, spec, cache=None):
429 """Create a templater from either a literal template or loading from 431 """Create a templater from either a literal template or loading from
430 a map file""" 432 a map file"""
431 assert not (spec.tmpl and spec.mapfile) 433 assert not (spec.tmpl and spec.mapfile)
438 return _maketemplater(ui, '', tmpl, cache=cache) 440 return _maketemplater(ui, '', tmpl, cache=cache)
439 441
440 def _maketemplater(ui, topic, tmpl, cache=None): 442 def _maketemplater(ui, topic, tmpl, cache=None):
441 aliases = ui.configitems('templatealias') 443 aliases = ui.configitems('templatealias')
442 t = templater.templater(cache=cache, aliases=aliases) 444 t = templater.templater(cache=cache, aliases=aliases)
445 t.cache.update((k, templater.unquotestring(v))
446 for k, v in ui.configitems('templates'))
443 if tmpl: 447 if tmpl:
444 t.cache[topic] = tmpl 448 t.cache[topic] = tmpl
445 return t 449 return t
446 450
447 def formatter(ui, out, topic, opts): 451 def formatter(ui, out, topic, opts):