Skip to content

Commit

Permalink
fix code_blocks scope
Browse files Browse the repository at this point in the history
  • Loading branch information
navanchauhan committed Mar 27, 2024
1 parent d8ec9b6 commit 343ae0a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/markdown2.py
Expand Up @@ -2777,6 +2777,7 @@ class Latex(Extra):
_pre_code_block_re = re.compile(r"<pre>(.*?)</pre>", re.DOTALL)

converter = None
code_blocks = {}

def _convert_single_match(self, match):
return self.converter.convert(match.group(1))
Expand All @@ -2785,8 +2786,8 @@ def _convert_double_match(self, match):
return self.converter.convert(match.group(1).replace(r"\n", ''), display="block")

def code_placeholder(self, match):
placeholder = f"<!--CODE_BLOCK_{len(code_blocks)}-->"
code_blocks[placeholder] = match.group(0)
placeholder = f"<!--CODE_BLOCK_{len(self.code_blocks)}-->"
self.code_blocks[placeholder] = match.group(0)
return placeholder

def run(self, text):
Expand All @@ -2796,16 +2797,14 @@ def run(self, text):
except ImportError:
raise ImportError('The "latex" extra requires the "latex2mathml" package to be installed.')

code_blocks = {}

# Replace code blocks with placeholder tag
text = self._pre_code_block_re.sub(self.code_placeholder, text)

text = self._single_dollar_re.sub(self._convert_single_match, text)
text = self._double_dollar_re.sub(self._convert_double_match, text)

# Convert placeholder tag back to original code
for placeholder, code_block in code_blocks.items():
for placeholder, code_block in self.code_blocks.items():
text = text.replace(placeholder, code_block)

return text
Expand Down

0 comments on commit 343ae0a

Please sign in to comment.