[PATCH] templates/filters: extracting the user portion of an email address

Matteo Capobianco m.capobianco at gmail.com
Wed Mar 28 09:14:10 CDT 2012


# HG changeset patch
# User Matteo Capobianco <m.capobianco at gmail.com>
# Date 1332943580 -7200
# Node ID 8fe5ec2993cb73aa4fa4aa3638a4824440598f61
# Parent  ba42eb722bb39e702c54b7a6fa7a703d4ba2cec3
templates/filters: extracting the user portion of an email address

Currently, the 'user' filter is using util.shortuser(text) (which clearly
doesn't extract only the user portion of an email address, even though the
help text says it does).

The new 'emailuser' filter uses the new util.emailuser(text) function which,
instead, does exactly that.

The help text on the 'user' filter has been modified accordingly.

diff -r ba42eb722bb3 -r 8fe5ec2993cb mercurial/templatefilters.py
--- a/mercurial/templatefilters.py	Tue Mar 27 16:17:46 2012 -0500
+++ b/mercurial/templatefilters.py	Wed Mar 28 16:06:20 2012 +0200
@@ -336,9 +336,14 @@
     return urllib.quote(text)
 
 def userfilter(text):
-    """:user: Any text. Returns the user portion of an email address."""
+    """:user: Any text. Returns a short representation of a user name or email
+    address."""
     return util.shortuser(text)
 
+def emailuser(text):
+    """:emailuser: Any text. Returns the user portion of an email address."""
+    return util.emailuser(text)
+
 def xmlescape(text):
     text = (text
             .replace('&', '&')
@@ -382,6 +387,7 @@
     "tabindent": tabindent,
     "urlescape": urlescape,
     "user": userfilter,
+    "emailuser": emailuser,
     "xmlescape": xmlescape,
 }
 
diff -r ba42eb722bb3 -r 8fe5ec2993cb mercurial/util.py
--- a/mercurial/util.py	Tue Mar 27 16:17:46 2012 -0500
+++ b/mercurial/util.py	Wed Mar 28 16:06:20 2012 +0200
@@ -1125,6 +1125,16 @@
         user = user[:f]
     return user
 
+def emailuser(user):
+    """Return the user portion of an email address."""
+    f = user.find('@')
+    if f >= 0:
+        user = user[:f]
+    f = user.find('<')
+    if f >= 0:
+        user = user[f + 1:]
+    return user
+
 def email(author):
     '''get email of author.'''
     r = author.find('>')


More information about the Mercurial-devel mailing list