##// END OF EJS Templates
Merge pull request #8066 from bollwyvl/mistune-math...
Thomas Kluyver -
r20752:9af7b2bc merge
parent child Browse files
Show More
@@ -23,7 +23,7 from IPython.utils.decorators import undoc
23
23
24 @undoc
24 @undoc
25 class MathBlockGrammar(mistune.BlockGrammar):
25 class MathBlockGrammar(mistune.BlockGrammar):
26 block_math = re.compile("^\$\$(.*?)\$\$", re.DOTALL)
26 block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL)
27 latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}",
27 latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}",
28 re.DOTALL)
28 re.DOTALL)
29
29
@@ -52,12 +52,13 class MathBlockLexer(mistune.BlockLexer):
52
52
53 @undoc
53 @undoc
54 class MathInlineGrammar(mistune.InlineGrammar):
54 class MathInlineGrammar(mistune.InlineGrammar):
55 math = re.compile("^\$(.+?)\$")
55 math = re.compile(r"^\$(.+?)\$")
56 block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL)
56 text = re.compile(r'^[\s\S]+?(?=[\\<!\[_*`~$]|https?://| {2,}\n|$)')
57 text = re.compile(r'^[\s\S]+?(?=[\\<!\[_*`~$]|https?://| {2,}\n|$)')
57
58
58 @undoc
59 @undoc
59 class MathInlineLexer(mistune.InlineLexer):
60 class MathInlineLexer(mistune.InlineLexer):
60 default_rules = ['math'] + mistune.InlineLexer.default_rules
61 default_rules = ['math', 'block_math'] + mistune.InlineLexer.default_rules
61
62
62 def __init__(self, renderer, rules=None, **kwargs):
63 def __init__(self, renderer, rules=None, **kwargs):
63 if rules is None:
64 if rules is None:
@@ -67,6 +68,9 class MathInlineLexer(mistune.InlineLexer):
67 def output_math(self, m):
68 def output_math(self, m):
68 return self.renderer.inline_math(m.group(1))
69 return self.renderer.inline_math(m.group(1))
69
70
71 def output_block_math(self, m):
72 return self.renderer.block_math(m.group(1))
73
70 @undoc
74 @undoc
71 class MarkdownWithMath(mistune.Markdown):
75 class MarkdownWithMath(mistune.Markdown):
72 def __init__(self, renderer, **kwargs):
76 def __init__(self, renderer, **kwargs):
@@ -122,13 +122,47 class TestMarkdown(TestsBase):
122 ]
122 ]
123 for case in cases:
123 for case in cases:
124 self.assertIn(case, markdown2html(case))
124 self.assertIn(case, markdown2html(case))
125
126 def test_markdown2html_math_mixed(self):
127 """ensure markdown between inline and inline-block math"""
128 case = """The entries of $C$ are given by the exact formula:
129 $$
130 C_{ik} = \sum_{j=1}^n A_{ij} B_{jk}
131 $$
132 but there are many ways to _implement_ this computation. $\approx 2mnp$ flops"""
133 self._try_markdown(markdown2html, case,
134 case.replace("_implement_", "<em>implement</em>"))
125
135
126 def test_markdown2html_math_paragraph(self):
136 def test_markdown2html_math_paragraph(self):
127 # https://github.com/ipython/ipython/issues/6724
137 """these should all parse without modification"""
128 a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
138 cases = [
139 # https://github.com/ipython/ipython/issues/6724
140 """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
129 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
141 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
130 With $s_0$ defined as the initial storage content in $t=1$, we have"""
142 With $s_0$ defined as the initial storage content in $t=1$, we have""",
131 self.assertIn(a, markdown2html(a))
143 # https://github.com/jupyter/nbviewer/issues/420
144 """$C_{ik}$
145 $$
146 C_{ik} = \sum_{j=1}
147 $$
148 $C_{ik}$""",
149 """$m$
150 $$
151 C = \begin{pmatrix}
152 0 & 0 & 0 & \cdots & 0 & 0 & -c_0 \\
153 0 & 0 & 0 & \cdots & 0 & 1 & -c_{m-1}
154 \end{pmatrix}
155 $$
156 $x^m$""",
157 """$r=\overline{1,n}$
158 $$ {\bf
159 b}_{i}^{r}(t)=(1-t)\,{\bf b}_{i}^{r-1}(t)+t\,{\bf b}_{i+1}^{r-1}(t),\:
160 i=\overline{0,n-r}, $$
161 i.e. the $i^{th}$"""
162 ]
163
164 for case in cases:
165 self.assertIn(case, markdown2html(case))
132
166
133 @dec.onlyif_cmds_exist('pandoc')
167 @dec.onlyif_cmds_exist('pandoc')
134 def test_markdown2rst(self):
168 def test_markdown2rst(self):
General Comments 0
You need to be logged in to leave comments. Login now