Patchwork D2960: stringutil: move person function from templatefilters

login
register
mail settings
Submitter phabricator
Date March 27, 2018, 4:04 p.m.
Message ID <differential-rev-PHID-DREV-moyzmzze3hdux47illwh-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29910/
State Superseded
Headers show

Comments

phabricator - March 27, 2018, 4:04 p.m.
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




To: sheehan, #hg-reviewers
Cc: mercurial-devel
phabricator - March 28, 2018, 5:10 a.m.
av6 requested changes to this revision.
av6 added inline comments.
This revision now requires changes to proceed.

INLINE COMMENTS

> templatefilters.py:293-294
>  def person(author):
> -    """Any text. Returns the name before an email address,
> -    interpreting it as per RFC 5322.
> -

Don't remove this docstring (at least not completely), its first paragraph goes to `hg help templates`.

REPOSITORY
  rHG Mercurial

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

To: sheehan, #hg-reviewers, av6
Cc: av6, mercurial-devel
phabricator - March 30, 2018, 12:36 p.m.
yuja added a comment.


  Queued, thanks. I've adjusted the location of codes so the author-related
  functions are grouped.

REPOSITORY
  rHG Mercurial

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

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

Patch

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@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('.', ' ')
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@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):