diff --git a/seminare/style/markdown_extensions.py b/seminare/style/markdown_extensions.py index 4d7679e..5690210 100644 --- a/seminare/style/markdown_extensions.py +++ b/seminare/style/markdown_extensions.py @@ -4,6 +4,7 @@ from markdown import Extension, Markdown from markdown.blockprocessors import BlockProcessor +from markdown.postprocessors import Postprocessor from markdown.preprocessors import Preprocessor from markdown.treeprocessors import Treeprocessor @@ -133,6 +134,35 @@ def fence_logic(self, parent, blocks): self.parser.parseBlocks(d, blocks) +class MathPreprocessor(Preprocessor): + MATH_RE = re.compile( + r"(\$\$.*?(? list[str]: + text = "\n".join(lines) + + if not hasattr(self.md, "math_stash"): + self.md.math_stash = [] + + def repl(match): + placeholder = f"MATH_STASH_{len(self.md.math_stash)}_ENDSTASH" + self.md.math_stash.append(match.group(1)) + return placeholder + + new_text = self.MATH_RE.sub(repl, text) + return new_text.split("\n") + + +class MathPostprocessor(Postprocessor): + def run(self, text: str) -> str: + if hasattr(self.md, "math_stash"): + for i, math_text in enumerate(self.md.math_stash): + placeholder = f"MATH_STASH_{i}_ENDSTASH" + text = text.replace(placeholder, math_text) + return text + + class SeminareExtension(Extension): def __init__(self, image_root: str | None = None, **kwargs) -> None: super().__init__(**kwargs) @@ -152,3 +182,5 @@ def extendMarkdown(self, md: Markdown) -> None: # noqa: N802 md.parser.blockprocessors.register( IOOutputBlockProcessor(md.parser), "IO_output", 100000 ) + md.preprocessors.register(MathPreprocessor(md), "math_stash", 100005) + md.postprocessors.register(MathPostprocessor(md), "math_restore", 100005) diff --git a/seminare/style/templates/root_base.html b/seminare/style/templates/root_base.html index 336eae7..bd5f626 100644 --- a/seminare/style/templates/root_base.html +++ b/seminare/style/templates/root_base.html @@ -13,9 +13,9 @@ - - - + + + @@ -122,7 +122,6 @@ {left: '\\[', right: '\\]', display: true} ], macros: macros, - throwOnError: false }); });