Patchwork [V2] templater: sort functions alphabetically, as filters are

login
register
mail settings
Submitter Alexander Plavin
Date July 10, 2013, 10:50 a.m.
Message ID <08eda82129f69895d244.1373453435@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/1828/
State Accepted
Commit 3af3a165db18ed093394fe661e797bea296dc602
Headers show

Comments

Alexander Plavin - July 10, 2013, 10:50 a.m.
# HG changeset patch
# User Alexander Plavin <me@aplavin.ru>
# Date 1372501673 -14400
#      Sat Jun 29 14:27:53 2013 +0400
# Node ID 08eda82129f69895d24405d0f27d56f094a3098f
# Parent  83d0df2ddf3f2a07ed347afcfa30ddacc18b4c3d
templater: sort functions alphabetically, as filters are

Patch

diff -r 83d0df2ddf3f -r 08eda82129f6 mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Tue Nov 06 00:22:56 2012 +0100
+++ 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:
 
diff -r 83d0df2ddf3f -r 08eda82129f6 mercurial/templater.py
--- a/mercurial/templater.py	Tue Nov 06 00:22:56 2012 +0100
+++ 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