Show More
@@ -21,6 +21,8 b' from io import TextIOWrapper, BytesIO' | |||||
21 |
|
21 | |||
22 | # IPython imports |
|
22 | # IPython imports | |
23 | from IPython.utils.py3compat import cast_bytes |
|
23 | from IPython.utils.py3compat import cast_bytes | |
|
24 | from IPython.utils.version import check_version | |||
|
25 | ||||
24 |
|
26 | |||
25 | from .exceptions import ConversionException |
|
27 | from .exceptions import ConversionException | |
26 |
|
28 | |||
@@ -85,11 +87,12 b' def pandoc_available(failmode="return", warn=False, alt=None):' | |||||
85 |
|
87 | |||
86 |
|
88 | |||
87 | minimal_version = "1.12.1" |
|
89 | minimal_version = "1.12.1" | |
|
90 | minimal_version_ok = False | |||
88 |
|
91 | |||
89 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
92 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): | |
90 | """Convert an input string in format `from` to format `to` via pandoc. |
|
93 | """Convert an input string in format `from` to format `to` via pandoc. | |
91 |
|
94 | |||
92 |
This function will raise |
|
95 | This function will raise PandocMissing if pandoc is not installed. | |
93 | Any error messages generated by pandoc are printed to stderr. |
|
96 | Any error messages generated by pandoc are printed to stderr. | |
94 |
|
97 | |||
95 | Parameters |
|
98 | Parameters | |
@@ -112,6 +115,7 b" def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):" | |||||
112 |
|
115 | |||
113 | # if pandoc is missing let the exception bubble us out of here |
|
116 | # if pandoc is missing let the exception bubble us out of here | |
114 | pandoc_available(failmode="raise", alt=cmd) |
|
117 | pandoc_available(failmode="raise", alt=cmd) | |
|
118 | check_pandoc_version() | |||
115 |
|
119 | |||
116 | # we can safely continue |
|
120 | # we can safely continue | |
117 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
121 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
@@ -121,22 +125,34 b" def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):" | |||||
121 |
|
125 | |||
122 |
|
126 | |||
123 | def get_pandoc_version(): |
|
127 | def get_pandoc_version(): | |
124 |
"""Gets the Pandoc version if Pandoc is installed. |
|
128 | """Gets the Pandoc version if Pandoc is installed. | |
|
129 | PandocMissing will be raised if pandoc is unavailable. | |||
|
130 | """ | |||
125 | try: |
|
131 | try: | |
|
132 | if not minimal_version_ok: | |||
|
133 | raise AttributeError() | |||
|
134 | else: | |||
126 | return pandoc.version |
|
135 | return pandoc.version | |
127 | except AttributeError: |
|
136 | except AttributeError: | |
128 |
|
|
137 | cmd = ["pandoc", "-v"] | |
|
138 | try: | |||
|
139 | out = subprocess.check_output(cmd, universal_newlines=True) | |||
|
140 | except OSError as e: | |||
|
141 | raise PandocMissing(cmd, e) | |||
129 | pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') |
|
142 | pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') | |
130 | pandoc.version = pv_re.search(out).group(0) |
|
143 | pandoc.version = pv_re.search(out).group(0) | |
131 | return pandoc.version |
|
144 | return pandoc.version | |
132 |
|
145 | |||
133 | def check_pandoc_version(): |
|
146 | def check_pandoc_version(): | |
134 |
"""Returns True if minimal pandoc version is met |
|
147 | """Returns True if minimal pandoc version is met. | |
135 | return get_pandoc_version() >= minimal_version |
|
148 | PandocMissing will be raised if pandoc is unavailable. | |
136 |
|
149 | """ | ||
137 | if pandoc_available(warn=True)[0]: |
|
150 | global minimal_version_ok | |
138 |
if |
|
151 | if not minimal_version_ok: | |
|
152 | minimal_version_ok = check_version( get_pandoc_version(), minimal_version ) | |||
|
153 | if not minimal_version_ok: | |||
139 | warnings.warn( "You are using an old version of pandoc (%s)\n" % pandoc.version + |
|
154 | warnings.warn( "You are using an old version of pandoc (%s)\n" % pandoc.version + | |
140 | "Recommended version is %s.\nTry updating." % minimal_version + |
|
155 | "Recommended version is %s.\nTry updating." % minimal_version + | |
141 | "http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", |
|
156 | "http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", | |
142 | RuntimeWarning, stacklevel=2) |
|
157 | RuntimeWarning, stacklevel=2) | |
|
158 | return minimal_version_ok |
General Comments 0
You need to be logged in to leave comments.
Login now