Skip to content

Commit

Permalink
Merge pull request ipython#4205 from minrk/universal-newline-pandoc
Browse files Browse the repository at this point in the history
use TextIOWrapper when communicating with pandoc subprocess
  • Loading branch information
takluyver committed Sep 14, 2013
2 parents 361a9e9 + 5318b27 commit 69ed165
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 8 additions & 0 deletions IPython/nbconvert/tests/files/notebook2.ipynb
Expand Up @@ -123,6 +123,14 @@
],
"prompt_number": 10
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Here is a very long heading that pandoc will wrap and wrap and wrap and wrap and wrap and wrap and wrap and wrap and wrap and wrap and wrap and wrap"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
15 changes: 14 additions & 1 deletion IPython/nbconvert/tests/test_nbconvertapp.py
Expand Up @@ -103,6 +103,20 @@ def test_post_processor(self):
assert os.path.isfile('notebook1.tex')
assert os.path.isfile('notebook1.pdf')

@dec.onlyif_cmds_exist('pandoc')
def test_spurious_cr(self):
"""Check for extra CR characters"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.call('nbconvert --log-level 0 --to latex notebook2')
assert os.path.isfile('notebook2.tex')
with open('notebook2.tex') as f:
tex = f.read()
self.call('nbconvert --log-level 0 --to html notebook2')
assert os.path.isfile('notebook2.html')
with open('notebook2.html') as f:
html = f.read()
self.assertEqual(tex.count('\r'), tex.count('\r\n'))
self.assertEqual(html.count('\r'), html.count('\r\n'))

@dec.onlyif_cmds_exist('pandoc')
def test_png_base64_html_ok(self):
Expand All @@ -114,7 +128,6 @@ def test_png_base64_html_ok(self):
with open('notebook2.html') as f:
assert "data:image/png;base64,b'" not in f.read()


@dec.onlyif_cmds_exist('pandoc')
def test_template(self):
"""
Expand Down
5 changes: 3 additions & 2 deletions IPython/nbconvert/utils/pandoc.py
Expand Up @@ -15,6 +15,7 @@

# Stdlib imports
import subprocess
from io import TextIOWrapper, BytesIO

# IPython imports
from IPython.utils.py3compat import cast_bytes
Expand Down Expand Up @@ -64,6 +65,6 @@ def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
"http://johnmacfarlane.net/pandoc/installing.html"
)
out, _ = p.communicate(cast_bytes(source, encoding))
out = out.decode(encoding, 'replace')
return out[:-1]
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')

0 comments on commit 69ed165

Please sign in to comment.