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