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

Yann E. MORIN yann.morin.1998 at free.fr
Wed Feb 29 23:39:27 UTC 2012


# 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

[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'


More information about the Mercurial-devel mailing list