diff --git a/rhodecode/lib/markup_renderer.py b/rhodecode/lib/markup_renderer.py --- a/rhodecode/lib/markup_renderer.py +++ b/rhodecode/lib/markup_renderer.py @@ -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 @@ class MarkupRenderer(object): @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) diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -392,14 +392,18 @@ def get_changeset_safe(repo, rev): 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()) + diff --git a/rhodecode/tests/test_libs.py b/rhodecode/tests/test_libs.py --- a/rhodecode/tests/test_libs.py +++ b/rhodecode/tests/test_libs.py @@ -103,9 +103,16 @@ class TestLibs(unittest.TestCase): 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))