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