##// END OF EJS Templates
formatter: add support for docheader and docfooter templates...
Yuya Nishihara -
r32949:13eebc18 default
parent child Browse files
Show More
@@ -350,8 +350,12 b' class templateformatter(baseformatter):'
350 spec = lookuptemplate(ui, topic, opts.get('template', ''))
350 spec = lookuptemplate(ui, topic, opts.get('template', ''))
351 self._tref = spec.ref
351 self._tref = spec.ref
352 self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl)
352 self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl)
353 self._parts = templatepartsmap(spec, self._t,
354 ['docheader', 'docfooter'])
353 self._counter = itertools.count()
355 self._counter = itertools.count()
354 self._cache = {} # for templatekw/funcs to store reusable data
356 self._cache = {} # for templatekw/funcs to store reusable data
357 self._renderitem('docheader', {})
358
355 def context(self, **ctxs):
359 def context(self, **ctxs):
356 '''insert context objects to be used to render template keywords'''
360 '''insert context objects to be used to render template keywords'''
357 ctxs = pycompat.byteskwargs(ctxs)
361 ctxs = pycompat.byteskwargs(ctxs)
@@ -363,7 +367,11 b' class templateformatter(baseformatter):'
363 item['index'] = next(self._counter)
367 item['index'] = next(self._counter)
364 self._renderitem(self._tref, item)
368 self._renderitem(self._tref, item)
365
369
366 def _renderitem(self, ref, item):
370 def _renderitem(self, part, item):
371 if part not in self._parts:
372 return
373 ref = self._parts[part]
374
367 # TODO: add support for filectx. probably each template keyword or
375 # TODO: add support for filectx. probably each template keyword or
368 # function will have to declare dependent resources. e.g.
376 # function will have to declare dependent resources. e.g.
369 # @templatekeyword(..., requires=('ctx',))
377 # @templatekeyword(..., requires=('ctx',))
@@ -381,6 +389,10 b' class templateformatter(baseformatter):'
381 g = self._t(ref, ui=self._ui, cache=self._cache, **props)
389 g = self._t(ref, ui=self._ui, cache=self._cache, **props)
382 self._out.write(templater.stringify(g))
390 self._out.write(templater.stringify(g))
383
391
392 def end(self):
393 baseformatter.end(self)
394 self._renderitem('docfooter', {})
395
384 templatespec = collections.namedtuple(r'templatespec',
396 templatespec = collections.namedtuple(r'templatespec',
385 r'ref tmpl mapfile')
397 r'ref tmpl mapfile')
386
398
@@ -434,6 +446,13 b' def lookuptemplate(ui, topic, tmpl):'
434 # constant string?
446 # constant string?
435 return templatespec('', tmpl, None)
447 return templatespec('', tmpl, None)
436
448
449 def templatepartsmap(spec, t, partnames):
450 """Create a mapping of {part: ref}"""
451 partsmap = {spec.ref: spec.ref} # initial ref must exist in t
452 if spec.mapfile:
453 partsmap.update((p, p) for p in partnames if p in t)
454 return partsmap
455
437 def loadtemplater(ui, spec, cache=None):
456 def loadtemplater(ui, spec, cache=None):
438 """Create a templater from either a literal template or loading from
457 """Create a templater from either a literal template or loading from
439 a map file"""
458 a map file"""
@@ -530,6 +530,19 b' template output:'
530 a: Adding b branch head 2
530 a: Adding b branch head 2
531 default: Adding root node
531 default: Adding root node
532
532
533 $ cat <<'EOF' > "$TESTTMP/map-myjson"
534 > docheader = '\{\n'
535 > docfooter = '\n}\n'
536 > branches = '{ifeq(index, 0, "", ",\n")} {dict(branch, node|short)|json}'
537 > EOF
538 $ hg branches -T "$TESTTMP/map-myjson"
539 {
540 {"branch": "b", "node": "e23b5505d1ad"},
541 {"branch": "a branch *", "node": "10ff5895aa57"}, (glob)
542 {"branch": "a", "node": "d8cbc61dbaa6"},
543 {"branch": "default", "node": "19709c5a4e75"}
544 }
545
533 Tests of revision branch name caching
546 Tests of revision branch name caching
534
547
535 We rev branch cache is updated automatically. In these tests we use a trick to
548 We rev branch cache is updated automatically. In these tests we use a trick to
General Comments 0
You need to be logged in to leave comments. Login now