4747 """ ,
4848 re .DOTALL | re .VERBOSE
4949)
50- _RE_CODE_BLOCK_REGEX = regex .compile (_RE_CODE_BLOCK .pattern , regex .DOTALL | regex .VERBOSE )
50+ # copy of _RE_CODE_BLOCK. Done like this for highlighting reasons (regex.compile doesn't properly highlight)
51+ _REGEX_CODE_BLOCK = regex .compile (_RE_CODE_BLOCK .pattern , regex .DOTALL | regex .VERBOSE )
5152
5253_RE_LANGUAGE = re .compile (
5354 fr"""
@@ -66,6 +67,7 @@ class CodeBlock(NamedTuple):
6667 language : str
6768 ticks : str
6869 tick : str
70+ is_python : bool
6971
7072
7173class BadLanguage (NamedTuple ):
@@ -89,7 +91,7 @@ def find_faulty_code_blocks(message: str) -> Sequence[CodeBlock] | None:
8991 log .trace ("Finding all code blocks in a message." )
9092
9193 code_blocks = []
92- for match in _RE_CODE_BLOCK_REGEX .finditer (message , overlapped = True ):
94+ for match in _REGEX_CODE_BLOCK .finditer (message , overlapped = True ):
9395 # Used to ensure non-matched groups have an empty string as the default value.
9496 groups = match .groupdict ("" )
9597 language = groups ["lang" ].strip () # Strip the whitespace cause it's included in the group.
@@ -102,11 +104,12 @@ def find_faulty_code_blocks(message: str) -> Sequence[CodeBlock] | None:
102104 log .trace ("Skipped a code block shorter than 4 lines." )
103105 continue
104106
107+ is_python = is_python_code (groups ["code" ])
105108 if (groups ["tick" ] == BACKTICK
106- or (language in PY_LANG_CODES and is_python_code ( groups [ "code" ]) )
109+ or (language in PY_LANG_CODES and is_python )
107110 or len (groups ["ticks" ]) >= 2 ):
108111 log .trace ("Message has an invalid code block." )
109- code_block = CodeBlock (groups ["code" ], language , groups ["ticks" ], groups ["tick" ])
112+ code_block = CodeBlock (groups ["code" ], language , groups ["ticks" ], groups ["tick" ], is_python )
110113 code_blocks .append (code_block )
111114 else :
112115 log .trace ("Skipped invalid code block due to uncertainty if it is supposed to be a code block." )
0 commit comments