Patchwork [V2] templater: add strip function with chars as an extra argument

login
register
mail settings
Submitter Alexander Plavin
Date June 25, 2013, 7:05 p.m.
Message ID <024a78c01c7347739a4f.1372187128@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/1746/
State Superseded, archived
Headers show

Comments

Alexander Plavin - June 25, 2013, 7:05 p.m.
# HG changeset patch
# User Alexander Plavin <me@aplavin.ru>
# Date 1372179742 -14400
#      Tue Jun 25 21:02:22 2013 +0400
# Node ID 024a78c01c7347739a4ff7c52c2eed046ed5b2f8
# Parent  1bef6f99a12d9062e737bb623da627719a3987e6
templater: add strip function with chars as an extra argument

This allows specifying characters to strip, like the Python strip function.

Patch

diff -r 1bef6f99a12d -r 024a78c01c73 mercurial/help/templates.txt
--- a/mercurial/help/templates.txt	Thu May 23 17:53:38 2013 -0500
+++ b/mercurial/help/templates.txt	Tue Jun 25 21:02:22 2013 +0400
@@ -62,6 +62,8 @@ 
 
 - rstdoc(text, style)
 
+- split(text, chars)
+
 Also, for any expression that returns a list, there is a list operator:
 
 - expr % "{template}"
diff -r 1bef6f99a12d -r 024a78c01c73 mercurial/templater.py
--- a/mercurial/templater.py	Thu May 23 17:53:38 2013 -0500
+++ b/mercurial/templater.py	Tue Jun 25 21:02:22 2013 +0400
@@ -333,6 +333,16 @@ 
         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"))
+
+    text = args[0][0](context, mapping, args[0][1])
+    if len(args) == 2:
+        chars = args[1][0](context, mapping, args[1][1])
+        return text.strip(chars)
+    return text.strip()
+
 methods = {
     "string": lambda e, c: (runstring, e[1]),
     "symbol": lambda e, c: (runsymbol, e[1]),
@@ -353,6 +363,7 @@ 
     "sub": sub,
     "fill": fill,
     "date": date,
+    "strip": strip,
 }
 
 # template engine