Show More
@@ -1,109 +1,115 b'' | |||||
1 | """Tests for conversions from markdown to other formats""" |
|
1 | """Tests for conversions from markdown to other formats""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | from copy import copy |
|
6 | from copy import copy | |
7 |
|
7 | |||
8 | from IPython.utils.py3compat import string_types |
|
8 | from IPython.utils.py3compat import string_types | |
9 | from IPython.testing import decorators as dec |
|
9 | from IPython.testing import decorators as dec | |
10 |
|
10 | |||
11 | from ...tests.base import TestsBase |
|
11 | from ...tests.base import TestsBase | |
12 | from ..markdown import markdown2latex, markdown2html, markdown2rst |
|
12 | from ..markdown import markdown2latex, markdown2html, markdown2rst | |
13 |
|
13 | |||
14 | from jinja2 import Environment |
|
14 | from jinja2 import Environment | |
15 |
|
15 | |||
16 | class TestMarkdown(TestsBase): |
|
16 | class TestMarkdown(TestsBase): | |
17 |
|
17 | |||
18 | tests = [ |
|
18 | tests = [ | |
19 | '*test', |
|
19 | '*test', | |
20 | '**test', |
|
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\n----', |
|
28 | 'test\n----', | |
29 | 'test [link](https://google.com/)'] |
|
29 | 'test [link](https://google.com/)'] | |
30 |
|
30 | |||
31 | tokens = [ |
|
31 | tokens = [ | |
32 | '*test', |
|
32 | '*test', | |
33 | '**test', |
|
33 | '**test', | |
34 | 'test', |
|
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', 'https://google.com/')] |
|
42 | ('test', 'https://google.com/')] | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | @dec.onlyif_cmds_exist('pandoc') |
|
45 | @dec.onlyif_cmds_exist('pandoc') | |
46 | def test_markdown2latex(self): |
|
46 | def test_markdown2latex(self): | |
47 | """markdown2latex test""" |
|
47 | """markdown2latex test""" | |
48 | for index, test in enumerate(self.tests): |
|
48 | for index, test in enumerate(self.tests): | |
49 | self._try_markdown(markdown2latex, test, self.tokens[index]) |
|
49 | self._try_markdown(markdown2latex, test, self.tokens[index]) | |
50 |
|
50 | |||
51 | @dec.onlyif_cmds_exist('pandoc') |
|
51 | @dec.onlyif_cmds_exist('pandoc') | |
52 | def test_pandoc_extra_args(self): |
|
52 | def test_pandoc_extra_args(self): | |
53 | # pass --no-wrap |
|
53 | # pass --no-wrap | |
54 | s = '\n'.join([ |
|
54 | s = '\n'.join([ | |
55 | "#latex {{long_line | md2l(['--no-wrap'])}}", |
|
55 | "#latex {{long_line | md2l(['--no-wrap'])}}", | |
56 | "#rst {{long_line | md2r(['--columns', '5'])}}", |
|
56 | "#rst {{long_line | md2r(['--columns', '5'])}}", | |
57 | ]) |
|
57 | ]) | |
58 | long_line = ' '.join(['long'] * 30) |
|
58 | long_line = ' '.join(['long'] * 30) | |
59 | env = Environment() |
|
59 | env = Environment() | |
60 | env.filters.update({ |
|
60 | env.filters.update({ | |
61 | 'md2l': markdown2latex, |
|
61 | 'md2l': markdown2latex, | |
62 | 'md2r': markdown2rst, |
|
62 | 'md2r': markdown2rst, | |
63 | }) |
|
63 | }) | |
64 | tpl = env.from_string(s) |
|
64 | tpl = env.from_string(s) | |
65 | rendered = tpl.render(long_line=long_line) |
|
65 | rendered = tpl.render(long_line=long_line) | |
66 | _, latex, rst = rendered.split('#') |
|
66 | _, latex, rst = rendered.split('#') | |
67 |
|
67 | |||
68 | self.assertEqual(latex.strip(), 'latex %s' % long_line) |
|
68 | self.assertEqual(latex.strip(), 'latex %s' % long_line) | |
69 | self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n')) |
|
69 | self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n')) | |
70 |
|
70 | |||
71 | def test_markdown2html(self): |
|
71 | def test_markdown2html(self): | |
72 | """markdown2html test""" |
|
72 | """markdown2html test""" | |
73 | for index, test in enumerate(self.tests): |
|
73 | for index, test in enumerate(self.tests): | |
74 | self._try_markdown(markdown2html, test, self.tokens[index]) |
|
74 | self._try_markdown(markdown2html, test, self.tokens[index]) | |
75 |
|
75 | |||
76 | def test_markdown2html_math(self): |
|
76 | def test_markdown2html_math(self): | |
77 | # Mathematical expressions should be passed through unaltered |
|
77 | # Mathematical expressions should be passed through unaltered | |
78 | cases = [("\\begin{equation*}\n" |
|
78 | cases = [("\\begin{equation*}\n" | |
79 | "\\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" |
|
79 | "\\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" | |
80 | "\\end{equation*}"), |
|
80 | "\\end{equation*}"), | |
81 | ("$$\n" |
|
81 | ("$$\n" | |
82 | "a = 1 *3* 5\n" |
|
82 | "a = 1 *3* 5\n" | |
83 | "$$"), |
|
83 | "$$"), | |
84 | "$ a = 1 *3* 5 $", |
|
84 | "$ a = 1 *3* 5 $", | |
85 | ] |
|
85 | ] | |
86 | for case in cases: |
|
86 | for case in cases: | |
87 | self.assertIn(case, markdown2html(case)) |
|
87 | self.assertIn(case, markdown2html(case)) | |
88 |
|
88 | |||
|
89 | def test_markdown2html_math_paragraph(self): | |||
|
90 | # https://github.com/ipython/ipython/issues/6724 | |||
|
91 | a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage, | |||
|
92 | $s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$. | |||
|
93 | With $s_0$ defined as the initial storage content in $t=1$, we have""" | |||
|
94 | self.assertIn(a, markdown2html(a)) | |||
89 |
|
95 | |||
90 | @dec.onlyif_cmds_exist('pandoc') |
|
96 | @dec.onlyif_cmds_exist('pandoc') | |
91 | def test_markdown2rst(self): |
|
97 | def test_markdown2rst(self): | |
92 | """markdown2rst test""" |
|
98 | """markdown2rst test""" | |
93 |
|
99 | |||
94 | #Modify token array for rst, escape asterik |
|
100 | #Modify token array for rst, escape asterik | |
95 | tokens = copy(self.tokens) |
|
101 | tokens = copy(self.tokens) | |
96 | tokens[0] = r'\*test' |
|
102 | tokens[0] = r'\*test' | |
97 | tokens[1] = r'\*\*test' |
|
103 | tokens[1] = r'\*\*test' | |
98 |
|
104 | |||
99 | for index, test in enumerate(self.tests): |
|
105 | for index, test in enumerate(self.tests): | |
100 | self._try_markdown(markdown2rst, test, tokens[index]) |
|
106 | self._try_markdown(markdown2rst, test, tokens[index]) | |
101 |
|
107 | |||
102 |
|
108 | |||
103 | def _try_markdown(self, method, test, tokens): |
|
109 | def _try_markdown(self, method, test, tokens): | |
104 | results = method(test) |
|
110 | results = method(test) | |
105 | if isinstance(tokens, string_types): |
|
111 | if isinstance(tokens, string_types): | |
106 | assert tokens in results |
|
112 | assert tokens in results | |
107 | else: |
|
113 | else: | |
108 | for token in tokens: |
|
114 | for token in tokens: | |
109 | assert token in results |
|
115 | assert token in results |
General Comments 0
You need to be logged in to leave comments.
Login now