diff options
| author | 2022-01-09 16:24:50 +0000 | |
|---|---|---|
| committer | 2022-01-09 16:24:50 +0000 | |
| commit | 2c6729d1e3d0d242c8f9611137c1e00a55a60969 (patch) | |
| tree | 3a238cd75dd68c722fe00d58e0536375cccf276c /botcore | |
| parent | Merge pull request #6 from python-discord/dependabot/pip/sphinx-4.3.2 (diff) | |
Add regexes for matching Discord code blocks
These are pulled directly from Python bot's snekbox cog.
Diffstat (limited to 'botcore')
| -rw-r--r-- | botcore/regex.py | 27 | 
1 files changed, 27 insertions, 0 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: +"""  |