template: add join function
authorMatt Mackall <mpm@selenic.com>
Sat, 22 Sep 2012 13:04:36 -0500
changeset 17633 312184f930b7
parent 17632 523625e46760
child 17634 6b307730c9f0
template: add join function This allows: {join(files % "{files}", ", ") }\n to produce a properly comma-separated list
mercurial/templater.py
--- 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