diff options
-rw-r--r-- | _pytest/test_unfurl.py | 25 | ||||
-rw-r--r-- | wee_slack.py | 36 |
2 files changed, 54 insertions, 7 deletions
diff --git a/_pytest/test_unfurl.py b/_pytest/test_unfurl.py index eebe446..40674b4 100644 --- a/_pytest/test_unfurl.py +++ b/_pytest/test_unfurl.py @@ -31,6 +31,26 @@ slack = wee_slack 'output': "url: https://example.com (example with spaces) suffix", }, { + 'input': "url: <https://example.com|example.com> suffix", + 'output': "url: example.com suffix", + 'auto_link_display': 'text', + }, + { + 'input': "url: <https://example.com|different text> suffix", + 'output': "url: https://example.com (different text) suffix", + 'auto_link_display': 'text', + }, + { + 'input': "url: <https://example.com|different text> suffix", + 'output': "url: https://example.com (different text) suffix", + 'auto_link_display': 'url', + }, + { + 'input': "url: <https://example.com|example.com> suffix", + 'output': "url: https://example.com suffix", + 'auto_link_display': 'url', + }, + { 'input': "<@U407ABLLW|@othernick> multiple unfurl <https://example.com|example with spaces>", 'output': "@othernick multiple unfurl https://example.com (example with spaces)", }, @@ -47,5 +67,8 @@ def test_unfurl_refs(case, realish_eventrouter): slack.EVENTROUTER = realish_eventrouter result = slack.unfurl_refs( - case['input'], ignore_alt_text=case.get('ignore_alt_text', False)) + case['input'], + ignore_alt_text=case.get('ignore_alt_text', False), + auto_link_display=case.get('auto_link_display', 'both'), + ) assert result == case['output'] diff --git a/wee_slack.py b/wee_slack.py index 79540b3..49ec61e 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -2565,9 +2565,9 @@ def render(message_json, team, channel, force=False): else: text = "" - text = unfurl_refs(text, ignore_alt_text=config.unfurl_ignore_alt_text) + text = unfurl_refs(text) - text += unfurl_refs(unwrap_attachments(message_json, text), ignore_alt_text=config.unfurl_ignore_alt_text) + text += unfurl_refs(unwrap_attachments(message_json, text)) text = text.lstrip() text = unhtmlescape(text.replace("\t", " ")) @@ -2624,7 +2624,7 @@ def linkify_text(message, team, channel): return " ".join(message) -def unfurl_refs(text, ignore_alt_text=False): +def unfurl_refs(text, ignore_alt_text=None, auto_link_display=None): """ input : <@U096Q7CQM|someuser> has joined the channel ouput : someuser has joined the channel @@ -2634,14 +2634,21 @@ def unfurl_refs(text, ignore_alt_text=False): # - <#C2147483705|#otherchannel> # - <@U2147483697|@othernick> # Test patterns lives in ./_pytest/test_unfurl.py + + if ignore_alt_text is None: + ignore_alt_text = config.unfurl_ignore_alt_text + if auto_link_display is None: + auto_link_display = config.unfurl_auto_link_display + matches = re.findall(r"(<[@#]?(?:[^>]*)>)", text) for m in matches: # Replace them with human readable strings - text = text.replace(m, unfurl_ref(m[1:-1], ignore_alt_text)) + text = text.replace( + m, unfurl_ref(m[1:-1], ignore_alt_text, auto_link_display)) return text -def unfurl_ref(ref, ignore_alt_text=False): +def unfurl_ref(ref, ignore_alt_text, auto_link_display): id = ref.split('|')[0] display_text = ref if ref.find('|') > -1: @@ -2654,7 +2661,14 @@ def unfurl_ref(ref, ignore_alt_text=False): display_text = ref.split('|')[1] else: url, desc = ref.split('|', 1) - display_text = "{} ({})".format(url, desc) + match_url = r"^\w+:(//)?{}$".format(re.escape(desc)) + url_matches_desc = re.match(match_url, url) + if url_matches_desc and auto_link_display == "text": + display_text = desc + elif url_matches_desc and auto_link_display == "url": + display_text = url + else: + display_text = "{} ({})".format(url, desc) else: display_text = resolve_ref(ref) return display_text @@ -3533,6 +3547,15 @@ class PluginConfig(object): desc='When displaying ("unfurling") links to channels/users/etc,' ' ignore the "alt text" present in the message and instead use the' ' canonical name of the thing being linked to.'), + 'unfurl_auto_link_display': Setting( + default='both', + desc='When displaying ("unfurling") links to channels/users/etc,' + ' determine what is displayed when the text matches the url' + ' without the protocol. This happens when Slack automatically' + ' creates links, e.g. from words separated by dots or email' + ' addresses. Set it to "text" to only display the text written by' + ' the user, "url" to only display the url or "both" (the default)' + ' to display both.'), 'unhide_buffers_with_activity': Setting( default='false', desc='When activity occurs on a buffer, unhide it even if it was' @@ -3599,6 +3622,7 @@ class PluginConfig(object): get_render_italic_as = get_string get_slack_timeout = get_int get_thread_suffix_color = get_string + get_unfurl_auto_link_display = get_string def get_distracting_channels(self, key): return [x.strip() for x in w.config_get_plugin(key).split(',')] |