From f4abd6c3d0316014f9609aa7e2ced9e04ca2c01a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 11 Apr 2020 02:05:10 +0200 Subject: [PATCH] Add url support --- royalnet/backpack/commands/version.py | 8 ++++---- royalnet/serf/discord/escape.py | 15 ++++++++++++++- royalnet/serf/telegram/escape.py | 15 +++++++++++++-- royalnet/version.py | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/royalnet/backpack/commands/version.py b/royalnet/backpack/commands/version.py index fee185a2..6b72d368 100644 --- a/royalnet/backpack/commands/version.py +++ b/royalnet/backpack/commands/version.py @@ -1,4 +1,4 @@ -import royalnet +import royalnet.version from royalnet.commands import * @@ -10,9 +10,9 @@ class VersionCommand(Command): async def run(self, args: CommandArgs, data: CommandData) -> None: # noinspection PyUnreachableCode if __debug__: - message = f"ℹ️ Royalnet {royalnet.__version__} (debug)\n" + message = f"ℹ️ Royalnet [url=https://github.com/Steffo99/royalnet/]Unreleased[/url]\n" else: - message = f"ℹ️ Royalnet {royalnet.__version__}\n" - if "69" in message: + message = f"ℹ️ Royalnet [url=https://github.com/Steffo99/royalnet/releases/tag/{royalnet.version.semantic}]{royalnet.version.semantic}[/url]\n" + if "69" in royalnet.version.semantic: message += "(Nice.)" await data.reply(message) diff --git a/royalnet/serf/discord/escape.py b/royalnet/serf/discord/escape.py index 10fa9a26..666a5cec 100644 --- a/royalnet/serf/discord/escape.py +++ b/royalnet/serf/discord/escape.py @@ -1,9 +1,18 @@ +import re + + def escape(string: str) -> str: """Escape a string to be sent through Discord, and format it using RoyalCode. Warning: Currently escapes everything, even items in code blocks.""" - return string.replace("*", "\\*") \ + url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") + url_replacement = r'\2 (\1)' + + escaped_string = string.replace("<", "<").replace(">", ">") + + simple_parse = escaped_string \ + .replace("*", "\\*") \ .replace("_", "\\_") \ .replace("`", "\\`") \ .replace("[b]", "**") \ @@ -16,3 +25,7 @@ def escape(string: str) -> str: .replace("[/c]", "`") \ .replace("[p]", "```") \ .replace("[/p]", "```") + + advanced_parse = re.sub(url_pattern, url_replacement, simple_parse) + + return advanced_parse diff --git a/royalnet/serf/telegram/escape.py b/royalnet/serf/telegram/escape.py index 6833ef6d..ebcc1771 100644 --- a/royalnet/serf/telegram/escape.py +++ b/royalnet/serf/telegram/escape.py @@ -1,10 +1,17 @@ +import re + + def escape(string: str) -> str: """Escape a string to be sent through Telegram (as HTML), and format it using RoyalCode. Warning: Currently escapes everything, even items in code blocks.""" - return string.replace("<", "<") \ - .replace(">", ">") \ + url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]") + url_replacement = r'\2' + + escaped_string = string.replace("<", "<").replace(">", ">") + + simple_parse = escaped_string \ .replace("[b]", "") \ .replace("[/b]", "") \ .replace("[i]", "") \ @@ -15,3 +22,7 @@ def escape(string: str) -> str: .replace("[/c]", "") \ .replace("[p]", "
") \
         .replace("[/p]", "
") + + advanced_parse = re.sub(url_pattern, url_replacement, simple_parse) + + return advanced_parse diff --git a/royalnet/version.py b/royalnet/version.py index 9198a640..064a7eec 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.7.4" +semantic = "5.7.5"