# coding: utf-8 """Tests for conversions from markdown to other formats""" # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. import re from copy import copy from IPython.utils.py3compat import string_types from IPython.testing import decorators as dec from ...tests.base import TestsBase from ..markdown import markdown2latex, markdown2html, markdown2rst from jinja2 import Environment class TestMarkdown(TestsBase): tests = [ '*test', '**test', '*test*', '_test_', '__test__', '__*test*__', '**test**', '#test', '##test', 'test\n----', 'test [link](https://google.com/)', ] tokens = [ '*test', '**test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', 'test', ('test', 'https://google.com/'), ] @dec.onlyif_cmds_exist('pandoc') def test_markdown2latex(self): """markdown2latex test""" for index, test in enumerate(self.tests): self._try_markdown(markdown2latex, test, self.tokens[index]) @dec.onlyif_cmds_exist('pandoc') 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 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( markdown2latex(s, markup='markdown_strict+tex_math_dollars'), expected) @dec.onlyif_cmds_exist('pandoc') def test_pandoc_extra_args(self): # pass --no-wrap s = '\n'.join([ "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}", "#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('#') self.assertEqual(latex.strip(), 'latex %s' % long_line) self.assertEqual(rst.strip(), 'rst %s' % long_line.replace(' ', '\n')) def test_markdown2html(self): """markdown2html test""" for index, test in enumerate(self.tests): self._try_markdown(markdown2html, test, self.tokens[index]) def test_markdown2html_heading_anchors(self): for md, tokens in [ ('# test', ('