comparison mercurial/templatefuncs.py @ 38448: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
comparison
equal deleted inserted replaced
38447:b6294c113794 38448:dae829b4de78
164 except IndexError: 164 except IndexError:
165 pass 165 pass
166 166
167 return templatefilters.fill(text, width, initindent, hangindent) 167 return templatefilters.fill(text, width, initindent, hangindent)
168 168
169 @templatefunc('filter(iterable)')
170 def filter_(context, mapping, args):
171 """Remove empty elements from a list or a dict."""
172 if len(args) != 1:
173 # i18n: "filter" is a keyword
174 raise error.ParseError(_("filter expects one argument"))
175 iterable = evalwrapped(context, mapping, args[0])
176 def select(w):
177 return w.tobool(context, mapping)
178 return iterable.filter(context, mapping, select)
179
169 @templatefunc('formatnode(node)', requires={'ui'}) 180 @templatefunc('formatnode(node)', requires={'ui'})
170 def formatnode(context, mapping, args): 181 def formatnode(context, mapping, args):
171 """Obtain the preferred form of a changeset hash. (DEPRECATED)""" 182 """Obtain the preferred form of a changeset hash. (DEPRECATED)"""
172 if len(args) != 1: 183 if len(args) != 1:
173 # i18n: "formatnode" is a keyword 184 # i18n: "formatnode" is a keyword