diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py index e7e0154..6c3b78c 100755 --- a/IPython/nbconvert/filters/markdown.py +++ b/IPython/nbconvert/filters/markdown.py @@ -1,18 +1,11 @@ """Markdown filters + This file contains a collection of utility filters for dealing with markdown within Jinja templates. """ -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. -# +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- from __future__ import print_function # Stdlib imports @@ -34,9 +27,7 @@ from IPython.utils.process import get_output_error_code from IPython.utils.py3compat import cast_bytes from IPython.utils.version import check_version -#----------------------------------------------------------------------------- -# Functions -#----------------------------------------------------------------------------- + marked = os.path.join(os.path.dirname(__file__), "marked.js") _node = None @@ -72,11 +63,12 @@ def markdown2latex(source): return pandoc(source, 'markdown', 'latex') class MathBlockGrammar(mistune.BlockGrammar): - block_math = re.compile("^\$\$(.*?)\$\$") - block_math2 = re.compile(r"^\\begin(.*?)\\end") + block_math = re.compile("^\$\$(.*?)\$\$", re.DOTALL) + latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}", + re.DOTALL) class MathBlockLexer(mistune.BlockLexer): - default_features = ['block_math', 'block_math2'] + mistune.BlockLexer.default_features + default_features = ['block_math', 'latex_environment'] + mistune.BlockLexer.default_features def __init__(self, rules=None, **kwargs): if rules is None: @@ -90,7 +82,12 @@ class MathBlockLexer(mistune.BlockLexer): 'text': m.group(1) }) - parse_block_math2 = parse_block_math + def parse_latex_environment(self, m): + self.tokens.append({ + 'type': 'latex_environment', + 'name': m.group(1), + 'text': m.group(2) + }) class MathInlineGrammar(mistune.InlineGrammar): math = re.compile("^\$(.+?)\$") @@ -104,7 +101,7 @@ class MathInlineLexer(mistune.InlineLexer): super(MathInlineLexer, self).__init__(renderer, rules, **kwargs) def output_math(self, m): - self.renderer.inline_math(m.group(1)) + return self.renderer.inline_math(m.group(1)) class MarkdownWithMath(mistune.Markdown): def __init__(self, renderer, **kwargs): @@ -117,6 +114,9 @@ class MarkdownWithMath(mistune.Markdown): def parse_block_math(self): return self.renderer.block_math(self.token['text']) + def parse_latex_environment(self): + return self.renderer.latex_environment(self.token['name'], self.token['text']) + class IPythonRenderer(mistune.Renderer): def block_code(self, code, lang): if lang: @@ -137,6 +137,9 @@ class IPythonRenderer(mistune.Renderer): def block_math(self, text): return '$$%s$$' % text + def latex_environment(self, name, text): + return r'\begin{%s}%s\end{%s}' % (name, text, name) + def inline_math(self, text): return '$%s$' % text diff --git a/IPython/nbconvert/filters/tests/test_markdown.py b/IPython/nbconvert/filters/tests/test_markdown.py index 0e4f5c7..991caef 100644 --- a/IPython/nbconvert/filters/tests/test_markdown.py +++ b/IPython/nbconvert/filters/tests/test_markdown.py @@ -1,19 +1,7 @@ +"""Tests for conversions from markdown to other formats""" -""" -Module with tests for Markdown -""" - -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. -# +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- from copy import copy @@ -24,10 +12,6 @@ from ...tests.base import TestsBase from ..markdown import markdown2latex, markdown2html, markdown2rst -#----------------------------------------------------------------------------- -# Class -#----------------------------------------------------------------------------- - class TestMarkdown(TestsBase): tests = [ @@ -68,6 +52,19 @@ class TestMarkdown(TestsBase): for index, test in enumerate(self.tests): self._try_markdown(markdown2html, test, self.tokens[index]) + 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)) + @dec.onlyif_cmds_exist('pandoc') def test_markdown2rst(self):