diff mercurial/formatter.py @ 35472:32c278eb876f

templater: keep default resources per template engine (API) This allows us to register a repo object as a resource in hgweb template, without loosing '{repo}' symbol: symbol('repo') -> mapping['repo'] (n/a) -> defaults['repo'] resource('repo') -> mapping['repo'] (n/a) -> resources['repo'] I'm thinking of redesigning the templatekw API to take (context, mapping) in place of **(context._resources + mapping), but that will be a big change and not implemented yet. props['templ'] is ported to the resources dict as an example. .. api:: mapping does not contain all template resources. use context.resource() in template functions.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 21 Dec 2017 21:29:06 +0900
parents f17a0e18c47e
children f1c54d003327
line wrap: on
line diff
--- a/mercurial/formatter.py	Thu Dec 21 21:03:25 2017 +0900
+++ b/mercurial/formatter.py	Thu Dec 21 21:29:06 2017 +0900
@@ -392,7 +392,6 @@
         props.update(item)
         if 'ctx' in item:
             # but template resources must be always available
-            props['templ'] = self._t
             props['repo'] = props['ctx'].repo()
             props['revcache'] = {}
         props = pycompat.strkwargs(props)
@@ -468,18 +467,19 @@
                 partsmap[part] = ref
     return partsmap
 
-def loadtemplater(ui, spec, cache=None):
+def loadtemplater(ui, spec, resources=None, cache=None):
     """Create a templater from either a literal template or loading from
     a map file"""
     assert not (spec.tmpl and spec.mapfile)
     if spec.mapfile:
-        return templater.templater.frommapfile(spec.mapfile, cache=cache)
-    return maketemplater(ui, spec.tmpl, cache=cache)
+        frommapfile = templater.templater.frommapfile
+        return frommapfile(spec.mapfile, resources=resources, cache=cache)
+    return maketemplater(ui, spec.tmpl, resources=resources, cache=cache)
 
-def maketemplater(ui, tmpl, cache=None):
+def maketemplater(ui, tmpl, resources=None, cache=None):
     """Create a templater from a string template 'tmpl'"""
     aliases = ui.configitems('templatealias')
-    t = templater.templater(cache=cache, aliases=aliases)
+    t = templater.templater(resources=resources, cache=cache, aliases=aliases)
     t.cache.update((k, templater.unquotestring(v))
                    for k, v in ui.configitems('templates'))
     if tmpl: