comparison mercurial/templater.py @ 36982:255f635c3204

templater: convert resources to a table of callables for future extension I'm going to add a full templating support to the annotate command. As the annotate is a filectx-oriented command, we'll need a way to look up a ctx from a bounded fctx only when necessary. This is the minimal change to support that. I'm thinking of defining a proper interface to look up template resources to fix other issues, but that isn't ready yet. (Changes from V1: just updated tests and patch descriptions.)
author Yuya Nishihara <yuya@tcha.org>
date Sun, 11 Mar 2018 21:05:29 +0900
parents 521f6c7e1756
children 036e4483d3a1
comparison
equal deleted inserted replaced
36978:c479692690ef 36982:255f635c3204
564 """Return internal data (e.g. cache) used for keyword/function 564 """Return internal data (e.g. cache) used for keyword/function
565 evaluation""" 565 evaluation"""
566 v = None 566 v = None
567 if key in self._resources: 567 if key in self._resources:
568 v = mapping.get(key) 568 v = mapping.get(key)
569 if v is None: 569 if v is None and key in self._resources:
570 v = self._resources.get(key) 570 v = self._resources[key](self, mapping, key)
571 if v is None: 571 if v is None:
572 raise templateutil.ResourceUnavailable( 572 raise templateutil.ResourceUnavailable(
573 _('template resource not available: %s') % key) 573 _('template resource not available: %s') % key)
574 return v 574 return v
575 575
668 """Create template engine optionally with preloaded template fragments 668 """Create template engine optionally with preloaded template fragments
669 669
670 - ``filters``: a dict of functions to transform a value into another. 670 - ``filters``: a dict of functions to transform a value into another.
671 - ``defaults``: a dict of symbol values/functions; may be overridden 671 - ``defaults``: a dict of symbol values/functions; may be overridden
672 by a ``mapping`` dict. 672 by a ``mapping`` dict.
673 - ``resources``: a dict of internal data (e.g. cache), inaccessible 673 - ``resources``: a dict of functions returning internal data
674 from user template; may be overridden by a ``mapping`` dict. 674 (e.g. cache), inaccessible from user template; may be overridden by
675 a ``mapping`` dict.
675 - ``cache``: a dict of preloaded template fragments. 676 - ``cache``: a dict of preloaded template fragments.
676 - ``aliases``: a list of alias (name, replacement) pairs. 677 - ``aliases``: a list of alias (name, replacement) pairs.
677 678
678 self.cache may be updated later to register additional template 679 self.cache may be updated later to register additional template
679 fragments. 680 fragments.
689 self.cache = cache.copy() 690 self.cache = cache.copy()
690 self.map = {} 691 self.map = {}
691 self.filters = templatefilters.filters.copy() 692 self.filters = templatefilters.filters.copy()
692 self.filters.update(filters) 693 self.filters.update(filters)
693 self.defaults = defaults 694 self.defaults = defaults
694 self._resources = {'templ': self} 695 self._resources = {'templ': lambda context, mapping, key: self}
695 self._resources.update(resources) 696 self._resources.update(resources)
696 self._aliases = aliases 697 self._aliases = aliases
697 self.minchunk, self.maxchunk = minchunk, maxchunk 698 self.minchunk, self.maxchunk = minchunk, maxchunk
698 self.ecache = {} 699 self.ecache = {}
699 700