diff options
author | 2021-09-29 23:14:13 +0100 | |
---|---|---|
committer | 2021-09-29 22:14:13 +0000 | |
commit | 58098b5121a4ebb06b6f5ed411143ec8d5766266 (patch) | |
tree | 02eaebab5f0bcdfb2dfa0d67438d944431be4558 | |
parent | Merge pull request #1848 from python-discord/add-more-supported-symbols-to-otn (diff) |
python-news escape fixes (#1822)
* Fix escapes in python-news posts
No longer escapes markdown inside of codeblocks or pre-escaped markdown.
Co-authored-by: Ryu18 <[email protected]>
* Add escaping to title of py-news posts
* Fix typo
Co-authored-by: Ryu18 <[email protected]>
-rw-r--r-- | bot/exts/info/python_news.py | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/bot/exts/info/python_news.py b/bot/exts/info/python_news.py index 58dcd3a02..2a8b64f32 100644 --- a/bot/exts/info/python_news.py +++ b/bot/exts/info/python_news.py @@ -23,6 +23,14 @@ THREAD_URL = "https://mail.python.org/archives/list/{list}@python.org/thread/{id AVATAR_URL = "https://www.python.org/static/opengraph-icon-200x200.png" +# By first matching everything within a codeblock, +# when matching markdown it won't be within a codeblock +MARKDOWN_REGEX = re.compile( + r"(?P<codeblock>`.*?`)" # matches everything within a codeblock + r"|(?P<markdown>(?<!\\)[_|])", # matches unescaped `_` and `|` + re.DOTALL # required to support multi-line codeblocks +) + log = logging.getLogger(__name__) @@ -76,8 +84,11 @@ class PythonNews(Cog): @staticmethod def escape_markdown(content: str) -> str: - """Escape the markdown underlines and spoilers.""" - return re.sub(r"[_|]", lambda match: "\\" + match[0], content) + """Escape the markdown underlines and spoilers that aren't in codeblocks.""" + return MARKDOWN_REGEX.sub( + lambda match: match.group("codeblock") or "\\" + match.group("markdown"), + content + ) async def post_pep_news(self) -> None: """Fetch new PEPs and when they don't have announcement in #python-news, create it.""" @@ -109,7 +120,7 @@ class PythonNews(Cog): # Build an embed and send a webhook embed = discord.Embed( - title=new["title"], + title=self.escape_markdown(new["title"]), description=self.escape_markdown(new["summary"]), timestamp=new_datetime, url=new["link"], @@ -129,7 +140,7 @@ class PythonNews(Cog): self.bot.stats.incr("python_news.posted.pep") if msg.channel.is_news(): - log.trace("Publishing PEP annnouncement because it was in a news channel") + log.trace("Publishing PEP announcement because it was in a news channel") await msg.publish() # Apply new sent news to DB to avoid duplicate sending @@ -179,7 +190,7 @@ class PythonNews(Cog): # Build an embed and send a message to the webhook embed = discord.Embed( - title=thread_information["subject"], + title=self.escape_markdown(thread_information["subject"]), description=content[:1000] + f"... [continue reading]({link})" if len(content) > 1000 else content, timestamp=new_date, url=link, |