From d8ec9b646f9eb41ccfec0e1a7b5c1ddda3d3d2b7 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Tue, 26 Mar 2024 17:54:08 -0600 Subject: [PATCH 1/6] add latex extra --- lib/markdown2.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 3 ++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index 380b36d3..f3f92e34 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2764,6 +2764,51 @@ def sub(self, match): def run(self, text): return self.fenced_code_block_re.sub(self.sub, text) +class Latex(Extra): + ''' + Convert $ and $$ to and tags for inline and block math. + ''' + name = 'latex' + order = (), () + + _single_dollar_re = re.compile(r'(?(.*?)", re.DOTALL) + + converter = None + + def _convert_single_match(self, match): + return self.converter.convert(match.group(1)) + + 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_blocks[placeholder] = match.group(0) + return placeholder + + def run(self, text): + try: + import latex2mathml.converter + self.converter = latex2mathml.converter + 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(): + text = text.replace(placeholder, code_block) + + return text class LinkPatterns(Extra): ''' @@ -3328,6 +3373,7 @@ def test(self, text): Breaks.register() CodeFriendly.register() FencedCodeBlocks.register() +Latex.register() LinkPatterns.register() MarkdownInHTML.register() MiddleWordEm.register() diff --git a/setup.py b/setup.py index 9962f64a..23a52148 100755 --- a/setup.py +++ b/setup.py @@ -33,7 +33,8 @@ extras_require = { "code_syntax_highlighting": ["pygments>=2.7.3"], - "wavedrom": ["wavedrom; python_version>='3.7'"] + "wavedrom": ["wavedrom; python_version>='3.7'"], + "latex": ["python_version>='3.8.1'; latex2mathml"] } # nested listcomp to combine all optional extras into convenient "all" option extras_require["all"] = [i for v in tuple(extras_require.values()) for i in v] From 343ae0aaa95e4598c307fe95d1d267093f385094 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Tue, 26 Mar 2024 18:07:13 -0600 Subject: [PATCH 2/6] fix code_blocks scope --- lib/markdown2.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index f3f92e34..3b75295b 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2777,6 +2777,7 @@ class Latex(Extra): _pre_code_block_re = re.compile(r"
(.*?)
", re.DOTALL) converter = None + code_blocks = {} def _convert_single_match(self, match): return self.converter.convert(match.group(1)) @@ -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_blocks[placeholder] = match.group(0) + placeholder = f"" + self.code_blocks[placeholder] = match.group(0) return placeholder def run(self, text): @@ -2796,8 +2797,6 @@ 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) @@ -2805,7 +2804,7 @@ def run(self, 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 From d638b6e21e4627b6d7b5ad6c0a7f95abbfa5ebe1 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Wed, 27 Mar 2024 11:52:15 -0600 Subject: [PATCH 3/6] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 23a52148..72cc5ef8 100755 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ extras_require = { "code_syntax_highlighting": ["pygments>=2.7.3"], "wavedrom": ["wavedrom; python_version>='3.7'"], - "latex": ["python_version>='3.8.1'; latex2mathml"] + "latex": ['latex2mathml; python_version>="3.8.1"'], } # nested listcomp to combine all optional extras into convenient "all" option extras_require["all"] = [i for v in tuple(extras_require.values()) for i in v] From 1c7f4a38ed481546d6a1192fbc8d470a2ca31233 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Mon, 1 Apr 2024 15:58:47 -0600 Subject: [PATCH 4/6] disable between codeblocks --- lib/markdown2.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index 3b75295b..42a1518c 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -2769,12 +2769,15 @@ class Latex(Extra): Convert $ and $$ to and tags for inline and block math. ''' name = 'latex' - order = (), () + order = (Stage.CODE_BLOCKS, FencedCodeBlocks), () _single_dollar_re = re.compile(r'(?(.*?)", re.DOTALL) + + # Ways to escape + _pre_code_block_re = re.compile(r"
(.*?)
", re.DOTALL) # Wraped in
+    _triple_re = re.compile(r'```(.*?)```', re.DOTALL) # Wrapped in a code block ```
+    _single_re = re.compile(r'(?
Date: Mon, 1 Apr 2024 15:59:21 -0600
Subject: [PATCH 5/6] test for latex

---
 test/test.py             |  2 +-
 test/tm-cases/latex.html | 11 +++++++++++
 test/tm-cases/latex.opts |  1 +
 test/tm-cases/latex.tags |  1 +
 test/tm-cases/latex.text | 10 ++++++++++
 5 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 test/tm-cases/latex.html
 create mode 100644 test/tm-cases/latex.opts
 create mode 100644 test/tm-cases/latex.tags
 create mode 100644 test/tm-cases/latex.text

diff --git a/test/test.py b/test/test.py
index 8cdf3a89..871cd2e1 100755
--- a/test/test.py
+++ b/test/test.py
@@ -38,7 +38,7 @@ def setup():
     setup()
     default_tags = []
     warnings = []
-    for extra_lib in ('pygments', 'wavedrom'):
+    for extra_lib in ('pygments', 'wavedrom', 'latex2mathml'):
         try:
             mod = importlib.import_module(extra_lib)
         except ImportError:
diff --git a/test/tm-cases/latex.html b/test/tm-cases/latex.html
new file mode 100644
index 00000000..d5eec8ca
--- /dev/null
+++ b/test/tm-cases/latex.html
@@ -0,0 +1,11 @@
+

Simple Test

+ +

Inline Equations can be written as y=mx+b. +Block equations are wrapped using

+ +x=b±b24ac2a + +

This code will render everywhere + +some random code, describing $a and $b will be rendered, $y=mx$ +

diff --git a/test/tm-cases/latex.opts b/test/tm-cases/latex.opts new file mode 100644 index 00000000..627b9f1f --- /dev/null +++ b/test/tm-cases/latex.opts @@ -0,0 +1 @@ +{"extras": ["latex","latex2mathml"]} \ No newline at end of file diff --git a/test/tm-cases/latex.tags b/test/tm-cases/latex.tags new file mode 100644 index 00000000..b39d550d --- /dev/null +++ b/test/tm-cases/latex.tags @@ -0,0 +1 @@ +extra latex latex2mathml \ No newline at end of file diff --git a/test/tm-cases/latex.text b/test/tm-cases/latex.text new file mode 100644 index 00000000..5b8933ce --- /dev/null +++ b/test/tm-cases/latex.text @@ -0,0 +1,10 @@ +## Simple Test +Inline Equations can be written as $y=mx+b$. +Block equations are wrapped using +$$ +x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} +$$ +This code will render everywhere +``` +some random code, describing $a and $b will be rendered, $y=mx$ +``` \ No newline at end of file From d171ac9ef13a4fd2b875e274c511f68f8f7e5637 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Mon, 1 Apr 2024 16:04:26 -0600 Subject: [PATCH 6/6] test to check backticks --- test/tm-cases/latex.html | 11 +++++++++-- test/tm-cases/latex.text | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/test/tm-cases/latex.html b/test/tm-cases/latex.html index d5eec8ca..b91fd4b9 100644 --- a/test/tm-cases/latex.html +++ b/test/tm-cases/latex.html @@ -5,7 +5,14 @@

Simple Test

x=b±b24ac2a -

This code will render everywhere +

This code block will not have the math rendered. -some random code, describing $a and $b will be rendered, $y=mx$ +some random code, describing $a and $b will not be rendered, $y=mx$ + +This will not work either $\sqrt{2} or

+ +

+$$ +f = 12 +$$

diff --git a/test/tm-cases/latex.text b/test/tm-cases/latex.text index 5b8933ce..92a9bf29 100644 --- a/test/tm-cases/latex.text +++ b/test/tm-cases/latex.text @@ -4,7 +4,14 @@ Block equations are wrapped using $$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$ -This code will render everywhere +This code block will not have the math rendered. ``` -some random code, describing $a and $b will be rendered, $y=mx$ +some random code, describing $a and $b will not be rendered, $y=mx$ +``` +This will not work either `$\sqrt{2}` or + +``` +$$ +f = 12 +$$ ``` \ No newline at end of file