1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add url support

This commit is contained in:
Steffo 2020-04-11 02:05:10 +02:00
parent 02789649d9
commit f4abd6c3d0
4 changed files with 32 additions and 8 deletions

View file

@ -1,4 +1,4 @@
import royalnet import royalnet.version
from royalnet.commands import * from royalnet.commands import *
@ -10,9 +10,9 @@ class VersionCommand(Command):
async def run(self, args: CommandArgs, data: CommandData) -> None: async def run(self, args: CommandArgs, data: CommandData) -> None:
# noinspection PyUnreachableCode # noinspection PyUnreachableCode
if __debug__: if __debug__:
message = f" Royalnet {royalnet.__version__} (debug)\n" message = f" Royalnet [url=https://github.com/Steffo99/royalnet/]Unreleased[/url]\n"
else: else:
message = f" Royalnet {royalnet.__version__}\n" message = f" Royalnet [url=https://github.com/Steffo99/royalnet/releases/tag/{royalnet.version.semantic}]{royalnet.version.semantic}[/url]\n"
if "69" in message: if "69" in royalnet.version.semantic:
message += "(Nice.)" message += "(Nice.)"
await data.reply(message) await data.reply(message)

View file

@ -1,9 +1,18 @@
import re
def escape(string: str) -> str: def escape(string: str) -> str:
"""Escape a string to be sent through Discord, and format it using RoyalCode. """Escape a string to be sent through Discord, and format it using RoyalCode.
Warning: Warning:
Currently escapes everything, even items in code blocks.""" 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("<", "&lt;").replace(">", "&gt;")
simple_parse = escaped_string \
.replace("*", "\\*") \
.replace("_", "\\_") \ .replace("_", "\\_") \
.replace("`", "\\`") \ .replace("`", "\\`") \
.replace("[b]", "**") \ .replace("[b]", "**") \
@ -16,3 +25,7 @@ def escape(string: str) -> str:
.replace("[/c]", "`") \ .replace("[/c]", "`") \
.replace("[p]", "```") \ .replace("[p]", "```") \
.replace("[/p]", "```") .replace("[/p]", "```")
advanced_parse = re.sub(url_pattern, url_replacement, simple_parse)
return advanced_parse

View file

@ -1,10 +1,17 @@
import re
def escape(string: str) -> str: def escape(string: str) -> str:
"""Escape a string to be sent through Telegram (as HTML), and format it using RoyalCode. """Escape a string to be sent through Telegram (as HTML), and format it using RoyalCode.
Warning: Warning:
Currently escapes everything, even items in code blocks.""" Currently escapes everything, even items in code blocks."""
return string.replace("<", "&lt;") \ url_pattern = re.compile(r"\[url=(.*?)](.*?)\[/url]")
.replace(">", "&gt;") \ url_replacement = r'<a href="\1">\2</a>'
escaped_string = string.replace("<", "&lt;").replace(">", "&gt;")
simple_parse = escaped_string \
.replace("[b]", "<b>") \ .replace("[b]", "<b>") \
.replace("[/b]", "</b>") \ .replace("[/b]", "</b>") \
.replace("[i]", "<i>") \ .replace("[i]", "<i>") \
@ -15,3 +22,7 @@ def escape(string: str) -> str:
.replace("[/c]", "</code>") \ .replace("[/c]", "</code>") \
.replace("[p]", "<pre>") \ .replace("[p]", "<pre>") \
.replace("[/p]", "</pre>") .replace("[/p]", "</pre>")
advanced_parse = re.sub(url_pattern, url_replacement, simple_parse)
return advanced_parse

View file

@ -1 +1 @@
semantic = "5.7.4" semantic = "5.7.5"