Commits

Marcin Kuzminski  committed ea5ff84

#426 fixed mention extracting regex

  • Participants
  • Parent commits d7a4c7e

Comments (0)

Files changed (3)

File rhodecode/lib/markup_renderer.py

 import re
 import logging
 
-from rhodecode.lib.utils2 import safe_unicode
+from rhodecode.lib.utils2 import safe_unicode, MENTIONS_REGEX
 
 log = logging.getLogger(__name__)
 
 
     @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)

File rhodecode/lib/utils2.py

     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())
+

File rhodecode/tests/test_libs.py

 
     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))