##// END OF EJS Templates
Fix mathjax pass-through with mistune
Thomas Kluyver -
Show More
@@ -1,18 +1,11 b''
1 1 """Markdown filters
2
2 3 This file contains a collection of utility filters for dealing with
3 4 markdown within Jinja templates.
4 5 """
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
7 #
6 # Copyright (c) IPython Development Team.
8 7 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12 8
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16 9 from __future__ import print_function
17 10
18 11 # Stdlib imports
@@ -34,9 +27,7 b' from IPython.utils.process import get_output_error_code'
34 27 from IPython.utils.py3compat import cast_bytes
35 28 from IPython.utils.version import check_version
36 29
37 #-----------------------------------------------------------------------------
38 # Functions
39 #-----------------------------------------------------------------------------
30
40 31 marked = os.path.join(os.path.dirname(__file__), "marked.js")
41 32 _node = None
42 33
@@ -72,11 +63,12 b' def markdown2latex(source):'
72 63 return pandoc(source, 'markdown', 'latex')
73 64
74 65 class MathBlockGrammar(mistune.BlockGrammar):
75 block_math = re.compile("^\$\$(.*?)\$\$")
76 block_math2 = re.compile(r"^\\begin(.*?)\\end")
66 block_math = re.compile("^\$\$(.*?)\$\$", re.DOTALL)
67 latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}",
68 re.DOTALL)
77 69
78 70 class MathBlockLexer(mistune.BlockLexer):
79 default_features = ['block_math', 'block_math2'] + mistune.BlockLexer.default_features
71 default_features = ['block_math', 'latex_environment'] + mistune.BlockLexer.default_features
80 72
81 73 def __init__(self, rules=None, **kwargs):
82 74 if rules is None:
@@ -90,7 +82,12 b' class MathBlockLexer(mistune.BlockLexer):'
90 82 'text': m.group(1)
91 83 })
92 84
93 parse_block_math2 = parse_block_math
85 def parse_latex_environment(self, m):
86 self.tokens.append({
87 'type': 'latex_environment',
88 'name': m.group(1),
89 'text': m.group(2)
90 })
94 91
95 92 class MathInlineGrammar(mistune.InlineGrammar):
96 93 math = re.compile("^\$(.+?)\$")
@@ -104,7 +101,7 b' class MathInlineLexer(mistune.InlineLexer):'
104 101 super(MathInlineLexer, self).__init__(renderer, rules, **kwargs)
105 102
106 103 def output_math(self, m):
107 self.renderer.inline_math(m.group(1))
104 return self.renderer.inline_math(m.group(1))
108 105
109 106 class MarkdownWithMath(mistune.Markdown):
110 107 def __init__(self, renderer, **kwargs):
@@ -117,6 +114,9 b' class MarkdownWithMath(mistune.Markdown):'
117 114 def parse_block_math(self):
118 115 return self.renderer.block_math(self.token['text'])
119 116
117 def parse_latex_environment(self):
118 return self.renderer.latex_environment(self.token['name'], self.token['text'])
119
120 120 class IPythonRenderer(mistune.Renderer):
121 121 def block_code(self, code, lang):
122 122 if lang:
@@ -137,6 +137,9 b' class IPythonRenderer(mistune.Renderer):'
137 137 def block_math(self, text):
138 138 return '$$%s$$' % text
139 139
140 def latex_environment(self, name, text):
141 return r'\begin{%s}%s\end{%s}' % (name, text, name)
142
140 143 def inline_math(self, text):
141 144 return '$%s$' % text
142 145
@@ -1,19 +1,7 b''
1 """Tests for conversions from markdown to other formats"""
1 2
2 """
3 Module with tests for Markdown
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2013, the IPython Development Team.
8 #
3 # Copyright (c) IPython Development Team.
9 4 # Distributed under the terms of the Modified BSD License.
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17 5
18 6 from copy import copy
19 7
@@ -24,10 +12,6 b' from ...tests.base import TestsBase'
24 12 from ..markdown import markdown2latex, markdown2html, markdown2rst
25 13
26 14
27 #-----------------------------------------------------------------------------
28 # Class
29 #-----------------------------------------------------------------------------
30
31 15 class TestMarkdown(TestsBase):
32 16
33 17 tests = [
@@ -68,6 +52,19 b' class TestMarkdown(TestsBase):'
68 52 for index, test in enumerate(self.tests):
69 53 self._try_markdown(markdown2html, test, self.tokens[index])
70 54
55 def test_markdown2html_math(self):
56 # Mathematical expressions should be passed through unaltered
57 cases = [("\\begin{equation*}\n"
58 "\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)\n"
59 "\\end{equation*}"),
60 ("$$\n"
61 "a = 1 *3* 5\n"
62 "$$"),
63 "$ a = 1 *3* 5 $",
64 ]
65 for case in cases:
66 self.assertIn(case, markdown2html(case))
67
71 68
72 69 @dec.onlyif_cmds_exist('pandoc')
73 70 def test_markdown2rst(self):
General Comments 0
You need to be logged in to leave comments. Login now