aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2022-01-09 19:33:09 +0200
committerGravatar GitHub <[email protected]>2022-01-09 19:33:09 +0200
commit511bcba1b0196cd498c707a525ea56921bd971db (patch)
treea46bcf6189fe59657a52ab244a27a1d4827b92c2
parentMerge pull request #11 from python-discord/dependabot/pip/gitpython-3.1.25 (diff)
parentMerge branch 'main' into add-code-block-regex (diff)
Merge pull request #12 from python-discord/add-code-block-regexv1.2.0
Add regexes for matching Discord code blocks
-rw-r--r--botcore/regex.py27
-rw-r--r--docs/changelog.rst4
-rw-r--r--pyproject.toml2
3 files changed, 32 insertions, 1 deletions
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<delim>(?P<block>```)|``?)" # code delimiter: 1-3 backticks; (?P=block) only matches if it's a block
+ r"(?(block)(?:(?P<lang>[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<code>.*?)" # 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<code>.*?)" # 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:
+"""
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 48e07230..acb8b5a1 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 <[email protected]>"]
license = "MIT"