Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions bot/exts/info/codeblock/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
fr"""
(?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.
\2* # Match previous group up to N more times to ensure the same char.
)
(?P<lang>[A-Za-z0-9\+\-\.]+\n)? # Optionally match a language specifier followed by a newline.
(?P<lang>[A-Za-z0-9+\-.]+\s)? # Optionally match a language specifier followed by a whitespace.
(?P<code>.+?) # Match the actual code within the block.
\1 # Match the same 3 ticks used at the start of the block.
\1 # Match the same N ticks used at the start of the block.
""",
re.DOTALL | re.VERBOSE
)
Expand Down Expand Up @@ -86,11 +86,12 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock] | None:
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.
language = groups["lang"].strip() # Strip the whitespace cause it's included in the group.

if groups["tick"] == BACKTICK and language:
if groups["tick"] == BACKTICK and len(groups["ticks"]) == 3 and language and ("\n" in groups["lang"]):
log.trace("Message has a valid code block with a language; returning None.")
return None

if has_lines(groups["code"], constants.CodeBlock.minimum_lines):
code_block = CodeBlock(groups["code"], language, groups["tick"])
code_blocks.append(code_block)
Expand Down Expand Up @@ -181,14 +182,15 @@ def parse_bad_language(content: str) -> BadLanguage | None:
)


def _get_leading_spaces(content: str) -> int:
def _get_leading_spaces(content: str) -> int | None:
"""Return the number of spaces at the start of the first line in `content`."""
leading_spaces = 0
for char in content:
if char == " ":
leading_spaces += 1
else:
return leading_spaces

return None


Expand Down