refactor: Get config variables in unfurl_refs
Instead of each of the callers of unfurl_refs having to get the config
values, get them in unfurl_refs if they are not set by the caller.
@@ -2498,9 +2498,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", " "))
@@ -2557,7 +2557,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
@@ -2567,14 +2567,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:
@@ -2589,9 +2596,9 @@ def unfurl_ref(ref, ignore_alt_text=False):
url, desc = ref.split('|', 1)
match_url = r"^\w+:(//)?{}$".format(re.escape(desc))
url_matches_desc = re.match(match_url, url)
- if url_matches_desc and config.unfurl_auto_link_display == "text":
+ if url_matches_desc and auto_link_display == "text":
display_text = desc
- elif url_matches_desc and config.unfurl_auto_link_display == "url":
+ elif url_matches_desc and auto_link_display == "url":
display_text = url
else:
display_text = "{} ({})".format(url, desc)
