test_markdown.py
152 lines
| 4.9 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18596 | # coding: utf-8 | ||
Thomas Kluyver
|
r17259 | """Tests for conversions from markdown to other formats""" | ||
Jonathan Frederic
|
r11902 | |||
Thomas Kluyver
|
r17259 | # Copyright (c) IPython Development Team. | ||
Jonathan Frederic
|
r11902 | # Distributed under the terms of the Modified BSD License. | ||
Min RK
|
r18698 | import re | ||
Jonathan Frederic
|
r11920 | from copy import copy | ||
Jonathan Frederic
|
r11902 | |||
Jonathan Frederic
|
r11917 | from IPython.utils.py3compat import string_types | ||
Jonathan Frederic
|
r11935 | from IPython.testing import decorators as dec | ||
Jonathan Frederic
|
r11902 | |||
from ...tests.base import TestsBase | ||||
Jonathan Frederic
|
r11928 | from ..markdown import markdown2latex, markdown2html, markdown2rst | ||
Jonathan Frederic
|
r11902 | |||
MinRK
|
r17885 | from jinja2 import Environment | ||
Jonathan Frederic
|
r11902 | |||
class TestMarkdown(TestsBase): | ||||
tests = [ | ||||
'*test', | ||||
'**test', | ||||
'*test*', | ||||
'_test_', | ||||
'__test__', | ||||
'__*test*__', | ||||
'**test**', | ||||
'#test', | ||||
'##test', | ||||
'test\n----', | ||||
MinRK
|
r18596 | 'test [link](https://google.com/)', | ||
] | ||||
Jonathan Frederic
|
r11902 | |||
Jonathan Frederic
|
r11917 | tokens = [ | ||
'*test', | ||||
'**test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
'test', | ||||
MinRK
|
r18596 | ('test', 'https://google.com/'), | ||
] | ||||
Jonathan Frederic
|
r11917 | |||
Jonathan Frederic
|
r11902 | |||
Jonathan Frederic
|
r11935 | @dec.onlyif_cmds_exist('pandoc') | ||
Jonathan Frederic
|
r11902 | def test_markdown2latex(self): | ||
Jonathan Frederic
|
r11934 | """markdown2latex test""" | ||
Jonathan Frederic
|
r11902 | for index, test in enumerate(self.tests): | ||
Thomas Kluyver
|
r12373 | self._try_markdown(markdown2latex, test, self.tokens[index]) | ||
Jonathan Frederic
|
r11902 | |||
MinRK
|
r17885 | @dec.onlyif_cmds_exist('pandoc') | ||
Benjamin ABEL
|
r18427 | def test_markdown2latex_markup(self): | ||
"""markdown2latex with markup kwarg test""" | ||||
# This string should be passed through unaltered with pandoc's | ||||
# markdown_strict reader | ||||
s = '1) arabic number with parenthesis' | ||||
self.assertEqual(markdown2latex(s, markup='markdown_strict'), s) | ||||
# This string should be passed through unaltered with pandoc's | ||||
# markdown_strict+tex_math_dollars reader | ||||
Min RK
|
r18698 | s = r'$\alpha$ latex math' | ||
# sometimes pandoc uses $math$, sometimes it uses \(math\) | ||||
expected = re.compile(r'(\$|\\\()\\alpha(\$|\\\)) latex math') | ||||
try: | ||||
# py3 | ||||
assertRegex = self.assertRegex | ||||
except AttributeError: | ||||
# py2 | ||||
assertRegex = self.assertRegexpMatches | ||||
assertRegex( | ||||
Benjamin ABEL
|
r18427 | markdown2latex(s, markup='markdown_strict+tex_math_dollars'), | ||
Min RK
|
r18698 | expected) | ||
Benjamin ABEL
|
r18427 | |||
@dec.onlyif_cmds_exist('pandoc') | ||||
MinRK
|
r17885 | def test_pandoc_extra_args(self): | ||
# pass --no-wrap | ||||
s = '\n'.join([ | ||||
Benjamin ABEL
|
r18427 | "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}", | ||
MinRK
|
r17885 | "#rst {{long_line | md2r(['--columns', '5'])}}", | ||
]) | ||||
long_line = ' '.join(['long'] * 30) | ||||
env = Environment() | ||||
env.filters.update({ | ||||
'md2l': markdown2latex, | ||||
'md2r': markdown2rst, | ||||
}) | ||||
tpl = env.from_string(s) | ||||
rendered = tpl.render(long_line=long_line) | ||||
_, latex, rst = rendered.split('#') | ||||
Benjamin ABEL
|
r18427 | |||
MinRK
|
r17885 | self.assertEqual(latex.strip(), 'latex %s' % long_line) | ||
self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n')) | ||||
Jonathan Frederic
|
r11902 | def test_markdown2html(self): | ||
Jonathan Frederic
|
r11934 | """markdown2html test""" | ||
Jonathan Frederic
|
r11902 | for index, test in enumerate(self.tests): | ||
Thomas Kluyver
|
r12373 | self._try_markdown(markdown2html, test, self.tokens[index]) | ||
Jonathan Frederic
|
r11902 | |||
MinRK
|
r18596 | def test_markdown2html_heading_anchors(self): | ||
for md, tokens in [ | ||||
('# test', | ||||
('<h1', '>test', 'id="test"', u'¶</a>', "anchor-link") | ||||
), | ||||
('###test head space', | ||||
('<h3', '>test head space', 'id="test-head-space"', u'¶</a>', "anchor-link") | ||||
) | ||||
]: | ||||
self._try_markdown(markdown2html, md, tokens) | ||||
Thomas Kluyver
|
r17259 | def test_markdown2html_math(self): | ||
# Mathematical expressions should be passed through unaltered | ||||
cases = [("\\begin{equation*}\n" | ||||
"\\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" | ||||
"\\end{equation*}"), | ||||
("$$\n" | ||||
"a = 1 *3* 5\n" | ||||
"$$"), | ||||
"$ a = 1 *3* 5 $", | ||||
] | ||||
for case in cases: | ||||
self.assertIn(case, markdown2html(case)) | ||||
Thomas Kluyver
|
r18395 | def test_markdown2html_math_paragraph(self): | ||
# https://github.com/ipython/ipython/issues/6724 | ||||
a = """Water that is stored in $t$, $s_t$, must equal the storage content of the previous stage, | ||||
$s_{t-1}$, plus a stochastic inflow, $I_t$, minus what is being released in $t$, $r_t$. | ||||
With $s_0$ defined as the initial storage content in $t=1$, we have""" | ||||
self.assertIn(a, markdown2html(a)) | ||||
Jonathan Frederic
|
r11902 | |||
Jonathan Frederic
|
r11935 | @dec.onlyif_cmds_exist('pandoc') | ||
Jonathan Frederic
|
r11902 | def test_markdown2rst(self): | ||
Jonathan Frederic
|
r11934 | """markdown2rst test""" | ||
Jonathan Frederic
|
r11920 | |||
#Modify token array for rst, escape asterik | ||||
tokens = copy(self.tokens) | ||||
tokens[0] = r'\*test' | ||||
tokens[1] = r'\*\*test' | ||||
Jonathan Frederic
|
r11902 | for index, test in enumerate(self.tests): | ||
Thomas Kluyver
|
r12373 | self._try_markdown(markdown2rst, test, tokens[index]) | ||
Jonathan Frederic
|
r11902 | |||
Jonathan Frederic
|
r11917 | def _try_markdown(self, method, test, tokens): | ||
results = method(test) | ||||
if isinstance(tokens, string_types): | ||||
assert tokens in results | ||||
else: | ||||
for token in tokens: | ||||
assert token in results | ||||