diff options
| author | 2020-05-03 21:45:55 -0700 | |
|---|---|---|
| committer | 2020-06-13 11:21:04 -0700 | |
| commit | 1db3327239c65def7e3ddfcc54453cdadf240a90 (patch) | |
| tree | 99ed981cba8c9e5fe35ef31bbf690ed5b41f71f9 | |
| parent | Code block: add function to find invalid code blocks (diff) | |
Code block: return code blocks with valid ticks but no lang
Such code block will be useful down the road for sending information
on including a language specified if the content successfully parses
as valid Python.
| -rw-r--r-- | bot/cogs/codeblock/cog.py | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/bot/cogs/codeblock/cog.py b/bot/cogs/codeblock/cog.py index 6e87f9f15..970cbd63d 100644 --- a/bot/cogs/codeblock/cog.py +++ b/bot/cogs/codeblock/cog.py @@ -227,26 +227,23 @@ class CodeBlockCog(Cog, name="Code Block"): log.trace("The code consists only of expressions, not sending instructions") @staticmethod - def find_invalid_code_blocks(message: str) -> Sequence[CodeBlock]: + def find_code_blocks(message: str) -> Sequence[CodeBlock]: """ - Find and return all invalid Markdown code blocks in the `message`. + Find and return all 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. + If the `message` contains at least one code block with valid ticks and a specified language, + 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: + language = language.strip() + if tick == BACKTICK and language: return () else: - code_block = CodeBlock(content, language.strip(), tick) + code_block = CodeBlock(content, language, 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: |