mercurial/formatter.py
changeset 35469 f1c54d003327
parent 35468 32c278eb876f
child 35470 a33be093ec62
equal deleted inserted replaced
35468:32c278eb876f 35469:f1c54d003327
   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, cache=templatekw.defaulttempl)
   366         self._t = loadtemplater(ui, spec, resources=templateresources(ui),
       
   367                                 cache=templatekw.defaulttempl)
   367         self._parts = templatepartsmap(spec, self._t,
   368         self._parts = templatepartsmap(spec, self._t,
   368                                        ['docheader', 'docfooter', 'separator'])
   369                                        ['docheader', 'docfooter', 'separator'])
   369         self._counter = itertools.count()
   370         self._counter = itertools.count()
   370         self._cache = {}  # for templatekw/funcs to store reusable data
       
   371         self._renderitem('docheader', {})
   371         self._renderitem('docheader', {})
   372 
   372 
   373     def _showitem(self):
   373     def _showitem(self):
   374         item = self._item.copy()
   374         item = self._item.copy()
   375         item['index'] = index = next(self._counter)
   375         item['index'] = index = next(self._counter)
   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['repo'] = props['ctx'].repo()
   395             props['repo'] = props['ctx'].repo()
   396             props['revcache'] = {}
   396             props['revcache'] = {}
   397         props = pycompat.strkwargs(props)
   397         props = pycompat.strkwargs(props)
   398         g = self._t(ref, ui=self._ui, cache=self._cache, **props)
   398         g = self._t(ref, **props)
   399         self._out.write(templater.stringify(g))
   399         self._out.write(templater.stringify(g))
   400 
   400 
   401     def end(self):
   401     def end(self):
   402         baseformatter.end(self)
   402         baseformatter.end(self)
   403         self._renderitem('docfooter', {})
   403         self._renderitem('docfooter', {})
   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
       
   488 
       
   489 def templateresources(ui, repo=None):
       
   490     """Create a dict of template resources designed for the default templatekw
       
   491     and function"""
       
   492     return {
       
   493         'cache': {},  # for templatekw/funcs to store reusable data
       
   494         'repo': repo,
       
   495         'ui': ui,
       
   496     }
   488 
   497 
   489 def formatter(ui, out, topic, opts):
   498 def formatter(ui, out, topic, opts):
   490     template = opts.get("template", "")
   499     template = opts.get("template", "")
   491     if template == "json":
   500     if template == "json":
   492         return jsonformatter(ui, out, topic, opts)
   501         return jsonformatter(ui, out, topic, opts)