##// END OF EJS Templates
Backport PR #4178: add missing data_javascript...
Backport PR #4178: add missing data_javascript Note that javascript is still excluded from html output by default, but this makes it possible to include javascript output in custom templates. candidate for backport to 1.1

File last commit:

r11703:be2440eb
r12465:b2940d33
Show More
pandoc.py
69 lines | 2.2 KiB | text/x-python | PythonLexer
MinRK
add nbconvert.utils.pandoc...
r11267 """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
# IPython imports
from IPython.utils.py3compat import cast_bytes
David Wolever
Catch ConversionExceptions in main nbconvert loop
r11703 from .exceptions import ConversionException
MinRK
add nbconvert.utils.pandoc...
r11267 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
David Wolever
Catch ConversionExceptions in main nbconvert loop
r11703 class PandocMissing(ConversionException):
"""Exception raised when Pandoc is missing. """
David Wolever
Raise a named exception when pandoc is missing...
r11702 pass
MinRK
allow extra pandoc args
r11293 def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
MinRK
add nbconvert.utils.pandoc...
r11267 """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.
"""
MinRK
allow extra pandoc args
r11293 command = ['pandoc', '-f', fmt, '-t', to]
if extra_args:
MinRK
command.extend in pandoc
r11430 command.extend(extra_args)
Paul Ivanov
Nicer message when pandoc is missing, closes #3730
r11626 try:
p = subprocess.Popen(command,
David Wolever
Raise a named exception when pandoc is missing...
r11702 stdin=subprocess.PIPE, stdout=subprocess.PIPE
)
except OSError as e:
raise PandocMissing(
David Wolever
Catch ConversionExceptions in main nbconvert loop
r11703 "The command '%s' returned an error: %s.\n" %(" ".join(command), e) +
David Wolever
Raise a named exception when pandoc is missing...
r11702 "Please check that pandoc is installed:\n" +
"http://johnmacfarlane.net/pandoc/installing.html"
Paul Ivanov
Nicer message when pandoc is missing, closes #3730
r11626 )
MinRK
add nbconvert.utils.pandoc...
r11267 out, _ = p.communicate(cast_bytes(source, encoding))
out = out.decode(encoding, 'replace')
return out[:-1]