Changeset - d9948f5ac7af
[Not reviewed]
default
0 3 0
Mads Kiilerich - 12 years ago 2013-12-10 19:30:37
madski@unity3d.com
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.
3 files changed with 15 insertions and 9 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/utils2.py
Show inline comments
 
@@ -553,17 +553,22 @@ def time_to_datetime(tm):
 
                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())
kallithea/public/js/base.js
Show inline comments
 
@@ -1449,10 +1449,12 @@ var MentionsAutoComplete = function (div
 

	
 
    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);
kallithea/tests/other/test_libs.py
Show inline comments
 
@@ -113,9 +113,8 @@ class TestLibs(BaseTestCase):
 
        )
 

	
 
        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([
0 comments (0 inline, 0 general)