aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-05-03 21:40:06 -0700
committerGravatar MarkKoz <[email protected]>2020-06-13 11:21:04 -0700
commitf51b2cacdb8824b51517d10a479be9ec0629d066 (patch)
treede50287de9f14d5a7d35c9ead4818974abc30493
parentCode block: add regex to search for any code blocks (diff)
Code block: add function to find invalid code blocks
* Create a `NamedTuple` representing a code block
-rw-r--r--bot/cogs/codeblock/cog.py34
1 files changed, 32 insertions, 2 deletions
diff --git a/bot/cogs/codeblock/cog.py b/bot/cogs/codeblock/cog.py
index 292735f3f..6e87f9f15 100644
--- a/bot/cogs/codeblock/cog.py
+++ b/bot/cogs/codeblock/cog.py
@@ -2,7 +2,7 @@ import ast
import logging
import re
import time
-from typing import Optional, Tuple
+from typing import NamedTuple, Optional, Sequence, Tuple
import discord
from discord import Embed, Message, RawMessageUpdateEvent
@@ -16,8 +16,9 @@ log = logging.getLogger(__name__)
RE_MARKDOWN = re.compile(r'([*_~`|>])')
RE_CODE_BLOCK_LANGUAGE = re.compile(r"```(?:[^\W_]+)\n(.*?)```", re.DOTALL)
+BACKTICK = "`"
TICKS = {
- "`",
+ BACKTICK,
"'",
'"',
"\u00b4", # ACUTE ACCENT
@@ -43,6 +44,14 @@ RE_CODE_BLOCK = re.compile(
)
+class CodeBlock(NamedTuple):
+ """Represents a Markdown code block."""
+
+ content: str
+ language: str
+ tick: str
+
+
class CodeBlockCog(Cog, name="Code Block"):
"""Detect improperly formatted code blocks and suggest proper formatting."""
@@ -217,6 +226,27 @@ class CodeBlockCog(Cog, name="Code Block"):
else:
log.trace("The code consists only of expressions, not sending instructions")
+ @staticmethod
+ def find_invalid_code_blocks(message: str) -> Sequence[CodeBlock]:
+ """
+ Find and return all invalid Markdown code blocks in the `message`.
+
+ An invalid code block is considered to be one which uses invalid back ticks.
+
+ If the `message` contains at least one valid code block, return an empty sequence. This is
+ based on the assumption that if the user managed to get one code block right, they already
+ know how to fix the rest themselves.
+ """
+ code_blocks = []
+ for _, tick, language, content in RE_CODE_BLOCK.finditer(message):
+ if tick == BACKTICK:
+ return ()
+ else:
+ code_block = CodeBlock(content, language.strip(), tick)
+ code_blocks.append(code_block)
+
+ return code_blocks
+
def fix_indentation(self, msg: str) -> str:
"""Attempts to fix badly indented code."""
def unindent(code: str, skip_spaces: int = 0) -> str: