[PATCH V3] templater: add strip function with chars as an extra argument

Kevin Bullock kbullock+mercurial at ringworld.org
Wed Jun 26 07:24:12 CDT 2013


On 25 Jun 2013, at 3:41 PM, Alexander Plavin wrote:

> # HG changeset patch
> # User Alexander Plavin <me at aplavin.ru>
> # Date 1372179742 -14400
> #      Tue Jun 25 21:02:22 2013 +0400
> # Node ID f21a44fe66da3d66313b71015947456f5a3789cd
> # Parent  1bef6f99a12d9062e737bb623da627719a3987e6
> templater: add strip function with chars as an extra argument

LGTM

pacem in terris / мир / शान्ति / ‎‫سَلاَم‬ / 平和
Kevin R. Bullock

> This allows specifying characters to strip, like the Python strip function.
> 
> diff -r 1bef6f99a12d -r f21a44fe66da 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)
> 
> +- strip(text, chars)
> +
> Also, for any expression that returns a list, there is a list operator:
> 
> - expr % "{template}"
> diff -r 1bef6f99a12d -r f21a44fe66da 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
> diff -r 1bef6f99a12d -r f21a44fe66da tests/test-command-template.t
> --- a/tests/test-command-template.t	Thu May 23 17:53:38 2013 -0500
> +++ b/tests/test-command-template.t	Tue Jun 25 21:02:22 2013 +0400
> @@ -1536,3 +1536,31 @@
> 
>   $ hg log -R latesttag -r 10 --template '{sub("[0-9]", "x", "{rev}")}\n'
>   xx
> +
> +Test the strip function with chars specified:
> +
> +  $ hg log -R latesttag --template '{desc}\n'
> +  at3
> +  t5
> +  t3
> +  t2
> +  t1
> +  merge
> +  h2e
> +  h2d
> +  h1c
> +  b
> +  a
> +
> +  $ hg log -R latesttag --template '{strip(desc, "te")}\n'
> +  at3
> +  5
> +  3
> +  2
> +  1
> +  merg
> +  h2
> +  h2d
> +  h1c
> +  b
> +  a
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel



pacem in terris / мир / शान्ति / ‎‫سَلاَم‬ / 平和
Kevin R. Bullock



More information about the Mercurial-devel mailing list