diff --git a/IPython/nbconvert/utils/pandoc.py b/IPython/nbconvert/utils/pandoc.py index b1dd7bc..f78453c 100644 --- a/IPython/nbconvert/utils/pandoc.py +++ b/IPython/nbconvert/utils/pandoc.py @@ -21,6 +21,8 @@ from io import TextIOWrapper, BytesIO # IPython imports from IPython.utils.py3compat import cast_bytes +from IPython.utils.version import check_version + from .exceptions import ConversionException @@ -85,11 +87,12 @@ def pandoc_available(failmode="return", warn=False, alt=None): minimal_version = "1.12.1" +minimal_version_ok = False 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. + This function will raise PandocMissing if pandoc is not installed. Any error messages generated by pandoc are printed to stderr. Parameters @@ -112,6 +115,7 @@ def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): # if pandoc is missing let the exception bubble us out of here pandoc_available(failmode="raise", alt=cmd) + check_pandoc_version() # we can safely continue p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) @@ -121,22 +125,34 @@ def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): def get_pandoc_version(): - """Gets the Pandoc version if Pandoc is installed.""" + """Gets the Pandoc version if Pandoc is installed. + PandocMissing will be raised if pandoc is unavailable. + """ try: - return pandoc.version + if not minimal_version_ok: + raise AttributeError() + else: + return pandoc.version except AttributeError: - out = pandoc("None", "None", "None", ["-v"]) + cmd = ["pandoc", "-v"] + try: + out = subprocess.check_output(cmd, universal_newlines=True) + except OSError as e: + raise PandocMissing(cmd, e) pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') pandoc.version = pv_re.search(out).group(0) return pandoc.version def check_pandoc_version(): - """Returns True if minimal pandoc version is met""" - return get_pandoc_version() >= minimal_version - -if pandoc_available(warn=True)[0]: - 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) + """Returns True if minimal pandoc version is met. + PandocMissing will be raised if pandoc is unavailable. + """ + global minimal_version_ok + if not minimal_version_ok: + minimal_version_ok = check_version( get_pandoc_version(), minimal_version ) + if not minimal_version_ok: + 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) + return minimal_version_ok