##// END OF EJS Templates
Raise a named exception when pandoc is missing...
David Wolever -
Show More
@@ -14,7 +14,6 b''
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib imports
16 # Stdlib imports
17 import sys
18 import subprocess
17 import subprocess
19
18
20 # IPython imports
19 # IPython imports
@@ -24,6 +23,15 b' from IPython.utils.py3compat import cast_bytes'
24 # Classes and functions
23 # Classes and functions
25 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
26
25
26 class PandocMissing(SystemExit):
27 """Exception raised when Pandoc is missing.
28
29 A subclass of SystemExit so it will cause an exit if not caught, but
30 explicitly named so that it's possible to catch.
31 """
32 pass
33
34
27 def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
35 def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
28 """Convert an input string in format `from` to format `to` via pandoc.
36 """Convert an input string in format `from` to format `to` via pandoc.
29
37
@@ -49,11 +57,14 b" def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):"
49 command.extend(extra_args)
57 command.extend(extra_args)
50 try:
58 try:
51 p = subprocess.Popen(command,
59 p = subprocess.Popen(command,
52 stdin=subprocess.PIPE, stdout=subprocess.PIPE
60 stdin=subprocess.PIPE, stdout=subprocess.PIPE
61 )
62 except OSError as e:
63 raise PandocMissing(
64 "Error trying to run '%s': %s.\n" %(" ".join(command), e) +
65 "Please check that pandoc is installed:\n" +
66 "http://johnmacfarlane.net/pandoc/installing.html"
53 )
67 )
54 except OSError:
55 sys.exit("ERROR: Unable to launch pandoc. Please install pandoc:\n"
56 "(http://johnmacfarlane.net/pandoc/installing.html)")
57 out, _ = p.communicate(cast_bytes(source, encoding))
68 out, _ = p.communicate(cast_bytes(source, encoding))
58 out = out.decode(encoding, 'replace')
69 out = out.decode(encoding, 'replace')
59 return out[:-1]
70 return out[:-1]
General Comments 0
You need to be logged in to leave comments. Login now