changeset 2201:ea5ff843b200 beta

#426 fixed mention extracting regex
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 18 Apr 2012 02:07:22 +0200
parents d7a4c7e3528e
children 48d9a62c9b75
files rhodecode/lib/markup_renderer.py rhodecode/lib/utils2.py rhodecode/tests/test_libs.py
diffstat 3 files changed, 23 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/markup_renderer.py	Tue Apr 17 23:50:32 2012 +0200
+++ b/rhodecode/lib/markup_renderer.py	Wed Apr 18 02:07:22 2012 +0200
@@ -27,7 +27,7 @@
 import re
 import logging
 
-from rhodecode.lib.utils2 import safe_unicode
+from rhodecode.lib.utils2 import safe_unicode, MENTIONS_REGEX
 
 log = logging.getLogger(__name__)
 
@@ -128,10 +128,10 @@
 
     @classmethod
     def rst_with_mentions(cls, source):
-        mention_pat = re.compile(r'(?:^@|\s@)(\w+)')
+        mention_pat = re.compile(MENTIONS_REGEX)
 
         def wrapp(match_obj):
             uname = match_obj.groups()[0]
-            return ' **@%(uname)s** ' % {'uname':uname}
+            return ' **@%(uname)s** ' % {'uname': uname}
         mention_hl = mention_pat.sub(wrapp, source).strip()
         return cls.rst(mention_hl)
--- a/rhodecode/lib/utils2.py	Tue Apr 17 23:50:32 2012 +0200
+++ b/rhodecode/lib/utils2.py	Wed Apr 18 02:07:22 2012 +0200
@@ -392,14 +392,18 @@
     return cs
 
 
+MENTIONS_REGEX = r'(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)(?:\s{1})'
+
+
 def extract_mentioned_users(s):
     """
     Returns unique usernames from given string s that have @mention
 
     :param s: string to get mentions
     """
-    usrs = {}
-    for username in re.findall(r'(?:^@|\s@)(\w+)', s):
-        usrs[username] = username
+    usrs = set()
+    for username in re.findall(MENTIONS_REGEX, s):
+        usrs.add(username)
 
-    return sorted(usrs.keys())
+    return sorted(list(usrs), key=lambda k: k.lower())
+
--- a/rhodecode/tests/test_libs.py	Tue Apr 17 23:50:32 2012 +0200
+++ b/rhodecode/tests/test_libs.py	Wed Apr 18 02:07:22 2012 +0200
@@ -103,9 +103,16 @@
 
     def test_mention_extractor(self):
         from rhodecode.lib.utils2 import extract_mentioned_users
-        sample = ("@first hi there @marcink here's my email marcin@email.com "
-                  "@lukaszb check it pls @ ttwelve @D[] @one@two@three "
-                  "@MARCIN    @maRCiN @2one_more22")
-        s = ['2one_more22', 'D', 'MARCIN', 'first', 'lukaszb',
-             'maRCiN', 'marcink', 'one']
+        sample = (
+            "@first hi there @marcink here's my email marcin@email.com "
+            "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three "
+            "@MARCIN    @maRCiN @2one_more22 @john please see this http://org.pl "
+            "@marian.user just do it @marco-polo and next extract @marco_polo "
+            "user.dot  hej ! not-needed maril@domain.org"
+        )
+
+        s = sorted([
+        'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john',
+        'marian.user', 'marco-polo', 'marco_polo'
+        ], key=lambda k: k.lower())
         self.assertEqual(s, extract_mentioned_users(sample))