Skip to content

Commit 2ade4a3

Browse files
added is_python field for code blocks; added comment explaining regex duplication
1 parent d92d178 commit 2ade4a3

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

bot/exts/info/codeblock/_instructions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _get_bad_ticks_message(code_block: _parsing.CodeBlock) -> str | None:
4444
log.trace("Check if the bad ticks code block also has issues with the language specifier.")
4545
addition_msg = _get_bad_lang_message(code_block.content)
4646
if not addition_msg and not code_block.language:
47-
addition_msg = _get_no_lang_message(code_block.content)
47+
addition_msg = _get_no_lang_message(code_block)
4848

4949
# Combine the back ticks message with the language specifier message. The latter will
5050
# already have an example code block.
@@ -112,15 +112,15 @@ def _get_bad_lang_message(content: str) -> str | None:
112112
return None
113113

114114

115-
def _get_no_lang_message(content: str) -> str | None:
115+
def _get_no_lang_message(code_block: _parsing.CodeBlock) -> str | None:
116116
"""
117117
Return instructions on specifying a language for a code block.
118118
119119
If `content` is not valid Python or Python REPL code, return None.
120120
"""
121121
log.trace("Creating instructions for a missing language.")
122122

123-
if _parsing.is_python_code(content):
123+
if code_block.is_python:
124124
example_blocks = _get_example("py")
125125

126126
# Note that _get_bad_ticks_message expects the first line to have two newlines.
@@ -160,6 +160,6 @@ def get_instructions(content: str) -> str | None:
160160
# Check for a bad language first to avoid parsing content into an AST.
161161
instructions = _get_bad_lang_message(block.content)
162162
if not instructions:
163-
instructions = _get_no_lang_message(block.content)
163+
instructions = _get_no_lang_message(block)
164164

165165
return instructions

bot/exts/info/codeblock/_parsing.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
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

7173
class 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

Comments
 (0)