Mercurial > public > mercurial-scm > hg
comparison mercurial/templatefilters.py @ 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 | f0b6fbea00cf |
children | 54355c243042 |
comparison
equal
deleted
inserted
replaced
37154:f8e1f48de118 | 37155:fb7140f1d09d |
---|---|
290 | 290 |
291 @templatefilter('person') | 291 @templatefilter('person') |
292 def person(author): | 292 def person(author): |
293 """Any text. Returns the name before an email address, | 293 """Any text. Returns the name before an email address, |
294 interpreting it as per RFC 5322. | 294 interpreting it as per RFC 5322. |
295 | 295 """ |
296 >>> person(b'foo@bar') | 296 return stringutil.person(author) |
297 'foo' | |
298 >>> person(b'Foo Bar <foo@bar>') | |
299 'Foo Bar' | |
300 >>> person(b'"Foo Bar" <foo@bar>') | |
301 'Foo Bar' | |
302 >>> person(b'"Foo \"buz\" Bar" <foo@bar>') | |
303 'Foo "buz" Bar' | |
304 >>> # The following are invalid, but do exist in real-life | |
305 ... | |
306 >>> person(b'Foo "buz" Bar <foo@bar>') | |
307 'Foo "buz" Bar' | |
308 >>> person(b'"Foo Bar <foo@bar>') | |
309 'Foo Bar' | |
310 """ | |
311 if '@' not in author: | |
312 return author | |
313 f = author.find('<') | |
314 if f != -1: | |
315 return author[:f].strip(' "').replace('\\"', '"') | |
316 f = author.find('@') | |
317 return author[:f].replace('.', ' ') | |
318 | 297 |
319 @templatefilter('revescape') | 298 @templatefilter('revescape') |
320 def revescape(text): | 299 def revescape(text): |
321 """Any text. Escapes all "special" characters, except @. | 300 """Any text. Escapes all "special" characters, except @. |
322 Forward slashes are escaped twice to prevent web servers from prematurely | 301 Forward slashes are escaped twice to prevent web servers from prematurely |