templater: add sub() function
authorMatt Mackall <mpm@selenic.com>
Mon, 24 Sep 2012 15:26:17 -0500
changeset 17635 8804e3cb51bd
parent 17634 6b307730c9f0
child 17636 ce18dbcd91c8
templater: add sub() function
mercurial/templater.py
--- a/mercurial/templater.py	Mon Sep 24 15:24:27 2012 -0500
+++ b/mercurial/templater.py	Mon Sep 24 15:26:17 2012 -0500
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
-import sys, os
+import sys, os, re
 import util, config, templatefilters, parser, error
 
 # template parsing
@@ -218,6 +218,15 @@
             yield joiner
         yield x
 
+def sub(context, mapping, args):
+    if len(args) != 3:
+        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]))
+    yield re.sub(pat, rpl, src)
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "symbol": lambda e, c: (runsymbol, e[1]),
@@ -230,6 +239,7 @@
 
 funcs = {
     "join": join,
+    "sub": sub,
 }
 
 # template engine