diff --git a/mercurial/help/templates.txt b/mercurial/help/templates.txt --- a/mercurial/help/templates.txt +++ b/mercurial/help/templates.txt @@ -44,19 +44,21 @@ Note that a filter is nothing more than In addition to filters, there are some basic built-in functions: +- date(date[, fmt]) + +- fill(text[, width]) + +- get(dict, key) + - if(expr, then[, else]) - ifeq(expr, expr, then[, else]) -- sub(pat, repl, expr) - - join(list, sep) - label(label, expr) -- date(date[, fmt]) - -- fill(text[, width]) +- sub(pat, repl, expr) Also, for any expression that returns a list, there is a list operator: diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -207,6 +207,19 @@ def buildfunc(exp, context): f = context._filters[n] return (runfilter, (args[0][0], args[0][1], f)) +def get(context, mapping, args): + if len(args) != 2: + # i18n: "get" is a keyword + raise error.ParseError(_("get() expects two arguments")) + + dictarg = args[0][0](context, mapping, args[0][1]) + if not util.safehasattr(dictarg, 'get'): + # i18n: "get" is a keyword + raise error.ParseError(_("get() expects a dict as first argument")) + + key = args[1][0](context, mapping, args[1][1]) + yield dictarg.get(key) + def join(context, mapping, args): if not (1 <= len(args) <= 2): # i18n: "join" is a keyword @@ -285,11 +298,12 @@ methods = { } funcs = { + "get": get, "if": if_, "ifeq": ifeq, "join": join, + "label": label, "sub": sub, - "label": label, } # template engine