changeset 37398:3235afdfcf1c

templater: add function that expands internal literal templates This will be used when rendering nested formatter items with the default template, e.g. fm.nested('parents', tmpl='{rev}:{node|formatnode}', sep=' ') ^^^^^^^^^^^^^^^^^^^^^^^ the default item template
author Yuya Nishihara <yuya@tcha.org>
date Sat, 17 Mar 2018 22:56:49 +0900
parents 46d9f998c3ed
children 0b64416224d9
files mercurial/templater.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Fri Apr 06 11:28:26 2018 -0700
+++ b/mercurial/templater.py	Sat Mar 17 22:56:49 2018 +0900
@@ -603,6 +603,7 @@
         self._resources = resources
         self._aliasmap = _aliasrules.buildmap(aliases)
         self._cache = {}  # key: (func, data)
+        self._tmplcache = {}  # literal template: (func, data)
 
     def overlaymap(self, origmapping, newmapping):
         """Create combined mapping from the original mapping and partial
@@ -659,6 +660,13 @@
                 raise
         return self._cache[t]
 
+    def _parse(self, tmpl):
+        """Parse and cache a literal template"""
+        if tmpl not in self._tmplcache:
+            x = parse(tmpl)
+            self._tmplcache[tmpl] = compileexp(x, self, methods)
+        return self._tmplcache[tmpl]
+
     def preload(self, t):
         """Load, parse, and cache the specified template if available"""
         try:
@@ -672,6 +680,18 @@
         mapping contains added elements for use during expansion. Is a
         generator.'''
         func, data = self._load(t)
+        return self._expand(func, data, mapping)
+
+    def expand(self, tmpl, mapping):
+        """Perform expansion over a literal template
+
+        No user aliases will be expanded since this is supposed to be called
+        with an internal template string.
+        """
+        func, data = self._parse(tmpl)
+        return self._expand(func, data, mapping)
+
+    def _expand(self, func, data, mapping):
         # populate additional items only if they don't exist in the given
         # mapping. this is slightly different from overlaymap() because the
         # initial 'revcache' may contain pre-computed items.