pandoc.py
91 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
MinRK
|
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 | ||||
Daniel B. Vasquez
|
r14759 | import re | ||
import warnings | ||||
MinRK
|
r12523 | from io import TextIOWrapper, BytesIO | ||
MinRK
|
r11267 | |||
# IPython imports | ||||
from IPython.utils.py3compat import cast_bytes | ||||
David Wolever
|
r11703 | from .exceptions import ConversionException | ||
MinRK
|
r11267 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
David Wolever
|
r11703 | class PandocMissing(ConversionException): | ||
"""Exception raised when Pandoc is missing. """ | ||||
David Wolever
|
r11702 | pass | ||
Daniel B. Vasquez
|
r14759 | minimal_version = "1.12.1" | ||
David Wolever
|
r11702 | |||
MinRK
|
r11293 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): | ||
MinRK
|
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
|
r11293 | command = ['pandoc', '-f', fmt, '-t', to] | ||
if extra_args: | ||||
MinRK
|
r11430 | command.extend(extra_args) | ||
Paul Ivanov
|
r11626 | try: | ||
p = subprocess.Popen(command, | ||||
David Wolever
|
r11702 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | ||
) | ||||
except OSError as e: | ||||
raise PandocMissing( | ||||
David Wolever
|
r11703 | "The command '%s' returned an error: %s.\n" %(" ".join(command), e) + | ||
David Wolever
|
r11702 | "Please check that pandoc is installed:\n" + | ||
"http://johnmacfarlane.net/pandoc/installing.html" | ||||
Paul Ivanov
|
r11626 | ) | ||
MinRK
|
r11267 | out, _ = p.communicate(cast_bytes(source, encoding)) | ||
MinRK
|
r12523 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() | ||
return out.rstrip('\n') | ||||
MinRK
|
r11267 | |||
Daniel B. Vasquez
|
r14759 | |||
pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') | ||||
def get_pandoc_version(): | ||||
out = subprocess.check_output(['pandoc', '--version'], universal_newlines=True) | ||||
return pv_re.search(out).group(0) | ||||
pandoc.version = get_pandoc_version() | ||||
def check_pandoc_version(): | ||||
return pandoc.version >= minimal_version | ||||
if(not check_pandoc_version()): | ||||
warnings.warn( "You are using an old version of pandoc (%s)\n"%pandoc.version + | ||||
"Recommended version is %s.\nTry updating."%minimal_version + | ||||
"http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", | ||||
RuntimeWarning, | ||||
stacklevel=2) | ||||