comparison mercurial/utils/stringutil.py @ 37158: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 2a2ce93e12f4
comparison
equal deleted inserted replaced
37157:f8e1f48de118 37158:fb7140f1d09d
129 r = author.find('>') 129 r = author.find('>')
130 if r == -1: 130 if r == -1:
131 r = None 131 r = None
132 return author[author.find('<') + 1:r] 132 return author[author.find('<') + 1:r]
133 133
134 def person(author):
135 """Returns the name before an email address,
136 interpreting it as per RFC 5322
137
138 >>> person(b'foo@bar')
139 'foo'
140 >>> person(b'Foo Bar <foo@bar>')
141 'Foo Bar'
142 >>> person(b'"Foo Bar" <foo@bar>')
143 'Foo Bar'
144 >>> person(b'"Foo \"buz\" Bar" <foo@bar>')
145 'Foo "buz" Bar'
146 >>> # The following are invalid, but do exist in real-life
147 ...
148 >>> person(b'Foo "buz" Bar <foo@bar>')
149 'Foo "buz" Bar'
150 >>> person(b'"Foo Bar <foo@bar>')
151 'Foo Bar'
152 """
153 if '@' not in author:
154 return author
155 f = author.find('<')
156 if f != -1:
157 return author[:f].strip(' "').replace('\\"', '"')
158 f = author.find('@')
159 return author[:f].replace('.', ' ')
160
134 _correctauthorformat = remod.compile(br'^[^<]+\s\<[^<>]+@[^<>]+\>$') 161 _correctauthorformat = remod.compile(br'^[^<]+\s\<[^<>]+@[^<>]+\>$')
135 162
136 def isauthorwellformed(author): 163 def isauthorwellformed(author):
137 '''Return True if the author field is well formed 164 '''Return True if the author field is well formed
138 (ie "Contributor Name <contrib@email.dom>") 165 (ie "Contributor Name <contrib@email.dom>")