templater: factor out helper that renders named template as string
This is quite common in non-web templating, and **kwargs expansion is annoying
because of the unicode-ness of Python3.
--- a/mercurial/formatter.py Sat Mar 17 11:23:04 2018 +0900
+++ b/mercurial/formatter.py Fri Mar 16 21:24:12 2018 +0900
@@ -401,9 +401,7 @@
if 'ctx' in item or 'fctx' in item:
# but template resources must be always available
props['revcache'] = {}
- props = pycompat.strkwargs(props)
- g = self._t(ref, **props)
- self._out.write(templateutil.stringify(g))
+ self._out.write(self._t.render(ref, props))
def end(self):
baseformatter.end(self)
--- a/mercurial/hgweb/hgweb_mod.py Sat Mar 17 11:23:04 2018 +0900
+++ b/mercurial/hgweb/hgweb_mod.py Fri Mar 16 21:24:12 2018 +0900
@@ -30,7 +30,6 @@
repoview,
templatefilters,
templater,
- templateutil,
ui as uimod,
util,
wireprotoserver,
@@ -378,8 +377,8 @@
try:
rctx.tmpl = rctx.templater(req)
- ctype = rctx.tmpl('mimetype', encoding=encoding.encoding)
- ctype = templateutil.stringify(ctype)
+ ctype = rctx.tmpl.render('mimetype',
+ {'encoding': encoding.encoding})
# check read permissions non-static content
if cmd != 'static':
--- a/mercurial/hgweb/hgwebdir_mod.py Sat Mar 17 11:23:04 2018 +0900
+++ b/mercurial/hgweb/hgwebdir_mod.py Fri Mar 16 21:24:12 2018 +0900
@@ -35,7 +35,6 @@
pycompat,
scmutil,
templater,
- templateutil,
ui as uimod,
util,
)
@@ -381,8 +380,7 @@
virtual = req.dispatchpath.strip('/')
tmpl = self.templater(req, nonce)
- ctype = tmpl('mimetype', encoding=encoding.encoding)
- ctype = templateutil.stringify(ctype)
+ ctype = tmpl.render('mimetype', {'encoding': encoding.encoding})
# Global defaults. These can be overridden by any handler.
res.status = '200 Script output follows'
--- a/mercurial/logcmdutil.py Sat Mar 17 11:23:04 2018 +0900
+++ b/mercurial/logcmdutil.py Fri Mar 16 21:24:12 2018 +0900
@@ -33,7 +33,6 @@
smartset,
templatekw,
templater,
- templateutil,
util,
)
from .utils import dateutil
@@ -450,15 +449,13 @@
self._parts.update(m)
if self._parts['docheader']:
- self.ui.write(
- templateutil.stringify(self.t(self._parts['docheader'])))
+ self.ui.write(self.t.render(self._parts['docheader'], {}))
def close(self):
if self._parts['docfooter']:
if not self.footer:
self.footer = ""
- self.footer += templateutil.stringify(
- self.t(self._parts['docfooter']))
+ self.footer += self.t.render(self._parts['docfooter'], {})
return super(changesettemplater, self).close()
def _show(self, ctx, copies, props):
@@ -467,18 +464,16 @@
props['ctx'] = ctx
props['index'] = index = next(self._counter)
props['revcache'] = {'copies': copies}
- props = pycompat.strkwargs(props)
# write separator, which wouldn't work well with the header part below
# since there's inherently a conflict between header (across items) and
# separator (per item)
if self._parts['separator'] and index > 0:
- self.ui.write(
- templateutil.stringify(self.t(self._parts['separator'])))
+ self.ui.write(self.t.render(self._parts['separator'], {}))
# write header
if self._parts['header']:
- h = templateutil.stringify(self.t(self._parts['header'], **props))
+ h = self.t.render(self._parts['header'], props)
if self.buffered:
self.header[ctx.rev()] = h
else:
@@ -488,13 +483,12 @@
# write changeset metadata, then patch if requested
key = self._parts[self._tref]
- self.ui.write(templateutil.stringify(self.t(key, **props)))
+ self.ui.write(self.t.render(key, props))
self._showpatch(ctx)
if self._parts['footer']:
if not self.footer:
- self.footer = templateutil.stringify(
- self.t(self._parts['footer'], **props))
+ self.footer = self.t.render(self._parts['footer'], props)
def templatespec(tmpl, mapfile):
if mapfile:
--- a/mercurial/templater.py Sat Mar 17 11:23:04 2018 +0900
+++ b/mercurial/templater.py Fri Mar 16 21:24:12 2018 +0900
@@ -725,8 +725,12 @@
def renderdefault(self, mapping):
"""Render the default unnamed template and return result as string"""
+ return self.render('', mapping)
+
+ def render(self, t, mapping):
+ """Render the specified named template and return result as string"""
mapping = pycompat.strkwargs(mapping)
- return templateutil.stringify(self('', **mapping))
+ return templateutil.stringify(self(t, **mapping))
def __call__(self, t, **mapping):
mapping = pycompat.byteskwargs(mapping)