comparison mercurial/formatter.py @ 32948:12a0794fa2e3

formatter: extract helper function to render template
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 21:38:08 +0900
parents 799db2af824c
children 13eebc189ff3
comparison
equal deleted inserted replaced
32947:3f07f12c6e10 32948:12a0794fa2e3
355 def context(self, **ctxs): 355 def context(self, **ctxs):
356 '''insert context objects to be used to render template keywords''' 356 '''insert context objects to be used to render template keywords'''
357 ctxs = pycompat.byteskwargs(ctxs) 357 ctxs = pycompat.byteskwargs(ctxs)
358 assert all(k == 'ctx' for k in ctxs) 358 assert all(k == 'ctx' for k in ctxs)
359 self._item.update(ctxs) 359 self._item.update(ctxs)
360 def _showitem(self): 360
361 def _showitem(self):
362 item = self._item.copy()
363 item['index'] = next(self._counter)
364 self._renderitem(self._tref, item)
365
366 def _renderitem(self, ref, item):
361 # TODO: add support for filectx. probably each template keyword or 367 # TODO: add support for filectx. probably each template keyword or
362 # function will have to declare dependent resources. e.g. 368 # function will have to declare dependent resources. e.g.
363 # @templatekeyword(..., requires=('ctx',)) 369 # @templatekeyword(..., requires=('ctx',))
364 props = {} 370 props = {}
365 if 'ctx' in self._item: 371 if 'ctx' in item:
366 props.update(templatekw.keywords) 372 props.update(templatekw.keywords)
367 props['index'] = next(self._counter)
368 # explicitly-defined fields precede templatekw 373 # explicitly-defined fields precede templatekw
369 props.update(self._item) 374 props.update(item)
370 if 'ctx' in self._item: 375 if 'ctx' in item:
371 # but template resources must be always available 376 # but template resources must be always available
372 props['templ'] = self._t 377 props['templ'] = self._t
373 props['repo'] = props['ctx'].repo() 378 props['repo'] = props['ctx'].repo()
374 props['revcache'] = {} 379 props['revcache'] = {}
375 props = pycompat.strkwargs(props) 380 props = pycompat.strkwargs(props)
376 g = self._t(self._tref, ui=self._ui, cache=self._cache, **props) 381 g = self._t(ref, ui=self._ui, cache=self._cache, **props)
377 self._out.write(templater.stringify(g)) 382 self._out.write(templater.stringify(g))
378 383
379 templatespec = collections.namedtuple(r'templatespec', 384 templatespec = collections.namedtuple(r'templatespec',
380 r'ref tmpl mapfile') 385 r'ref tmpl mapfile')
381 386