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.
@@ -3242,15 +3242,8 @@ 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):
+ def unfurl_ref(match):
+ ref = match.group(1)
id = ref.split('|')[0]
display_text = ref
if ref.find('|') > -1:
@@ -3281,6 +3274,8 @@ def unfurl_ref(ref, ignore_alt_text, auto_link_display):
display_text = resolve_ref(ref)
return display_text
+ return re.sub(r"<([@#!]?[^>]*)>", unfurl_ref, text)
+
def unhtmlescape(text):
return text.replace("&lt;", "<") \
