templater: parse template string to tree by templater class
The parsed tree could be cached, but it isn't for now. We can add a cache
later if that matters.
--- a/hgext/schemes.py Thu May 03 10:58:56 2018 +0900
+++ b/hgext/schemes.py Thu May 03 11:17:52 2018 +0900
@@ -114,7 +114,7 @@
def extsetup(ui):
schemes.update(dict(ui.configitems('schemes')))
- t = templater.engine(lambda x: x)
+ t = templater.engine(templater.parse)
for scheme, url in schemes.items():
if (pycompat.iswindows and len(scheme) == 1 and scheme.isalpha()
and os.path.exists('%s:\\' % scheme)):
--- a/mercurial/templater.py Thu May 03 10:58:56 2018 +0900
+++ b/mercurial/templater.py Thu May 03 11:17:52 2018 +0900
@@ -597,8 +597,7 @@
filter uses function to transform value. syntax is
{key|filter1|filter2|...}.'''
- def __init__(self, loader, filters=None, defaults=None, resources=None,
- aliases=()):
+ def __init__(self, loader, filters=None, defaults=None, resources=None):
self._loader = loader
if filters is None:
filters = {}
@@ -610,7 +609,6 @@
resources = nullresourcemapper()
self._defaults = defaults
self._resources = resources
- self._aliasmap = _aliasrules.buildmap(aliases)
self._cache = {} # key: (func, data)
self._tmplcache = {} # literal template: (func, data)
@@ -665,9 +663,7 @@
def _load(self, t):
'''load, parse, and cache a template'''
if t not in self._cache:
- x = parse(self._loader(t))
- if self._aliasmap:
- x = _aliasrules.expand(self._aliasmap, x)
+ x = self._loader(t)
# put poison to cut recursion while compiling 't'
self._cache[t] = (_runrecursivesymbol, t)
try:
@@ -808,7 +804,7 @@
self._filters.update(filters)
self.defaults = defaults
self._resources = resources
- self._aliases = aliases
+ self._aliasmap = _aliasrules.buildmap(aliases)
self._minchunk, self._maxchunk = minchunk, maxchunk
@classmethod
@@ -819,14 +815,14 @@
cache, tmap, aliases = _readmapfile(mapfile)
t.cache.update(cache)
t._map = tmap
- t._aliases = aliases
+ t._aliasmap = _aliasrules.buildmap(aliases)
return t
def __contains__(self, key):
return key in self.cache or key in self._map
def load(self, t):
- '''Get the template for the given template name. Use a local cache.'''
+ """Get parsed tree for the given template name. Use a local cache."""
if t not in self.cache:
try:
self.cache[t] = util.readfile(self._map[t])
@@ -838,7 +834,13 @@
% (self._map[t],
stringutil.forcebytestr(inst.args[1])))
raise IOError(inst.args[0], encoding.strfromlocal(reason))
- return self.cache[t]
+ return self._parse(self.cache[t])
+
+ def _parse(self, tmpl):
+ x = parse(tmpl)
+ if self._aliasmap:
+ x = _aliasrules.expand(self._aliasmap, x)
+ return x
def renderdefault(self, mapping):
"""Render the default unnamed template and return result as string"""
@@ -851,8 +853,7 @@
def generate(self, t, mapping):
"""Return a generator that renders the specified named template and
yields chunks"""
- proc = engine(self.load, self._filters, self.defaults, self._resources,
- self._aliases)
+ proc = engine(self.load, self._filters, self.defaults, self._resources)
stream = proc.process(t, mapping)
if self._minchunk:
stream = util.increasingchunks(stream, min=self._minchunk,