changeset 17633:312184f930b7

template: add join function This allows: {join(files % "{files}", ", ") }\n to produce a properly comma-separated list
author Matt Mackall <mpm@selenic.com>
date Sat, 22 Sep 2012 13:04:36 -0500
parents 523625e46760
children 6b307730c9f0
files mercurial/templater.py
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sat Sep 22 13:02:33 2012 -0500
+++ b/mercurial/templater.py	Sat Sep 22 13:04:36 2012 -0500
@@ -195,6 +195,26 @@
         f = context._filters[n]
         return (runfilter, (args[0][0], args[0][1], f))
 
+def join(context, mapping, args):
+    if not (1 <= len(args) <= 2):
+        raise error.ParseError(_("join expects one or two arguments"))
+
+    joinset = args[0][0](context, mapping, args[0][1])
+    if util.safehasattr(joinset, '__call__'):
+        joinset = [x.values()[0] for x in joinset()]
+
+    joiner = " "
+    if len(args) > 1:
+        joiner = args[1][0](context, mapping, args[1][1])
+
+    first = True
+    for x in joinset:
+        if first:
+            first = False
+        else:
+            yield joiner
+        yield x
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "symbol": lambda e, c: (runsymbol, e[1]),
@@ -206,6 +226,7 @@
     }
 
 funcs = {
+    "join": join,
 }
 
 # template engine