# HG changeset patch
# User "Yann E. MORIN" <yann.morin.1998@free.fr>
# Date 2012-03-06 22:23:30
# Node ID eb39bbda167bc599ceb61c52d30674fa0be3870b
# Parent  a6941d7033fa4f8b56ad92e4d75c31778f2d2ffe

templates/filters: strip quotes from {author|person}

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. Also, DQUOTEs in a
quoted string will be escaped using "\" [2][3].

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...

Moreover, as an example, the Mercurial repository has a bunch of
incoherent uses of DQUOTES in author names. As per Matt's digging:

  $ hg log --template "{author|person}\n" | grep '"' | sort | uniq
  "Andrei Vermel
  "Aurelien Jacobs
  "Daniel Santa Cruz
  "Hidetaka Iwai
  "Hiroshi Funai"
  "Mathieu Clabaut
  "Paul Moore
  "Peter Arrenbrecht"
  "Rafael Villar Burke
  "Shun-ichi GOTO"
  "Wallace, Eric S"
  "Yann E. MORIN"
  Josef "Jeff" Sipek
  Radoslaw "AstralStorm" Szkodzinski

Fix the 'person' filter to remove leading and trailing DQUOTES,
and unescape remaining DQUOTES.

Given this author: "J. \"random\" DOE" <john@doe.net>
  before:   {author|person} : "J. \"random\" DOE"
  after:    {author|person} : J. "random" DOE

For the Mercurial repository, that leaves us with two authors with
DQUOTES, in acceptable positions:

  $ hg log --template "{author|person}\n" | grep '"' | sort | uniq
  Josef "Jeff" Sipek
  Radoslaw "AstralStorm" Szkodzinski

[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
[3] https://tools.ietf.org/html/rfc5322#section-3.2.1

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -242,12 +242,14 @@ def permissions(flags):
     return "-rw-r--r--"
 
 def person(author):
-    """:person: Any text. Returns the text before an email address."""
+    """:person: Any text. Returns the name before an email address,
+    interpreting it as per RFC 5322.
+    """
     if not '@' in author:
         return author
     f = author.find('<')
     if f != -1:
-        return author[:f].rstrip()
+        return author[:f].strip(' "').replace('\\"', '"')
     f = author.find('@')
     return author[:f].replace('.', ' ')