[PATCH 1 of 2] templates/filters: add 'stripquote' filter

Matt Mackall mpm at selenic.com
Sun Mar 4 17:45:11 CST 2012


On Thu, 2012-03-01 at 00:39 +0100, Yann E. MORIN wrote:
> # HG changeset patch
> # User "Yann E. MORIN" <yann.morin.1998 at free.fr>
> # Date 1329262818 -3600
> # Node ID 03bbe66e40ad60efb9e9042cdd4d471d296941ff
> # Parent  a4413624d0146f1edaa712a32c36c3409209ed6a
> templates/filters: add 'stripquote' filter
> 
> RFC5322 (Internet Message Format) [0] says that the 'display name' of
> an internet address [1] (what Mercurial calls 'person') can be quoted
> with DQUOTE (ASCII 34: ") if it contains non-atom characters [2].
> For example, dot '.' is a non-atom character.
> 
> The current {author|person} template+filter just extracts the part
> before an email address as-is. This can look ugly, especially on the
> web interface, or when generating output for post-processing...
> 
> Add a template-filter 'stripquote' which, as 'strip' does for spaces,
> removes leading and trailing DQUOTES:
> 
> Given this author: "J. DOE" <john at doe.net>
>     {author|person}             : "J. DOE"
>     {author|person|stripquote}  : J. DOE

I would have guessed that the reason for not just fixing the person
filter would be to preserve backward compatibility.. but then you go and
change the stock command-line templates, which contradicts that goal.

Absent an argument as to why changing the command line output is safe
for all the stupid scripts in the world (could they already be doing
author = author[1:-1]?), you should avoid touching it. 

But if you do have such an argument, it should probably just be rolled
into the person filter.

Please use a doctest for testing simple functions, it has orders of
magnitude less overhead per test:

http://mercurial.selenic.com/wiki/WritingTests#Writing_a_Python_doctest

> [0] https://tools.ietf.org/html/rfc5322
> [1] https://tools.ietf.org/html/rfc5322#section-3.4
> [2] https://tools.ietf.org/html/rfc5322#section-3.2.4
> 
> Signed-off-by: "Yann E. MORIN" <yann.morin.1998 at free.fr>
> 
> diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
> --- a/mercurial/templatefilters.py
> +++ b/mercurial/templatefilters.py
> @@ -306,6 +306,12 @@
>      else:
>          return dir
>  
> +def stripquote(text):
> +    """:stripquote: Any text. Returns the text, with leading and trailing
> +    quotes (") removed. Eg.: "J. Doe" becomes: J. Doe
> +    """
> +    return text.strip('"')
> +
>  def tabindent(text):
>      """:tabindent: Any text. Returns the text, with every line except the
>      first starting with a tab character.
> @@ -362,6 +368,7 @@
>      "stringify": stringify,
>      "strip": strip,
>      "stripdir": stripdir,
> +    "stripquote": stripquote,
>      "tabindent": tabindent,
>      "urlescape": urlescape,
>      "user": userfilter,
> diff --git a/tests/test-command-template.t b/tests/test-command-template.t
> --- a/tests/test-command-template.t
> +++ b/tests/test-command-template.t
> @@ -1202,6 +1202,39 @@
>    1: 2:97054abb4ab8
>    0: 1:b608e9d1a3f0
>  
> +Author with quotes in 'person':
> +
> +  $ hg update null
> +  0 files updated, 0 files merged, 2 files removed, 0 files unresolved
> +  $ echo fifth > fifth
> +  $ hg add fifth
> +  $ hg commit -m 'fifth' -u '"Q. Uoted" <q at uoted>'
> +  created new head
> +
> +  $ hg log --template '{author|person}\n'
> +  "Q. Uoted"
> +  test
> +  User Name
> +  person
> +  person
> +  person
> +  person
> +  other
> +  A. N. Other
> +  User Name
> +
> +  $ hg log --template '{author|person|stripquote}\n'
> +  Q. Uoted
> +  test
> +  User Name
> +  person
> +  person
> +  person
> +  person
> +  other
> +  A. N. Other
> +  User Name
> +
>  Formatnode filter works:
>  
>    $ hg -q log -r 0 --template '{node|formatnode}\n'
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel


-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list