changeset 32873:2ecce24dfcd3

templater: add simple interface for unnamed template (API) This provides a simpler API for callers which don't need full templating stack. Instead of storing the given template as the name specified by topic, use '' as the default template to be rendered.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 22 Apr 2017 19:56:47 +0900
parents 9fcb6df413c9
children dddba6f3e59c
files mercurial/cmdutil.py mercurial/debugcommands.py mercurial/filemerge.py mercurial/formatter.py mercurial/templater.py
diffstat 5 files changed, 17 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cmdutil.py	Wed Jun 14 20:56:34 2017 -0400
+++ b/mercurial/cmdutil.py	Sat Apr 22 19:56:47 2017 +0900
@@ -2297,7 +2297,7 @@
         return templatekw.showgraphnode  # fast path for "{graphnode}"
 
     spec = templater.unquotestring(spec)
-    templ = formatter.maketemplater(ui, 'graphnode', spec)
+    templ = formatter.maketemplater(ui, spec)
     cache = {}
     if isinstance(displayer, changeset_templater):
         cache = displayer.cache  # reuse cache of slow templates
@@ -2309,7 +2309,7 @@
         props['repo'] = repo
         props['ui'] = repo.ui
         props['revcache'] = {}
-        return templater.stringify(templ('graphnode', **props))
+        return templ.render(props)
     return formatnode
 
 def displaygraph(ui, repo, dag, displayer, edgefn, getrenamed=None,
--- a/mercurial/debugcommands.py	Wed Jun 14 20:56:34 2017 -0400
+++ b/mercurial/debugcommands.py	Sat Apr 22 19:56:47 2017 +0900
@@ -2121,9 +2121,9 @@
             ui.note(("* expanded:\n"), templater.prettyformat(newtree), '\n')
 
     if revs is None:
-        k = 'debugtemplate'
-        t = formatter.maketemplater(ui, k, tmpl)
-        ui.write(templater.stringify(t(k, ui=ui, **props)))
+        t = formatter.maketemplater(ui, tmpl)
+        props['ui'] = ui
+        ui.write(t.render(props))
     else:
         displayer = cmdutil.makelogtemplater(ui, repo, tmpl)
         for r in revs:
--- a/mercurial/filemerge.py	Wed Jun 14 20:56:34 2017 -0400
+++ b/mercurial/filemerge.py	Sat Apr 22 19:56:47 2017 +0900
@@ -534,10 +534,10 @@
     props['templ'] = template
     props['ctx'] = ctx
     props['repo'] = repo
-    templateresult = template('conflictmarker', **props)
+    templateresult = template.render(props)
 
     label = ('%s:' % label).ljust(pad + 1)
-    mark = '%s %s' % (label, templater.stringify(templateresult))
+    mark = '%s %s' % (label, templateresult)
 
     if mark:
         mark = mark.splitlines()[0] # split for safety
@@ -566,7 +566,7 @@
     ui = repo.ui
     template = ui.config('ui', 'mergemarkertemplate', _defaultconflictmarker)
     template = templater.unquotestring(template)
-    tmpl = formatter.maketemplater(ui, 'conflictmarker', template)
+    tmpl = formatter.maketemplater(ui, template)
 
     pad = max(len(l) for l in labels)
 
--- a/mercurial/formatter.py	Wed Jun 14 20:56:34 2017 -0400
+++ b/mercurial/formatter.py	Sat Apr 22 19:56:47 2017 +0900
@@ -431,10 +431,13 @@
     assert not (spec.tmpl and spec.mapfile)
     if spec.mapfile:
         return templater.templater.frommapfile(spec.mapfile, cache=cache)
-    return maketemplater(ui, spec.ref, spec.tmpl, cache=cache)
+    return _maketemplater(ui, spec.ref, spec.tmpl, cache=cache)
 
-def maketemplater(ui, topic, tmpl, cache=None):
+def maketemplater(ui, tmpl, cache=None):
     """Create a templater from a string template 'tmpl'"""
+    return _maketemplater(ui, '', tmpl, cache=cache)
+
+def _maketemplater(ui, topic, tmpl, cache=None):
     aliases = ui.configitems('templatealias')
     t = templater.templater(cache=cache, aliases=aliases)
     if tmpl:
--- a/mercurial/templater.py	Wed Jun 14 20:56:34 2017 -0400
+++ b/mercurial/templater.py	Sat Apr 22 19:56:47 2017 +0900
@@ -1298,6 +1298,10 @@
                               (self.map[t][1], inst.args[1]))
         return self.cache[t]
 
+    def render(self, mapping):
+        """Render the default unnamed template and return result as string"""
+        return stringify(self('', **mapping))
+
     def __call__(self, t, **mapping):
         ttype = t in self.map and self.map[t][0] or 'default'
         if ttype not in self.ecache: