changeset 4403:d9948f5ac7af

mention: improve pattern for matching multiple mentions It would (silently as always) fail to recognize some @mentions - especially when only separated by a single whitespace char.
author Mads Kiilerich <madski@unity3d.com>
date Tue, 10 Dec 2013 19:30:37 +0100
parents 10190008738e
children 231442b0b6aa
files kallithea/lib/utils2.py kallithea/public/js/base.js kallithea/tests/other/test_libs.py
diffstat 3 files changed, 15 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/utils2.py	Fri Aug 01 20:28:42 2014 +0200
+++ b/kallithea/lib/utils2.py	Tue Dec 10 19:30:37 2013 +0100
@@ -553,17 +553,22 @@
                 return
         return datetime.datetime.fromtimestamp(tm)
 
-MENTIONS_REGEX = r'(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)(?:\s{1})'
-
+# Must match regexp in kallithea/public/js/base.js MentionsAutoComplete()
+# Eat char before @ - it must not look like we are in an email addresses.
+# Matching is gready so we don't have to look beyond the end.
+MENTIONS_REGEX = re.compile(r'(?:^|[^a-zA-Z0-9])@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])')
 
 def extract_mentioned_users(s):
-    """
+    r"""
     Returns unique usernames from given string s that have @mention
 
     :param s: string to get mentions
+
+    >>> extract_mentioned_users('@1-2.a_X,@1234 not@not @ddd@not @n @ee @ff @gg, @gg;@hh @n\n@zz,')
+    ['1-2.a_X', '1234', 'ddd', 'ee', 'ff', 'gg', 'hh', 'zz']
     """
     usrs = set()
-    for username in re.findall(MENTIONS_REGEX, s):
+    for username in MENTIONS_REGEX.findall(s):
         usrs.add(username)
 
     return sorted(list(usrs), key=lambda k: k.lower())
--- a/kallithea/public/js/base.js	Fri Aug 01 20:28:42 2014 +0200
+++ b/kallithea/public/js/base.js	Tue Dec 10 19:30:37 2013 +0100
@@ -1449,10 +1449,12 @@
 
     ownerAC.get_mention = function(msg, max_pos) {
         var org = msg;
-        var re = new RegExp('(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)$')
+        // Must match utils2.py MENTIONS_REGEX.
+        // Only matching on string up to cursor, so it must end with $
+        var re = new RegExp('(?:^|[^a-zA-Z0-9])@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])$')
         var chunks  = [];
 
-        // cut first chunk until curret pos
+        // cut first chunk until current pos
         var to_max = msg.substr(0, max_pos);
         var at_pos = Math.max(0,to_max.lastIndexOf('@')-1);
         var msg2 = to_max.substr(at_pos);
--- a/kallithea/tests/other/test_libs.py	Fri Aug 01 20:28:42 2014 +0200
+++ b/kallithea/tests/other/test_libs.py	Tue Dec 10 19:30:37 2013 +0100
@@ -113,9 +113,8 @@
         )
 
         s = sorted([
-        'first', 'marcink', 'lukaszb', 'one_more22', 'MARCIN', 'maRCiN', 'john',
-        'marian.user', 'marco-polo', 'marco_polo'
-        ], key=lambda k: k.lower())
+            '2one_more22', 'first', 'marcink', 'lukaszb', 'one', 'one_more22', 'MARCIN', 'maRCiN', 'john',
+            'marian.user', 'marco-polo', 'marco_polo'], key=lambda k: k.lower())
         self.assertEqual(s, extract_mentioned_users(sample))
 
     @parameterized.expand([