aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2019-08-24 19:00:12 +0200
committerTrygve Aaberge <trygveaa@gmail.com>2019-08-25 19:42:06 +0200
commitf5cfdabb740ddbebf379fa93f9cb80b00ace2c48 (patch)
tree770e42a42f620cf3837367328f47acc52c6ef454
parentc35a6d81368f81e9d2ac991c9f4ae0d22405918b (diff)
downloadwee-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.
-rw-r--r--_pytest/test_linkifytext.py5
-rw-r--r--wee_slack.py47
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('&', '&amp;')
.replace('<', '&lt;')
- .replace('>', '&gt;')
- .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('>', '&gt;'))
+
+ 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):