changeset 17635:8804e3cb51bd

templater: add sub() function
author Matt Mackall <mpm@selenic.com>
date Mon, 24 Sep 2012 15:26:17 -0500
parents 6b307730c9f0
children ce18dbcd91c8
files mercurial/templater.py
diffstat 1 files changed, 11 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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