diff options
| author | 2020-05-06 14:18:40 -0700 | |
|---|---|---|
| committer | 2020-06-13 11:21:06 -0700 | |
| commit | 8782d3018e5cbc4ef04e4b8e74b90025de3004b3 (patch) | |
| tree | cb30b4ca6a57e5c58c45c4417eb7b01e1b7eb860 /bot/cogs/codeblock/parsing.py | |
| parent | Code block: load the extension (diff) | |
Code block: fix find_code_blocks iteration and missing return
* Add named capture groups to the regex
Diffstat (limited to '')
| -rw-r--r-- | bot/cogs/codeblock/parsing.py | 27 | 
1 files changed, 16 insertions, 11 deletions
| diff --git a/bot/cogs/codeblock/parsing.py b/bot/cogs/codeblock/parsing.py index 88a5c7b7a..9adb4e0ab 100644 --- a/bot/cogs/codeblock/parsing.py +++ b/bot/cogs/codeblock/parsing.py @@ -21,13 +21,13 @@ TICKS = {  }  RE_CODE_BLOCK = re.compile(      fr""" -    ( -        ([{''.join(TICKS)}])  # Put all ticks into a character class within a group. -        \2{{2}}               # Match the previous group 2 more times to ensure it's the same char. +    (?P<ticks> +        (?P<tick>[{''.join(TICKS)}])  # Put all ticks into a character class within a group. +        \2{{2}}                       # Match previous group 2 more times to ensure the same char.      ) -    ([^\W_]+\n)?              # Optionally match a language specifier followed by a newline. -    (.+?)                     # Match the actual code within the block. -    \1                        # Match the same 3 ticks used at the start of the block. +    (?P<lang>[^\W_]+\n)?              # Optionally match a language specifier followed by a newline. +    (?P<code>.+?)                     # Match the actual code within the block. +    \1                                # Match the same 3 ticks used at the start of the block.      """,      re.DOTALL | re.VERBOSE  ) @@ -52,14 +52,19 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock]:      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): -        language = language.strip() -        if tick == BACKTICK and language: +    for match in RE_CODE_BLOCK.finditer(message): +        # Used to ensure non-matched groups have an empty string as the default value. +        groups = match.groupdict("") +        language = groups["lang"].strip()  # Strip the newline cause it's included in the group. + +        if groups["tick"] == BACKTICK and language:              return () -        elif len(content.split("\n", 3)) > 3: -            code_block = CodeBlock(content, language, tick) +        elif len(groups["code"].split("\n", 3)) > 3: +            code_block = CodeBlock(groups["code"], language, groups["tick"])              code_blocks.append(code_block) +    return code_blocks +  def is_python_code(content: str) -> bool:      """Return True if `content` is valid Python consisting of more than just expressions.""" | 
