diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2019-08-24 18:55:04 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2019-08-25 19:42:06 +0200 |
commit | 6042a85c9b2e9bf266d3225a2a878583ea5b944d (patch) | |
tree | 86771df839e74f073ce807c803f03c0c66b81efc | |
parent | ede483b1c5fc5ed2b04b668b57980e7a9bbab1ad (diff) | |
download | wee-slack-6042a85c9b2e9bf266d3225a2a878583ea5b944d.tar.gz |
Refactor unfurl_refs to use re.sub
Instead of finding each match and calling replace on the text with that
match, use re.sub to replace all the matches.
-rw-r--r-- | wee_slack.py | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/wee_slack.py b/wee_slack.py index 984c3b4..366bde6 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -3242,44 +3242,39 @@ def unfurl_refs(text, ignore_alt_text=None, auto_link_display=None): 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, auto_link_display)) - return text - - -def unfurl_ref(ref, ignore_alt_text, auto_link_display): - id = ref.split('|')[0] - display_text = ref - if ref.find('|') > -1: - if ignore_alt_text: - display_text = resolve_ref(id) - else: - if id.startswith("#C"): - display_text = "#{}".format(ref.split('|')[1]) - elif id.startswith("@U"): - display_text = ref.split('|')[1] - elif id.startswith("!subteam"): - if ref.split('|')[1].startswith('@'): - handle = ref.split('|')[1][1:] - else: - handle = ref.split('|')[1] - display_text = '@{}'.format(handle) + def unfurl_ref(match): + ref = match.group(1) + id = ref.split('|')[0] + display_text = ref + if ref.find('|') > -1: + if ignore_alt_text: + display_text = resolve_ref(id) else: - 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 auto_link_display == "text": - display_text = desc - elif url_matches_desc and auto_link_display == "url": - display_text = url + if id.startswith("#C"): + display_text = "#{}".format(ref.split('|')[1]) + elif id.startswith("@U"): + display_text = ref.split('|')[1] + elif id.startswith("!subteam"): + if ref.split('|')[1].startswith('@'): + handle = ref.split('|')[1][1:] + else: + handle = ref.split('|')[1] + display_text = '@{}'.format(handle) else: - display_text = "{} ({})".format(url, desc) - else: - display_text = resolve_ref(ref) - return display_text + 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 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 + + return re.sub(r"<([@#!]?[^>]*)>", unfurl_ref, text) def unhtmlescape(text): |