[PATCH 3 of 5] templater: factor out helper that renders named template as string
Yuya Nishihara
yuya at tcha.org
Sun Mar 18 00:39:39 EDT 2018
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1521203052 -32400
# Fri Mar 16 21:24:12 2018 +0900
# Node ID 38286983261c9f01bb71cd7994033cd3f96e8d38
# Parent 3cc6b81c3c742e32662b35c74ccc81e1950c5ecb
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.
diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -401,9 +401,7 @@ class templateformatter(baseformatter):
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)
diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -30,7 +30,6 @@ from .. import (
repoview,
templatefilters,
templater,
- templateutil,
ui as uimod,
util,
wireprotoserver,
@@ -378,8 +377,8 @@ class hgweb(object):
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':
diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -35,7 +35,6 @@ from .. import (
pycompat,
scmutil,
templater,
- templateutil,
ui as uimod,
util,
)
@@ -381,8 +380,7 @@ class hgwebdir(object):
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'
diff --git a/mercurial/logcmdutil.py b/mercurial/logcmdutil.py
--- a/mercurial/logcmdutil.py
+++ b/mercurial/logcmdutil.py
@@ -33,7 +33,6 @@ from . import (
smartset,
templatekw,
templater,
- templateutil,
util,
)
from .utils import dateutil
@@ -450,15 +449,13 @@ class changesettemplater(changesetprinte
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 @@ class changesettemplater(changesetprinte
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 @@ class changesettemplater(changesetprinte
# 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:
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -725,8 +725,12 @@ class templater(object):
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)
More information about the Mercurial-devel
mailing list