# HG changeset patch # User Yuya Nishihara # Date 1525313872 -32400 # Node ID e637dc0b3b1f9fb53562772969cfe7e3981441a8 # Parent 48289eafb37d43d92f77f3b50dfe2e6180ae4ae9 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. diff -r 48289eafb37d -r e637dc0b3b1f hgext/schemes.py --- 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)): diff -r 48289eafb37d -r e637dc0b3b1f mercurial/templater.py --- 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,