Mercurial > public > mercurial-scm > hg
comparison mercurial/formatter.py @ 35468:32c278eb876f
templater: keep default resources per template engine (API)
This allows us to register a repo object as a resource in hgweb template,
without loosing '{repo}' symbol:
symbol('repo') -> mapping['repo'] (n/a) -> defaults['repo']
resource('repo') -> mapping['repo'] (n/a) -> resources['repo']
I'm thinking of redesigning the templatekw API to take (context, mapping)
in place of **(context._resources + mapping), but that will be a big change
and not implemented yet.
props['templ'] is ported to the resources dict as an example.
.. api::
mapping does not contain all template resources. use context.resource()
in template functions.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 21 Dec 2017 21:29:06 +0900 |
parents | f17a0e18c47e |
children | f1c54d003327 |
comparison
equal
deleted
inserted
replaced
35467:d6cfa722b044 | 35468:32c278eb876f |
---|---|
390 props.update(templatekw.keywords) | 390 props.update(templatekw.keywords) |
391 # explicitly-defined fields precede templatekw | 391 # explicitly-defined fields precede templatekw |
392 props.update(item) | 392 props.update(item) |
393 if 'ctx' in item: | 393 if 'ctx' in item: |
394 # but template resources must be always available | 394 # but template resources must be always available |
395 props['templ'] = self._t | |
396 props['repo'] = props['ctx'].repo() | 395 props['repo'] = props['ctx'].repo() |
397 props['revcache'] = {} | 396 props['revcache'] = {} |
398 props = pycompat.strkwargs(props) | 397 props = pycompat.strkwargs(props) |
399 g = self._t(ref, ui=self._ui, cache=self._cache, **props) | 398 g = self._t(ref, ui=self._ui, cache=self._cache, **props) |
400 self._out.write(templater.stringify(g)) | 399 self._out.write(templater.stringify(g)) |
466 ref = '%s:%s' % (spec.ref, part) # select config sub-section | 465 ref = '%s:%s' % (spec.ref, part) # select config sub-section |
467 if ref in t: | 466 if ref in t: |
468 partsmap[part] = ref | 467 partsmap[part] = ref |
469 return partsmap | 468 return partsmap |
470 | 469 |
471 def loadtemplater(ui, spec, cache=None): | 470 def loadtemplater(ui, spec, resources=None, cache=None): |
472 """Create a templater from either a literal template or loading from | 471 """Create a templater from either a literal template or loading from |
473 a map file""" | 472 a map file""" |
474 assert not (spec.tmpl and spec.mapfile) | 473 assert not (spec.tmpl and spec.mapfile) |
475 if spec.mapfile: | 474 if spec.mapfile: |
476 return templater.templater.frommapfile(spec.mapfile, cache=cache) | 475 frommapfile = templater.templater.frommapfile |
477 return maketemplater(ui, spec.tmpl, cache=cache) | 476 return frommapfile(spec.mapfile, resources=resources, cache=cache) |
478 | 477 return maketemplater(ui, spec.tmpl, resources=resources, cache=cache) |
479 def maketemplater(ui, tmpl, cache=None): | 478 |
479 def maketemplater(ui, tmpl, resources=None, cache=None): | |
480 """Create a templater from a string template 'tmpl'""" | 480 """Create a templater from a string template 'tmpl'""" |
481 aliases = ui.configitems('templatealias') | 481 aliases = ui.configitems('templatealias') |
482 t = templater.templater(cache=cache, aliases=aliases) | 482 t = templater.templater(resources=resources, cache=cache, aliases=aliases) |
483 t.cache.update((k, templater.unquotestring(v)) | 483 t.cache.update((k, templater.unquotestring(v)) |
484 for k, v in ui.configitems('templates')) | 484 for k, v in ui.configitems('templates')) |
485 if tmpl: | 485 if tmpl: |
486 t.cache[''] = tmpl | 486 t.cache[''] = tmpl |
487 return t | 487 return t |