From 2c6729d1e3d0d242c8f9611137c1e00a55a60969 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 9 Jan 2022 16:24:50 +0000 Subject: Add regexes for matching Discord code blocks These are pulled directly from Python bot's snekbox cog. --- botcore/regex.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/botcore/regex.py b/botcore/regex.py index cb1832d3..036a5113 100644 --- a/botcore/regex.py +++ b/botcore/regex.py @@ -19,3 +19,30 @@ Regex for discord server invites. :meta hide-value: """ + +FORMATTED_CODE_REGEX = re.compile( + r"(?P(?P```)|``?)" # code delimiter: 1-3 backticks; (?P=block) only matches if it's a block + r"(?(block)(?:(?P[a-z]+)\n)?)" # if we're in a block, match optional language (only letters plus newline) + r"(?:[ \t]*\n)*" # any blank (empty or tabs/spaces only) lines before the code + r"(?P.*?)" # extract all code inside the markup + r"\s*" # any more whitespace before the end of the code markup + r"(?P=delim)", # match the exact same delimiter from the start again + re.DOTALL | re.IGNORECASE # "." also matches newlines, case insensitive +) +""" +Regex for formatted code, using Discord's code blocks. + +:meta hide-value: +""" + +RAW_CODE_REGEX = re.compile( + r"^(?:[ \t]*\n)*" # any blank (empty or tabs/spaces only) lines before the code + r"(?P.*?)" # extract all the rest as code + r"\s*$", # any trailing whitespace until the end of the string + re.DOTALL # "." also matches newlines +) +""" +Regex for raw code, *not* using Discord's code blocks. + +:meta hide-value: +""" -- cgit v1.2.3 From 741b4f086dfe73e5e3d45274ece64f21d09d33c9 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 9 Jan 2022 16:35:01 +0000 Subject: Bump version and add changelogs --- docs/changelog.rst | 4 ++++ pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 4954b59c..743fcc20 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,10 @@ Changelog ========= +- :release:`1.2.0 <9th January 2022>` +- :feature:`12` Code block detection regex +- :release:`1.1.0 <2nd December 2021>` - :support:`2` Autogenerated docs. - :feature:`2` Regex utility. +- :release:`1.0.0 <17th November 2021>` - :support:`1` Core package, poetry, and linting CI. diff --git a/pyproject.toml b/pyproject.toml index 0e7d4192..b9a1a66d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bot-core" -version = "0.1.0" +version = "1.2.0" description = "Bot-Core provides the core functionality and utilities for the bots of the Python Discord community." authors = ["Python Discord "] license = "MIT" -- cgit v1.2.3