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