[PATCH 9 of 9] templatekw: add compatlist() as a replacement for showlist()

Yuya Nishihara yuya at tcha.org
Tue Feb 27 09:58:02 EST 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1519542877 -32400
#      Sun Feb 25 16:14:37 2018 +0900
# Node ID 1a1f7b96d833af5a29d6d75580d083511ff388f6
# Parent  e6b41f06c7bbcbc745814ec066c7f660d63d200f
templatekw: add compatlist() as a replacement for showlist()

Just like compatdict(), this is mostly a copy of showlist(). showchildren()
is ported to the new API as an example.

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -148,6 +148,17 @@ def compatdict(context, mapping, name, d
     f = _showlist(name, c, t, mapping, plural, separator)
     return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
 
+def compatlist(context, mapping, name, data, element=None, fmt='%s',
+               plural=None, separator=' '):
+    """Wrap data like hybridlist(), but also supports old-style list template
+
+    This exists for backward compatibility with the old-style template. Use
+    hybridlist() for new template keywords.
+    """
+    t = context.resource(mapping, 'templ')
+    f = _showlist(name, data, t, mapping, plural, separator)
+    return hybridlist(data, name=element or name, fmt=fmt, gen=f)
+
 def showdict(name, data, mapping, plural=None, key='key', value='value',
              fmt='%s=%s', separator=' '):
     c = [{key: k, value: v} for k, v in data.iteritems()]
@@ -397,13 +408,12 @@ def showbookmarks(**args):
     f = _showlist('bookmark', bookmarks, args['templ'], args)
     return _hybrid(f, bookmarks, makemap, pycompat.identity)
 
- at templatekeyword('children')
-def showchildren(**args):
+ at templatekeyword('children', requires={'ctx', 'templ'})
+def showchildren(context, mapping):
     """List of strings. The children of the changeset."""
-    args = pycompat.byteskwargs(args)
-    ctx = args['ctx']
+    ctx = context.resource(mapping, 'ctx')
     childrevs = ['%d:%s' % (cctx.rev(), cctx) for cctx in ctx.children()]
-    return showlist('children', childrevs, args, element='child')
+    return compatlist(context, mapping, 'children', childrevs, element='child')
 
 # Deprecated, but kept alive for help generation a purpose.
 @templatekeyword('currentbookmark', requires={'repo', 'ctx'})


More information about the Mercurial-devel mailing list