D2960: stringutil: move person function from templatefilters

sheehan (Connor Sheehan) phabricator at mercurial-scm.org
Tue Mar 27 16:04:51 UTC 2018


sheehan created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Move the person function from template filters to the stringutil
  module, so it can be reused in the mailmap template function.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2960

AFFECTED FILES
  mercurial/templatefilters.py
  mercurial/utils/stringutil.py

CHANGE DETAILS

diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -308,3 +308,29 @@
     False
     '''
     return bool(_correctauthorformat.match(author))
+
+def person(author):
+    """Any text. Returns the name before an email address,
+    interpreting it as per RFC 5322.
+    >>> person(b'foo at bar')
+    'foo'
+    >>> person(b'Foo Bar <foo at bar>')
+    'Foo Bar'
+    >>> person(b'"Foo Bar" <foo at bar>')
+    'Foo Bar'
+    >>> person(b'"Foo \"buz\" Bar" <foo at bar>')
+    'Foo "buz" Bar'
+    >>> # The following are invalid, but do exist in real-life
+    ...
+    >>> person(b'Foo "buz" Bar <foo at bar>')
+    'Foo "buz" Bar'
+    >>> person(b'"Foo Bar <foo at 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('.', ' ')
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -290,31 +290,7 @@
 
 @templatefilter('person')
 def person(author):
-    """Any text. Returns the name before an email address,
-    interpreting it as per RFC 5322.
-
-    >>> person(b'foo at bar')
-    'foo'
-    >>> person(b'Foo Bar <foo at bar>')
-    'Foo Bar'
-    >>> person(b'"Foo Bar" <foo at bar>')
-    'Foo Bar'
-    >>> person(b'"Foo \"buz\" Bar" <foo at bar>')
-    'Foo "buz" Bar'
-    >>> # The following are invalid, but do exist in real-life
-    ...
-    >>> person(b'Foo "buz" Bar <foo at bar>')
-    'Foo "buz" Bar'
-    >>> person(b'"Foo Bar <foo at 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):



To: sheehan, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list