diff --git a/src/api/util.py b/src/api/util.py
index 9d95ee0..0aae16a 100644
--- a/src/api/util.py
+++ b/src/api/util.py
@@ -29,6 +29,7 @@ if TYPE_CHECKING:
logger = logging.getLogger("src.api.util")
+
def default_message(event: str, formatter_hooks: dict) -> Callable:
"""Returns a method of a formatter responsible for the event or None if such does not exist."""
return formatter_hooks.get(event, formatter_hooks.get("generic", formatter_hooks["no_formatter"]))
@@ -40,12 +41,12 @@ def clean_link(link: str) -> str:
def sanitize_to_markdown(text: str):
- """Sanitizes given text to """
+ """Sanitizes given text to escape markdown formatting. It is used in values that will be visible on Discord in messages"""
def sanitize_to_url(text: str) -> str: # TODO ) replaces needed?
- """Formats a link to not embed it"""
- return quote(text, " \\/:").replace(' ', "_")
+ """Formats a string in a way where it can be safely added to a URL without breaking MediaWiki URL schema"""
+ return quote(text, " /:").replace(' ', "_").replace(")", "%29")
def parse_mediawiki_changes(ctx: Context, content: str, embed: DiscordMessage) -> None:
@@ -69,11 +70,6 @@ def parse_mediawiki_changes(ctx: Context, content: str, embed: DiscordMessage) -
embed.add_field(_("Added"), "{data}".format(data=edit_diff.small_prev_ins), inline=True)
-def escape_formatting(data: str) -> str:
- """Escape Discord formatting"""
- return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0)
-
-
def create_article_path(article: str) -> str:
"""Takes the string and creates an URL with it as the article name"""
return src.misc.WIKI_ARTICLE_PATH.replace("$1", article)
diff --git a/src/misc.py b/src/misc.py
index 67a7eb3..a2c847d 100644
--- a/src/misc.py
+++ b/src/misc.py
@@ -15,20 +15,17 @@
# You should have received a copy of the GNU General Public License
# along with RcGcDw. If not, see .
import base64
-import json, logging, sys
+import json, logging, sys, re
from html.parser import HTMLParser
from urllib.parse import urlparse, urlunparse
import requests
-from src.api.util import escape_formatting
from src.configloader import settings
from src.discord.message import DiscordMessage, DiscordMessageMetadata
from src.discord.queue import messagequeue, send_to_discord
from src.exceptions import MediaWikiError
from src.i18n import misc
-AUTO_SUPPRESSION_ENABLED = settings.get("auto_suppression", {"enabled": False}).get("enabled")
-
_ = misc.gettext
# Create a custom logger
@@ -138,6 +135,9 @@ class ContentParser(HTMLParser):
self.empty = True
def handle_data(self, data):
+ def escape_formatting(data: str) -> str:
+ """Escape Discord formatting"""
+ return re.sub(r"([`_*~<>{}@/|\\])", "\\\\\\1", data, 0)
data = escape_formatting(data)
if self.current_tag == "ins" and self.ins_length <= 1000:
self.ins_length += len("**" + data + "**")
diff --git a/test/test_util.py b/test/test_util.py
new file mode 100644
index 0000000..9122ce2
--- /dev/null
+++ b/test/test_util.py
@@ -0,0 +1,26 @@
+# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw).
+#
+# RcGcDw is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# RcGcDw is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with RcGcDw. If not, see .
+
+from unittest import TestCase, main
+from src.api.util import sanitize_to_url
+
+
+class Test(TestCase):
+ def test_sanitize_to_url(self):
+ self.assertEqual(sanitize_to_url("Breaking rcgcdw . \ / : ? = ) & - ~ this is a test)"), "Breaking_rcgcdw_._%5C_/_:_%3F_%3D_%29_%26_-_~_this_is_a_test%29")
+
+
+if __name__ == '__main__':
+ main()