# HG changeset patch # User Connor Sheehan # Date 2018-03-27 15:01:13 # Node ID fb7140f1d09d7464fdaebc86d5f8ee31f80353c4 # Parent f8e1f48de118e71c4be26d7f9e8953f65979b60c 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 diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -292,29 +292,8 @@ def permissions(flags): 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' - >>> person(b'"Foo Bar" ') - 'Foo Bar' - >>> person(b'"Foo \"buz\" Bar" ') - 'Foo "buz" Bar' - >>> # The following are invalid, but do exist in real-life - ... - >>> person(b'Foo "buz" Bar ') - 'Foo "buz" Bar' - >>> person(b'"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): diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py --- a/mercurial/utils/stringutil.py +++ b/mercurial/utils/stringutil.py @@ -131,6 +131,33 @@ def email(author): 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' + >>> person(b'"Foo Bar" ') + 'Foo Bar' + >>> person(b'"Foo \"buz\" Bar" ') + 'Foo "buz" Bar' + >>> # The following are invalid, but do exist in real-life + ... + >>> person(b'Foo "buz" Bar ') + 'Foo "buz" Bar' + >>> person(b'"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):