From 3ccc8dfdd48ebdc1bb66a7733cc35d0eccc9bc7d 2014-11-01 21:59:44 From: Min RK Date: 2014-11-01 21:59:44 Subject: [PATCH] Merge pull request #6588 from benjaminabel/master Handle numbers in heading cells when converting to Latex --- diff --git a/.travis.yml b/.travis.yml index 836cee1..0ed6dee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,8 @@ before_install: - sudo rm -rf /dev/shm && sudo ln -s /run/shm /dev/shm # Pierre Carrier's PPA for PhantomJS and CasperJS - time sudo add-apt-repository -y ppa:pcarrier/ppa + # Needed to get recent version of pandoc in ubntu 12.04 + - time sudo add-apt-repository -y ppa:marutter/c2d4u - time sudo apt-get update - time sudo apt-get install pandoc casperjs libzmq3-dev # pin tornado < 4 for js tests while phantom is on super old webkit diff --git a/IPython/nbconvert/exporters/templateexporter.py b/IPython/nbconvert/exporters/templateexporter.py index 9ca1f65..53bf36e 100644 --- a/IPython/nbconvert/exporters/templateexporter.py +++ b/IPython/nbconvert/exporters/templateexporter.py @@ -62,6 +62,7 @@ default_filters = { 'path2url': filters.path2url, 'add_prompts': filters.add_prompts, 'ascii_only': filters.ascii_only, + 'prevent_list_blocks': filters.prevent_list_blocks, } #----------------------------------------------------------------------------- diff --git a/IPython/nbconvert/filters/markdown.py b/IPython/nbconvert/filters/markdown.py index 09058de..10e57b0 100755 --- a/IPython/nbconvert/filters/markdown.py +++ b/IPython/nbconvert/filters/markdown.py @@ -45,7 +45,7 @@ class NodeJSMissing(ConversionException): """Exception raised when node.js is missing.""" pass -def markdown2latex(source, extra_args=None): +def markdown2latex(source, markup='markdown', extra_args=None): """Convert a markdown string to LaTeX via pandoc. This function will raise an error if pandoc is not installed. @@ -55,13 +55,17 @@ def markdown2latex(source, extra_args=None): ---------- source : string Input string, assumed to be valid markdown. + markup : string + Markup used by pandoc's reader + default : pandoc extended markdown + (see http://johnmacfarlane.net/pandoc/README.html#pandocs-markdown) Returns ------- out : string Output as returned by pandoc. """ - return pandoc(source, 'markdown', 'latex', extra_args=extra_args) + return pandoc(source, markup, 'latex', extra_args=extra_args) @undoc diff --git a/IPython/nbconvert/filters/strings.py b/IPython/nbconvert/filters/strings.py index c0223a2..8b9a7bd 100755 --- a/IPython/nbconvert/filters/strings.py +++ b/IPython/nbconvert/filters/strings.py @@ -45,6 +45,7 @@ __all__ = [ 'path2url', 'add_prompts', 'ascii_only', + 'prevent_list_blocks', ] @@ -218,4 +219,14 @@ def path2url(path): def ascii_only(s): """ensure a string is ascii""" s = py3compat.cast_unicode(s) - return s.encode('ascii', 'replace').decode('ascii') \ No newline at end of file + return s.encode('ascii', 'replace').decode('ascii') + +def prevent_list_blocks(s): + """ + Prevent presence of enumerate or itemize blocks in latex headings cells + """ + out = re.sub('(^\s*\d*)\.', '\\1\.', s) + out = re.sub('(^\s*)\-', '\\1\-', out) + out = re.sub('(^\s*)\+', '\\1\+', out) + out = re.sub('(^\s*)\*', '\\1\*', out) + return out diff --git a/IPython/nbconvert/filters/tests/test_markdown.py b/IPython/nbconvert/filters/tests/test_markdown.py index 4a8a609..d2eb698 100644 --- a/IPython/nbconvert/filters/tests/test_markdown.py +++ b/IPython/nbconvert/filters/tests/test_markdown.py @@ -49,10 +49,24 @@ class TestMarkdown(TestsBase): 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 = '$\\alpha$ latex math' + self.assertEqual( + markdown2latex(s, markup='markdown_strict+tex_math_dollars'), + s) + + @dec.onlyif_cmds_exist('pandoc') def test_pandoc_extra_args(self): # pass --no-wrap s = '\n'.join([ - "#latex {{long_line | md2l(['--no-wrap'])}}", + "#latex {{long_line | md2l('markdown', ['--no-wrap'])}}", "#rst {{long_line | md2r(['--columns', '5'])}}", ]) long_line = ' '.join(['long'] * 30) @@ -64,7 +78,7 @@ class TestMarkdown(TestsBase): 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')) diff --git a/IPython/nbconvert/filters/tests/test_strings.py b/IPython/nbconvert/filters/tests/test_strings.py index 7b24590..0340cbb 100644 --- a/IPython/nbconvert/filters/tests/test_strings.py +++ b/IPython/nbconvert/filters/tests/test_strings.py @@ -18,7 +18,7 @@ import os from ...tests.base import TestsBase from ..strings import (wrap_text, html2text, add_anchor, strip_dollars, strip_files_prefix, get_lines, comment_lines, ipython2python, posix_path, - add_prompts + add_prompts, prevent_list_blocks ) @@ -151,3 +151,15 @@ class TestStrings(TestsBase): text1 = """for i in range(10):\n i += 1\n print i""" text2 = """>>> for i in range(10):\n... i += 1\n... print i""" self.assertEqual(text2, add_prompts(text1)) + + def test_prevent_list_blocks(self): + """prevent_list_blocks test""" + tests = [ + ('1. arabic point', '1\\. arabic point'), + ('* bullet asterisk', '\\* bullet asterisk'), + ('+ bullet Plus Sign', '\\+ bullet Plus Sign'), + ('- bullet Hyphen-Minus', '\\- bullet Hyphen-Minus'), + (' 1. spaces + arabic point', ' 1\\. spaces + arabic point'), + ] + for test in tests: + self.assertEqual(prevent_list_blocks(test[0]), test[1]) diff --git a/IPython/nbconvert/templates/latex/base.tplx b/IPython/nbconvert/templates/latex/base.tplx index 204de96..3516749 100644 --- a/IPython/nbconvert/templates/latex/base.tplx +++ b/IPython/nbconvert/templates/latex/base.tplx @@ -201,7 +201,8 @@ This template does not define a docclass, the inheriting class must define this. ((* elif cell.level == 6 -*)) ((* block h6 -*))\\*\textit((* endblock h6 -*)) ((*- endif -*)) - {((( cell.source | replace('\n', ' ') | citation2latex | strip_files_prefix | markdown2latex )))} + {((( cell.source | replace('\n', ' ') | citation2latex | strip_files_prefix | prevent_list_blocks | markdown2latex(markup='markdown_strict+tex_math_dollars') )))} + ((* endblock headingcell *))