Don't escape formatting characters in URLs

master
Tristan Gosselin-Hane 6 years ago
parent 2eaad47a8d
commit bafc24edc0
No known key found for this signature in database
GPG Key ID: 86886FC44C72EAA1
  1. 29
      minecraft_discord_bridge/minecraft_discord_bridge.py

@ -507,12 +507,31 @@ class MinecraftDiscordBridge():
return emoji_pattern.sub(r'', dirty_string) return emoji_pattern.sub(r'', dirty_string)
def escape_markdown(self, md_string): def escape_markdown(self, md_string):
# Don't mess with urls
url_regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
escaped_string = ""
# Split the message into pieces, each "word" speparated into a string is a piece
# Discord ignores formatting characters in urls so we can't just replace the whole
# string... We need to go through words one by one to find out what is a url (don't)
# escape) and what isn't (escape).
for piece in md_string.split(" "):
if url_regex.match(piece):
escaped_string = "{} {}".format(escaped_string, piece)
continue
# Absolutely needs to go first or it will replace our escaping slashes! # Absolutely needs to go first or it will replace our escaping slashes!
escaped_string = md_string.replace("\\", "\\\\") piece = piece.replace("\\", "\\\\")
escaped_string = escaped_string.replace("_", "\\_") piece = piece.replace("_", "\\_")
escaped_string = escaped_string.replace("*", "\\*") piece = piece.replace("*", "\\*")
if md_string.startswith(">"): escaped_string = "{} {}".format(escaped_string, piece)
md_string = "\\" + md_string if escaped_string.startswith(">"):
escaped_string = "\\" + escaped_string
return escaped_string return escaped_string
def strip_colour(self, dirty_string): def strip_colour(self, dirty_string):

Loading…
Cancel
Save