Mercurial > hg
comparison mercurial/formatter.py @ 35483:817a3d20dd01
templater: register keywords to defaults table
Since the keywords are permanent, there should be no need to pass them
by a temporary mapping.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 22 Dec 2017 21:19:29 +0900 |
parents | a33be093ec62 |
children | 0fb0c304ebd6 |
comparison
equal
deleted
inserted
replaced
35482:c240657febb7 | 35483:817a3d20dd01 |
---|---|
361 def __init__(self, ui, out, topic, opts): | 361 def __init__(self, ui, out, topic, opts): |
362 baseformatter.__init__(self, ui, topic, opts, _templateconverter) | 362 baseformatter.__init__(self, ui, topic, opts, _templateconverter) |
363 self._out = out | 363 self._out = out |
364 spec = lookuptemplate(ui, topic, opts.get('template', '')) | 364 spec = lookuptemplate(ui, topic, opts.get('template', '')) |
365 self._tref = spec.ref | 365 self._tref = spec.ref |
366 self._t = loadtemplater(ui, spec, resources=templateresources(ui), | 366 self._t = loadtemplater(ui, spec, defaults=templatekw.keywords, |
367 resources=templateresources(ui), | |
367 cache=templatekw.defaulttempl) | 368 cache=templatekw.defaulttempl) |
368 self._parts = templatepartsmap(spec, self._t, | 369 self._parts = templatepartsmap(spec, self._t, |
369 ['docheader', 'docfooter', 'separator']) | 370 ['docheader', 'docfooter', 'separator']) |
370 self._counter = itertools.count() | 371 self._counter = itertools.count() |
371 self._renderitem('docheader', {}) | 372 self._renderitem('docheader', {}) |
384 | 385 |
385 # TODO: add support for filectx. probably each template keyword or | 386 # TODO: add support for filectx. probably each template keyword or |
386 # function will have to declare dependent resources. e.g. | 387 # function will have to declare dependent resources. e.g. |
387 # @templatekeyword(..., requires=('ctx',)) | 388 # @templatekeyword(..., requires=('ctx',)) |
388 props = {} | 389 props = {} |
389 if 'ctx' in item: | |
390 props.update(templatekw.keywords) | |
391 # explicitly-defined fields precede templatekw | 390 # explicitly-defined fields precede templatekw |
392 props.update(item) | 391 props.update(item) |
393 if 'ctx' in item: | 392 if 'ctx' in item: |
394 # but template resources must be always available | 393 # but template resources must be always available |
395 props['repo'] = props['ctx'].repo() | 394 props['repo'] = props['ctx'].repo() |
465 ref = '%s:%s' % (spec.ref, part) # select config sub-section | 464 ref = '%s:%s' % (spec.ref, part) # select config sub-section |
466 if ref in t: | 465 if ref in t: |
467 partsmap[part] = ref | 466 partsmap[part] = ref |
468 return partsmap | 467 return partsmap |
469 | 468 |
470 def loadtemplater(ui, spec, resources=None, cache=None): | 469 def loadtemplater(ui, spec, defaults=None, resources=None, cache=None): |
471 """Create a templater from either a literal template or loading from | 470 """Create a templater from either a literal template or loading from |
472 a map file""" | 471 a map file""" |
473 assert not (spec.tmpl and spec.mapfile) | 472 assert not (spec.tmpl and spec.mapfile) |
474 if spec.mapfile: | 473 if spec.mapfile: |
475 frommapfile = templater.templater.frommapfile | 474 frommapfile = templater.templater.frommapfile |
476 return frommapfile(spec.mapfile, resources=resources, cache=cache) | 475 return frommapfile(spec.mapfile, defaults=defaults, resources=resources, |
477 return maketemplater(ui, spec.tmpl, resources=resources, cache=cache) | 476 cache=cache) |
478 | 477 return maketemplater(ui, spec.tmpl, defaults=defaults, resources=resources, |
479 def maketemplater(ui, tmpl, resources=None, cache=None): | 478 cache=cache) |
479 | |
480 def maketemplater(ui, tmpl, defaults=None, resources=None, cache=None): | |
480 """Create a templater from a string template 'tmpl'""" | 481 """Create a templater from a string template 'tmpl'""" |
481 aliases = ui.configitems('templatealias') | 482 aliases = ui.configitems('templatealias') |
482 t = templater.templater(resources=resources, cache=cache, aliases=aliases) | 483 t = templater.templater(defaults=defaults, resources=resources, |
484 cache=cache, aliases=aliases) | |
483 t.cache.update((k, templater.unquotestring(v)) | 485 t.cache.update((k, templater.unquotestring(v)) |
484 for k, v in ui.configitems('templates')) | 486 for k, v in ui.configitems('templates')) |
485 if tmpl: | 487 if tmpl: |
486 t.cache[''] = tmpl | 488 t.cache[''] = tmpl |
487 return t | 489 return t |