diff mercurial/templatefuncs.py @ 38453:dae829b4de78

templater: introduce filter() function to remove empty items from list The primary use case is to filter out "tip" from a list of tags.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 14 Jun 2018 22:33:26 +0900
parents aa98392eb5b0
children bc8d925342f0
line wrap: on
line diff
--- a/mercurial/templatefuncs.py	Sun Jun 17 16:10:38 2018 +0900
+++ b/mercurial/templatefuncs.py	Thu Jun 14 22:33:26 2018 +0900
@@ -166,6 +166,17 @@
 
     return templatefilters.fill(text, width, initindent, hangindent)
 
+@templatefunc('filter(iterable)')
+def filter_(context, mapping, args):
+    """Remove empty elements from a list or a dict."""
+    if len(args) != 1:
+        # i18n: "filter" is a keyword
+        raise error.ParseError(_("filter expects one argument"))
+    iterable = evalwrapped(context, mapping, args[0])
+    def select(w):
+        return w.tobool(context, mapping)
+    return iterable.filter(context, mapping, select)
+
 @templatefunc('formatnode(node)', requires={'ui'})
 def formatnode(context, mapping, args):
     """Obtain the preferred form of a changeset hash. (DEPRECATED)"""