diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2019-08-24 19:00:12 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2019-08-25 19:42:06 +0200 |
commit | f5cfdabb740ddbebf379fa93f9cb80b00ace2c48 (patch) | |
tree | 770e42a42f620cf3837367328f47acc52c6ef454 /wee_slack.py | |
parent | c35a6d81368f81e9d2ac991c9f4ae0d22405918b (diff) | |
download | wee-slack-f5cfdabb740ddbebf379fa93f9cb80b00ace2c48.tar.gz |
Refactor linkify_text to use re.sub
Instead of splitting the message at spaces and checking and replacing
each word, use re.sub for the whole message.
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 47 |
1 files changed, 20 insertions, 27 deletions
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): |