##// END OF EJS Templates
Merge pull request #6863 from minrk/markdown-tex-math...
Thomas Kluyver -
r18699:8de9a940 merge
parent child Browse files
Show More
@@ -1,143 +1,152
1 # coding: utf-8
1 # coding: utf-8
2 """Tests for conversions from markdown to other formats"""
2 """Tests for conversions from markdown to other formats"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import re
7 from copy import copy
8 from copy import copy
8
9
9 from IPython.utils.py3compat import string_types
10 from IPython.utils.py3compat import string_types
10 from IPython.testing import decorators as dec
11 from IPython.testing import decorators as dec
11
12
12 from ...tests.base import TestsBase
13 from ...tests.base import TestsBase
13 from ..markdown import markdown2latex, markdown2html, markdown2rst
14 from ..markdown import markdown2latex, markdown2html, markdown2rst
14
15
15 from jinja2 import Environment
16 from jinja2 import Environment
16
17
17 class TestMarkdown(TestsBase):
18 class TestMarkdown(TestsBase):
18
19
19 tests = [
20 tests = [
20 '*test',
21 '*test',
21 '**test',
22 '**test',
22 '*test*',
23 '*test*',
23 '_test_',
24 '_test_',
24 '__test__',
25 '__test__',
25 '__*test*__',
26 '__*test*__',
26 '**test**',
27 '**test**',
27 '#test',
28 '#test',
28 '##test',
29 '##test',
29 'test\n----',
30 'test\n----',
30 'test [link](https://google.com/)',
31 'test [link](https://google.com/)',
31 ]
32 ]
32
33
33 tokens = [
34 tokens = [
34 '*test',
35 '*test',
35 '**test',
36 '**test',
36 'test',
37 'test',
37 'test',
38 'test',
38 'test',
39 'test',
39 'test',
40 'test',
40 'test',
41 'test',
41 'test',
42 'test',
42 'test',
43 'test',
43 'test',
44 'test',
44 ('test', 'https://google.com/'),
45 ('test', 'https://google.com/'),
45 ]
46 ]
46
47
47
48
48 @dec.onlyif_cmds_exist('pandoc')
49 @dec.onlyif_cmds_exist('pandoc')
49 def test_markdown2latex(self):
50 def test_markdown2latex(self):
50 """markdown2latex test"""
51 """markdown2latex test"""
51 for index, test in enumerate(self.tests):
52 for index, test in enumerate(self.tests):
52 self._try_markdown(markdown2latex, test, self.tokens[index])
53 self._try_markdown(markdown2latex, test, self.tokens[index])
53
54
54 @dec.onlyif_cmds_exist('pandoc')
55 @dec.onlyif_cmds_exist('pandoc')
55 def test_markdown2latex_markup(self):
56 def test_markdown2latex_markup(self):
56 """markdown2latex with markup kwarg test"""
57 """markdown2latex with markup kwarg test"""
57 # This string should be passed through unaltered with pandoc's
58 # This string should be passed through unaltered with pandoc's
58 # markdown_strict reader
59 # markdown_strict reader
59 s = '1) arabic number with parenthesis'
60 s = '1) arabic number with parenthesis'
60 self.assertEqual(markdown2latex(s, markup='markdown_strict'), s)
61 self.assertEqual(markdown2latex(s, markup='markdown_strict'), s)
61 # This string should be passed through unaltered with pandoc's
62 # This string should be passed through unaltered with pandoc's
62 # markdown_strict+tex_math_dollars reader
63 # markdown_strict+tex_math_dollars reader
63 s = '$\\alpha$ latex math'
64 s = r'$\alpha$ latex math'
64 self.assertEqual(
65 # sometimes pandoc uses $math$, sometimes it uses \(math\)
66 expected = re.compile(r'(\$|\\\()\\alpha(\$|\\\)) latex math')
67 try:
68 # py3
69 assertRegex = self.assertRegex
70 except AttributeError:
71 # py2
72 assertRegex = self.assertRegexpMatches
73 assertRegex(
65 markdown2latex(s, markup='markdown_strict+tex_math_dollars'),
74 markdown2latex(s, markup='markdown_strict+tex_math_dollars'),
66 s)
75 expected)
67
76
68 @dec.onlyif_cmds_exist('pandoc')
77 @dec.onlyif_cmds_exist('pandoc')
69 def test_pandoc_extra_args(self):
78 def test_pandoc_extra_args(self):
70 # pass --no-wrap
79 # pass --no-wrap
71 s = '\n'.join([
80 s = '\n'.join([
72 "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}",
81 "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}",
73 "#rst {{long_line | md2r(['--columns', '5'])}}",
82 "#rst {{long_line | md2r(['--columns', '5'])}}",
74 ])
83 ])
75 long_line = ' '.join(['long'] * 30)
84 long_line = ' '.join(['long'] * 30)
76 env = Environment()
85 env = Environment()
77 env.filters.update({
86 env.filters.update({
78 'md2l': markdown2latex,
87 'md2l': markdown2latex,
79 'md2r': markdown2rst,
88 'md2r': markdown2rst,
80 })
89 })
81 tpl = env.from_string(s)
90 tpl = env.from_string(s)
82 rendered = tpl.render(long_line=long_line)
91 rendered = tpl.render(long_line=long_line)
83 _, latex, rst = rendered.split('#')
92 _, latex, rst = rendered.split('#')
84
93
85 self.assertEqual(latex.strip(), 'latex %s' % long_line)
94 self.assertEqual(latex.strip(), 'latex %s' % long_line)
86 self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n'))
95 self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n'))
87
96
88 def test_markdown2html(self):
97 def test_markdown2html(self):
89 """markdown2html test"""
98 """markdown2html test"""
90 for index, test in enumerate(self.tests):
99 for index, test in enumerate(self.tests):
91 self._try_markdown(markdown2html, test, self.tokens[index])
100 self._try_markdown(markdown2html, test, self.tokens[index])
92
101
93 def test_markdown2html_heading_anchors(self):
102 def test_markdown2html_heading_anchors(self):
94 for md, tokens in [
103 for md, tokens in [
95 ('# test',
104 ('# test',
96 ('<h1', '>test', 'id="test"', u'&#182;</a>', "anchor-link")
105 ('<h1', '>test', 'id="test"', u'&#182;</a>', "anchor-link")
97 ),
106 ),
98 ('###test head space',
107 ('###test head space',
99 ('<h3', '>test head space', 'id="test-head-space"', u'&#182;</a>', "anchor-link")
108 ('<h3', '>test head space', 'id="test-head-space"', u'&#182;</a>', "anchor-link")
100 )
109 )
101 ]:
110 ]:
102 self._try_markdown(markdown2html, md, tokens)
111 self._try_markdown(markdown2html, md, tokens)
103
112
104 def test_markdown2html_math(self):
113 def test_markdown2html_math(self):
105 # Mathematical expressions should be passed through unaltered
114 # Mathematical expressions should be passed through unaltered
106 cases = [("\\begin{equation*}\n"
115 cases = [("\\begin{equation*}\n"
107 "\\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"
116 "\\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"
108 "\\end{equation*}"),
117 "\\end{equation*}"),
109 ("$$\n"
118 ("$$\n"
110 "a = 1 *3* 5\n"
119 "a = 1 *3* 5\n"
111 "$$"),
120 "$$"),
112 "$ a = 1 *3* 5 $",
121 "$ a = 1 *3* 5 $",
113 ]
122 ]
114 for case in cases:
123 for case in cases:
115 self.assertIn(case, markdown2html(case))
124 self.assertIn(case, markdown2html(case))
116
125
117 def test_markdown2html_math_paragraph(self):
126 def test_markdown2html_math_paragraph(self):
118 # https://github.com/ipython/ipython/issues/6724
127 # https://github.com/ipython/ipython/issues/6724
119 a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
128 a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
120 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
129 $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$.
121 With $s_0$ defined as the initial storage content in $t=1$, we have"""
130 With $s_0$ defined as the initial storage content in $t=1$, we have"""
122 self.assertIn(a, markdown2html(a))
131 self.assertIn(a, markdown2html(a))
123
132
124 @dec.onlyif_cmds_exist('pandoc')
133 @dec.onlyif_cmds_exist('pandoc')
125 def test_markdown2rst(self):
134 def test_markdown2rst(self):
126 """markdown2rst test"""
135 """markdown2rst test"""
127
136
128 #Modify token array for rst, escape asterik
137 #Modify token array for rst, escape asterik
129 tokens = copy(self.tokens)
138 tokens = copy(self.tokens)
130 tokens[0] = r'\*test'
139 tokens[0] = r'\*test'
131 tokens[1] = r'\*\*test'
140 tokens[1] = r'\*\*test'
132
141
133 for index, test in enumerate(self.tests):
142 for index, test in enumerate(self.tests):
134 self._try_markdown(markdown2rst, test, tokens[index])
143 self._try_markdown(markdown2rst, test, tokens[index])
135
144
136
145
137 def _try_markdown(self, method, test, tokens):
146 def _try_markdown(self, method, test, tokens):
138 results = method(test)
147 results = method(test)
139 if isinstance(tokens, string_types):
148 if isinstance(tokens, string_types):
140 assert tokens in results
149 assert tokens in results
141 else:
150 else:
142 for token in tokens:
151 for token in tokens:
143 assert token in results
152 assert token in results
General Comments 0
You need to be logged in to leave comments. Login now