##// END OF EJS Templates
Backport PR #4849: Various unicode fixes (mostly on Windows)...
Backport PR #4849: Various unicode fixes (mostly on Windows) There were warnings on startup of IPython, and the qtconsole and notebook could not be started at all if the user's home directory is unicode. Changes: - add `prefer_stream=True` flag to utils.encoding.getdefaultencoding, since the right answer is different for stream output and other things. - always encode args to Popen on Windows Python 2 - never add unicode to sys.path on Python 2 These should be backported for 1.2, and it would be great if some unicode-locale Windows users (@jstenar) could double check the sanity of the changes.

File last commit:

r13955:5fe35e64
r14916:7cbbbbc4
Show More
pandoc.py
70 lines | 2.3 KiB | text/x-python | PythonLexer
"""Utility for calling pandoc"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013 the 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
import subprocess
from io import TextIOWrapper, BytesIO
# IPython imports
from IPython.utils.py3compat import cast_bytes
from .exceptions import ConversionException
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class PandocMissing(ConversionException):
"""Exception raised when Pandoc is missing. """
pass
def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
"""Convert an input string in format `from` to format `to` via pandoc.
This function will raise an error if pandoc is not installed.
Any error messages generated by pandoc are printed to stderr.
Parameters
----------
source : string
Input string, assumed to be valid format `from`.
fmt : string
The name of the input format (markdown, etc.)
to : string
The name of the output format (html, etc.)
Returns
-------
out : unicode
Output as returned by pandoc.
"""
command = ['pandoc', '-f', fmt, '-t', to]
if extra_args:
command.extend(extra_args)
try:
p = subprocess.Popen(command,
stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
except OSError as e:
raise PandocMissing(
"The command '%s' returned an error: %s.\n" %(" ".join(command), e) +
"Please check that pandoc is installed:\n" +
"http://johnmacfarlane.net/pandoc/installing.html"
)
out, _ = p.communicate(cast_bytes(source, encoding))
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')