|
35 | 35 | fr""" |
36 | 36 | (?P<ticks> |
37 | 37 | (?P<tick>[{''.join(_TICKS)}]) # Put all ticks into a character class within a group. |
38 | | - \2{{2}} # Match previous group 2 more times to ensure the same char. |
| 38 | + \2* # Match previous group up to N more times to ensure the same char. |
39 | 39 | ) |
40 | | - (?P<lang>[A-Za-z0-9\+\-\.]+\n)? # Optionally match a language specifier followed by a newline. |
| 40 | + (?P<lang>[A-Za-z0-9+\-.]+\s)? # Optionally match a language specifier followed by a whitespace. |
41 | 41 | (?P<code>.+?) # Match the actual code within the block. |
42 | | - \1 # Match the same 3 ticks used at the start of the block. |
| 42 | + \1 # Match the same N ticks used at the start of the block. |
43 | 43 | """, |
44 | 44 | re.DOTALL | re.VERBOSE |
45 | 45 | ) |
@@ -86,11 +86,12 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock] | None: |
86 | 86 | for match in _RE_CODE_BLOCK.finditer(message): |
87 | 87 | # Used to ensure non-matched groups have an empty string as the default value. |
88 | 88 | groups = match.groupdict("") |
89 | | - language = groups["lang"].strip() # Strip the newline cause it's included in the group. |
| 89 | + language = groups["lang"].strip() # Strip the whitespace cause it's included in the group. |
90 | 90 |
|
91 | | - if groups["tick"] == BACKTICK and language: |
| 91 | + if groups["tick"] == BACKTICK and len(groups["ticks"]) == 3 and language and ("\n" in groups["lang"]): |
92 | 92 | log.trace("Message has a valid code block with a language; returning None.") |
93 | 93 | return None |
| 94 | + |
94 | 95 | if has_lines(groups["code"], constants.CodeBlock.minimum_lines): |
95 | 96 | code_block = CodeBlock(groups["code"], language, groups["tick"]) |
96 | 97 | code_blocks.append(code_block) |
@@ -181,14 +182,15 @@ def parse_bad_language(content: str) -> BadLanguage | None: |
181 | 182 | ) |
182 | 183 |
|
183 | 184 |
|
184 | | -def _get_leading_spaces(content: str) -> int: |
| 185 | +def _get_leading_spaces(content: str) -> int | None: |
185 | 186 | """Return the number of spaces at the start of the first line in `content`.""" |
186 | 187 | leading_spaces = 0 |
187 | 188 | for char in content: |
188 | 189 | if char == " ": |
189 | 190 | leading_spaces += 1 |
190 | 191 | else: |
191 | 192 | return leading_spaces |
| 193 | + |
192 | 194 | return None |
193 | 195 |
|
194 | 196 |
|
|
0 commit comments