changeset 37155:fb7140f1d09d

stringutil: move person function from templatefilters Move the person function from template filters to the stringutil module, so it can be reused in the mailmap template function. Differential Revision: https://phab.mercurial-scm.org/D2960
author Connor Sheehan <sheehan@mozilla.com>
date Tue, 27 Mar 2018 11:01:13 -0400
parents f8e1f48de118
children 888507ec655e
files mercurial/templatefilters.py mercurial/utils/stringutil.py
diffstat 2 files changed, 28 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Thu Mar 22 09:48:22 2018 -0400
+++ b/mercurial/templatefilters.py	Tue Mar 27 11:01:13 2018 -0400
@@ -292,29 +292,8 @@
 def person(author):
     """Any text. Returns the name before an email address,
     interpreting it as per RFC 5322.
-
-    >>> person(b'foo@bar')
-    'foo'
-    >>> person(b'Foo Bar <foo@bar>')
-    'Foo Bar'
-    >>> person(b'"Foo Bar" <foo@bar>')
-    'Foo Bar'
-    >>> person(b'"Foo \"buz\" Bar" <foo@bar>')
-    'Foo "buz" Bar'
-    >>> # The following are invalid, but do exist in real-life
-    ...
-    >>> person(b'Foo "buz" Bar <foo@bar>')
-    'Foo "buz" Bar'
-    >>> person(b'"Foo Bar <foo@bar>')
-    'Foo Bar'
     """
-    if '@' not in author:
-        return author
-    f = author.find('<')
-    if f != -1:
-        return author[:f].strip(' "').replace('\\"', '"')
-    f = author.find('@')
-    return author[:f].replace('.', ' ')
+    return stringutil.person(author)
 
 @templatefilter('revescape')
 def revescape(text):
--- a/mercurial/utils/stringutil.py	Thu Mar 22 09:48:22 2018 -0400
+++ b/mercurial/utils/stringutil.py	Tue Mar 27 11:01:13 2018 -0400
@@ -131,6 +131,33 @@
         r = None
     return author[author.find('<') + 1:r]
 
+def person(author):
+    """Returns the name before an email address,
+    interpreting it as per RFC 5322
+
+    >>> person(b'foo@bar')
+    'foo'
+    >>> person(b'Foo Bar <foo@bar>')
+    'Foo Bar'
+    >>> person(b'"Foo Bar" <foo@bar>')
+    'Foo Bar'
+    >>> person(b'"Foo \"buz\" Bar" <foo@bar>')
+    'Foo "buz" Bar'
+    >>> # The following are invalid, but do exist in real-life
+    ...
+    >>> person(b'Foo "buz" Bar <foo@bar>')
+    'Foo "buz" Bar'
+    >>> person(b'"Foo Bar <foo@bar>')
+    'Foo Bar'
+    """
+    if '@' not in author:
+        return author
+    f = author.find('<')
+    if f != -1:
+        return author[:f].strip(' "').replace('\\"', '"')
+    f = author.find('@')
+    return author[:f].replace('.', ' ')
+
 _correctauthorformat = remod.compile(br'^[^<]+\s\<[^<>]+@[^<>]+\>$')
 
 def isauthorwellformed(author):