Mercurial > hg-stable
changeset 19390:3af3a165db18
templater: sort functions alphabetically, as filters are
author | Alexander Plavin <me@aplavin.ru> |
---|---|
date | Sat, 29 Jun 2013 14:27:53 +0400 |
parents | 41c4bdd1d585 |
children | 33e1b2d4bdbc |
files | mercurial/help/templates.txt mercurial/templater.py |
diffstat | 2 files changed, 75 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/help/templates.txt Thu Jul 04 14:19:43 2013 +0400 +++ b/mercurial/help/templates.txt Sat Jun 29 14:27:53 2013 +0400 @@ -58,11 +58,11 @@ - label(label, expr) -- sub(pat, repl, expr) - - rstdoc(text, style) -- strip(text, chars) +- strip(text[, chars]) + +- sub(pat, repl, expr) Also, for any expression that returns a list, there is a list operator:
--- a/mercurial/templater.py Thu Jul 04 14:19:43 2013 +0400 +++ b/mercurial/templater.py Sat Jun 29 14:27:53 2013 +0400 @@ -205,6 +205,41 @@ f = context._filters[n] return (runfilter, (args[0][0], args[0][1], f)) +def date(context, mapping, args): + if not (1 <= len(args) <= 2): + raise error.ParseError(_("date expects one or two arguments")) + + date = args[0][0](context, mapping, args[0][1]) + if len(args) == 2: + fmt = stringify(args[1][0](context, mapping, args[1][1])) + return util.datestr(date, fmt) + return util.datestr(date) + +def fill(context, mapping, args): + if not (1 <= len(args) <= 4): + raise error.ParseError(_("fill expects one to four arguments")) + + text = stringify(args[0][0](context, mapping, args[0][1])) + width = 76 + initindent = '' + hangindent = '' + if 2 <= len(args) <= 4: + try: + width = int(stringify(args[1][0](context, mapping, args[1][1]))) + except ValueError: + raise error.ParseError(_("fill expects an integer width")) + try: + initindent = stringify(args[2][0](context, mapping, args[2][1])) + initindent = stringify(runtemplate(context, mapping, + compiletemplate(initindent, context))) + hangindent = stringify(args[3][0](context, mapping, args[3][1])) + hangindent = stringify(runtemplate(context, mapping, + compiletemplate(hangindent, context))) + except IndexError: + pass + + return templatefilters.fill(text, width, initindent, hangindent) + def get(context, mapping, args): if len(args) != 2: # i18n: "get" is a keyword @@ -218,40 +253,6 @@ 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 - raise error.ParseError(_("join expects one or two arguments")) - - joinset = args[0][0](context, mapping, args[0][1]) - if util.safehasattr(joinset, '__call__'): - jf = joinset.joinfmt - joinset = [jf(x) 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 - -def sub(context, mapping, args): - if len(args) != 3: - # i18n: "sub" is a keyword - raise error.ParseError(_("sub expects three arguments")) - - pat = stringify(args[0][0](context, mapping, args[0][1])) - rpl = stringify(args[1][0](context, mapping, args[1][1])) - src = stringify(args[2][0](context, mapping, args[2][1])) - src = stringify(runtemplate(context, mapping, - compiletemplate(src, context))) - yield re.sub(pat, rpl, src) - def if_(context, mapping, args): if not (2 <= len(args) <= 3): # i18n: "if" is a keyword @@ -279,6 +280,28 @@ t = stringify(args[3][0](context, mapping, args[3][1])) yield runtemplate(context, mapping, compiletemplate(t, context)) +def join(context, mapping, args): + if not (1 <= len(args) <= 2): + # i18n: "join" is a keyword + raise error.ParseError(_("join expects one or two arguments")) + + joinset = args[0][0](context, mapping, args[0][1]) + if util.safehasattr(joinset, '__call__'): + jf = joinset.joinfmt + joinset = [jf(x) 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 + def label(context, mapping, args): if len(args) != 2: # i18n: "label" is a keyword @@ -298,41 +321,6 @@ return minirst.format(text, style=style, keep=['verbose']) -def fill(context, mapping, args): - if not (1 <= len(args) <= 4): - raise error.ParseError(_("fill expects one to four arguments")) - - text = stringify(args[0][0](context, mapping, args[0][1])) - width = 76 - initindent = '' - hangindent = '' - if 2 <= len(args) <= 4: - try: - width = int(stringify(args[1][0](context, mapping, args[1][1]))) - except ValueError: - raise error.ParseError(_("fill expects an integer width")) - try: - initindent = stringify(args[2][0](context, mapping, args[2][1])) - initindent = stringify(runtemplate(context, mapping, - compiletemplate(initindent, context))) - hangindent = stringify(args[3][0](context, mapping, args[3][1])) - hangindent = stringify(runtemplate(context, mapping, - compiletemplate(hangindent, context))) - except IndexError: - pass - - return templatefilters.fill(text, width, initindent, hangindent) - -def date(context, mapping, args): - if not (1 <= len(args) <= 2): - raise error.ParseError(_("date expects one or two arguments")) - - date = args[0][0](context, mapping, args[0][1]) - if len(args) == 2: - fmt = stringify(args[1][0](context, mapping, args[1][1])) - return util.datestr(date, fmt) - return util.datestr(date) - def strip(context, mapping, args): if not (1 <= len(args) <= 2): raise error.ParseError(_("strip expects one or two arguments")) @@ -343,6 +331,18 @@ return text.strip(chars) return text.strip() +def sub(context, mapping, args): + if len(args) != 3: + # i18n: "sub" is a keyword + raise error.ParseError(_("sub expects three arguments")) + + pat = stringify(args[0][0](context, mapping, args[0][1])) + rpl = stringify(args[1][0](context, mapping, args[1][1])) + src = stringify(args[2][0](context, mapping, args[2][1])) + src = stringify(runtemplate(context, mapping, + compiletemplate(src, context))) + yield re.sub(pat, rpl, src) + methods = { "string": lambda e, c: (runstring, e[1]), "symbol": lambda e, c: (runsymbol, e[1]), @@ -354,16 +354,16 @@ } funcs = { + "date": date, + "fill": fill, "get": get, "if": if_, "ifeq": ifeq, "join": join, "label": label, "rstdoc": rstdoc, + "strip": strip, "sub": sub, - "fill": fill, - "date": date, - "strip": strip, } # template engine