[PATCH 1 of 6] templater: add simple interface for unnamed template (API)
Yuya Nishihara
yuya at tcha.org
Fri Jun 16 16:37:07 UTC 2017
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1492858607 -32400
# Sat Apr 22 19:56:47 2017 +0900
# Node ID a9aa703f63dc4bd994bb82c980cb5f0847b9a6ba
# Parent 9fcb6df413c9ca475e705ecc15df07584dadb0c1
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.
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2297,7 +2297,7 @@ def _graphnodeformatter(ui, displayer):
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 @@ def _graphnodeformatter(ui, displayer):
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,
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -2121,9 +2121,9 @@ def debugtemplate(ui, repo, tmpl, **opts
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:
diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -534,10 +534,10 @@ def _formatconflictmarker(repo, ctx, tem
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 @@ def _formatlabels(repo, fcd, fco, fca, l
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)
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -431,10 +431,13 @@ def loadtemplater(ui, spec, cache=None):
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:
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1298,6 +1298,10 @@ class templater(object):
(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:
More information about the Mercurial-devel
mailing list