##// END OF EJS Templates
adding tests from jupyter/nbviewer#420
Nicholas Bollweg -
Show More
@@ -1,152 +1,176 b''
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'&#182;</a>', "anchor-link")
106 106 ),
107 107 ('###test head space',
108 108 ('<h3', '>test head space', 'id="test-head-space"', u'&#182;</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 126 def test_markdown2html_math_paragraph(self):
127 # https://github.com/ipython/ipython/issues/6724
128 a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
127 """these should all parse without modification"""
128 patterns = [
129 # https://github.com/ipython/ipython/issues/6724
130 """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage,
129 131 $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"""
131 self.assertIn(a, markdown2html(a))
132 With $s_0$ defined as the initial storage content in $t=1$, we have""",
133 # https://github.com/jupyter/nbviewer/issues/420
134 """$C_{ik}$
135 $$
136 C_{ik} = \sum_{j=1}
137 $$
138 $C_{ik}$""",
139 """$m$
140 $$
141 C = \begin{pmatrix}
142 0 & 0 & 0 & \cdots & 0 & 0 & -c_0 \\
143 0 & 0 & 0 & \cdots & 0 & 1 & -c_{m-1}
144 \end{pmatrix}
145 $$
146 $x^m$""",
147 """$r=\overline{1,n}$
148 $$ {\bf
149 b}_{i}^{r}(t)=(1-t)\,{\bf b}_{i}^{r-1}(t)+t\,{\bf b}_{i+1}^{r-1}(t),\:
150 i=\overline{0,n-r}, $$
151 i.e. the $i^{th}$"""
152 ]
153
154 for pattern in patterns:
155 self.assertIn(pattern, markdown2html(pattern))
132 156
133 157 @dec.onlyif_cmds_exist('pandoc')
134 158 def test_markdown2rst(self):
135 159 """markdown2rst test"""
136 160
137 161 #Modify token array for rst, escape asterik
138 162 tokens = copy(self.tokens)
139 163 tokens[0] = r'\*test'
140 164 tokens[1] = r'\*\*test'
141 165
142 166 for index, test in enumerate(self.tests):
143 167 self._try_markdown(markdown2rst, test, tokens[index])
144 168
145 169
146 170 def _try_markdown(self, method, test, tokens):
147 171 results = method(test)
148 172 if isinstance(tokens, string_types):
149 173 assert tokens in results
150 174 else:
151 175 for token in tokens:
152 176 assert token in results
General Comments 0
You need to be logged in to leave comments. Login now