diff options
-rw-r--r-- | _pytest/test_linkifytext.py | 5 | ||||
-rw-r--r-- | wee_slack.py | 47 |
2 files changed, 25 insertions, 27 deletions
diff --git a/_pytest/test_linkifytext.py b/_pytest/test_linkifytext.py index 7c67fa6..4f9cd3b 100644 --- a/_pytest/test_linkifytext.py +++ b/_pytest/test_linkifytext.py @@ -86,3 +86,8 @@ def test_linkifytext_not_dm(team, channel_dm): text = linkify_text('#{}: my test message'.format(channel_dm.name), team) assert text == '#{}: my test message'.format(channel_dm.name) + +def test_linkifytext_not_mpdm(team, channel_mpdm): + text = linkify_text('#{}: my test message'.format(channel_mpdm.name), team) + + assert text == '#{}: my test message'.format(channel_mpdm.name) diff --git a/wee_slack.py b/wee_slack.py index c105a98..9e195e2 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -3187,7 +3187,7 @@ def linkify_text(message, team): usernames = team.get_username_map() channels = team.get_channel_map() usergroups = team.generate_usergroup_map() - message = (message + message_escaped = (message # Replace IRC formatting chars with Slack formatting chars. .replace('\x02', '*') .replace('\x1D', '_') @@ -3197,32 +3197,25 @@ def linkify_text(message, team): # See https://api.slack.com/docs/message-formatting for details. .replace('&', '&') .replace('<', '<') - .replace('>', '>') - .split(' ')) - for item in enumerate(message): - targets = re.match(r'^\s*([@#])([\w\(\)\'.-]+)(\W*)', item[1], re.UNICODE) - if targets and targets.groups()[0] == '@': - named = targets.groups() - if named[1] in ["channel", "everyone", "group", "here"]: - message[item[0]] = "<!{}>{}".format(named[1], named[2]) - elif named[0] + named[1] in usergroups.keys(): - message[item[0]] = "<!subteam^{}|@{}>{}".format(usergroups[named[0] + named[1]], named[1], named[2]) - else: - try: - if usernames[named[1]]: - message[item[0]] = "<@{}>{}".format(usernames[named[1]], named[2]) - except: - message[item[0]] = "@{}{}".format(named[1], named[2]) - if targets and targets.groups()[0] == '#': - named = targets.groups() - try: - if channels[named[0] + named[1]]: - message[item[0]] = "<#{}|{}>{}".format(channels[named[0] + named[1]], named[1], named[2]) - except: - message[item[0]] = "#{}{}".format(named[1], named[2]) - - # dbg(message) - return " ".join(message) + .replace('>', '>')) + + def linkify_word(match): + word = match.group(0) + prefix, name = match.groups() + if prefix == "@": + if name in ["channel", "everyone", "group", "here"]: + return "<!{}>".format(name) + elif name in usernames: + return "<@{}>".format(usernames[name]) + elif word in usergroups.keys(): + return "<!subteam^{}|{}>".format(usergroups[word], word) + elif prefix == "#": + if word in channels: + return "<#{}|{}>".format(channels[word], name) + return word + + linkify_regex = r'(?:^|(?<=\s))([@#])([\w\(\)\'.-]+)' + return re.sub(linkify_regex, linkify_word, message_escaped, re.UNICODE) def unfurl_refs(text, ignore_alt_text=None, auto_link_display=None): |