##// END OF EJS Templates
cast unicode to allow json dump
cast unicode to allow json dump

File last commit:

r17550:713053c4
r18039:462e3bd5
Show More
pandoc.py
141 lines | 4.7 KiB | text/x-python | PythonLexer
MinRK
add nbconvert.utils.pandoc...
r11267 """Utility for calling pandoc"""
#-----------------------------------------------------------------------------
Jonathan Frederic
A couple more small changes,...
r14827 # Copyright (c) 2014 the IPython Development Team.
MinRK
add nbconvert.utils.pandoc...
r11267 #
# 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
Daniel B. Vasquez
There is a bug in pandoc < 1.12.1 where the --no-highlight option is not honored...
r14759 import warnings
cel
Revised pandoc version detection.
r17549 import re
MinRK
use TextIOWrapper when communicating with pandoc subprocess...
r12523 from io import TextIOWrapper, BytesIO
MinRK
add nbconvert.utils.pandoc...
r11267
# IPython imports
from IPython.utils.py3compat import cast_bytes
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 from IPython.utils.version import check_version
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 from IPython.utils.process import is_cmd_found, FindCmdError
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763
David Wolever
Catch ConversionExceptions in main nbconvert loop
r11703 from .exceptions import ConversionException
MinRK
add nbconvert.utils.pandoc...
r11267 #-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 _minimal_version = "1.12.1"
David Wolever
Raise a named exception when pandoc is missing...
r11702
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.
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.
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Thomas Kluyver
Fix some doc formatting in pandoc checks.
r14913 Raises
------
PandocMissing
If pandoc is not installed.
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766 Any error messages generated by pandoc are printed to stderr.
MinRK
add nbconvert.utils.pandoc...
r11267 """
Daniel B. Vasquez
Adding checks to nbconvert.utils.pandoc module to ensure Pandoc is present...
r14760 cmd = ['pandoc', '-f', fmt, '-t', to]
MinRK
allow extra pandoc args
r11293 if extra_args:
Daniel B. Vasquez
Adding checks to nbconvert.utils.pandoc module to ensure Pandoc is present...
r14760 cmd.extend(extra_args)
Daniel B. Vasquez
move the pandoc_available(...) call inside get_pandoc_version(...) as it also tested for availability
r14765 # this will raise an exception that will pop us out of here
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 check_pandoc_version()
Daniel B. Vasquez
Adding checks to nbconvert.utils.pandoc module to ensure Pandoc is present...
r14760
# we can safely continue
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
MinRK
add nbconvert.utils.pandoc...
r11267 out, _ = p.communicate(cast_bytes(source, encoding))
MinRK
use TextIOWrapper when communicating with pandoc subprocess...
r12523 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')
MinRK
add nbconvert.utils.pandoc...
r11267
Daniel B. Vasquez
There is a bug in pandoc < 1.12.1 where the --no-highlight option is not honored...
r14759
def get_pandoc_version():
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 """Gets the Pandoc version if Pandoc is installed.
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value.
If the minimal version is met, it will return the cached version and stop probing Pandoc
Thomas Kluyver
More minor doc fixes.
r14918 (unless :func:`clean_cache()` is called).
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Thomas Kluyver
Fix some doc formatting in pandoc checks.
r14913 Raises
------
PandocMissing
If pandoc is unavailable.
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 """
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 global __version
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Jonathan Frederic
A couple more small changes,...
r14827 if __version is None:
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 if not is_cmd_found('pandoc'):
raise PandocMissing()
cel
Fixed pandoc version detection.
r17468 out = subprocess.check_output(['pandoc', '-v'],
universal_newlines=True)
out_lines = out.splitlines()
cel
Revised pandoc version detection.
r17549 version_pattern = re.compile(r"^\d+(\.\d+){1,}$")
for tok in out_lines[0].split():
if version_pattern.match(tok):
__version = tok
break
Jonathan Frederic
A couple more small changes,...
r14827 return __version
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Daniel B. Vasquez
There is a bug in pandoc < 1.12.1 where the --no-highlight option is not honored...
r14759
def check_pandoc_version():
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 """Returns True if minimal pandoc version is met.
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Thomas Kluyver
Fix some doc formatting in pandoc checks.
r14913 Raises
------
PandocMissing
If pandoc is unavailable.
Daniel B. Vasquez
pandoc version is now checked at each call to pandoc.pandoc(...). Version is tested with IPython.utils.version.version_check
r14763 """
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 v = get_pandoc_version()
cel
Revised pandoc version detection.
r17549 if v is None:
warnings.warn("Sorry, we cannot determine the version of pandoc.\n"
"Please consider reporting this issue and include the"
"output of pandoc --version.\nContinuing...",
RuntimeWarning, stacklevel=2)
cel
Fixed indentation error.
r17550 return False
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 ok = check_version(v , _minimal_version )
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766 if not ok:
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 warnings.warn( "You are using an old version of pandoc (%s)\n" % v +
"Recommended version is %s.\nTry updating." % _minimal_version +
"http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...",
RuntimeWarning, stacklevel=2)
return ok
Daniel B. Vasquez
nbconvert.utils.pandoc:...
r14766
Daniel B. Vasquez
code shuffling to present the API functions first, and the innards at the end of the file
r14767 #-----------------------------------------------------------------------------
# Exception handling
#-----------------------------------------------------------------------------
class PandocMissing(ConversionException):
"""Exception raised when Pandoc is missing. """
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 def __init__(self, *args, **kwargs):
super(PandocMissing, self).__init__( "Pandoc wasn't found.\n" +
Daniel B. Vasquez
code shuffling to present the API functions first, and the innards at the end of the file
r14767 "Please check that pandoc is installed:\n" +
"http://johnmacfarlane.net/pandoc/installing.html" )
#-----------------------------------------------------------------------------
# Internal state management
#-----------------------------------------------------------------------------
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 def clean_cache():
global __version
__version = None
Daniel B. Vasquez
code shuffling to present the API functions first, and the innards at the end of the file
r14767
Daniel B. Vasquez
big cleanup of nbconvert.utils.pandoc module. Remove useless functions, less cached values.
r14768 __version = None