##// END OF EJS Templates
Raise a named exception when pandoc is missing...
David Wolever -
Show More
@@ -1,60 +1,71 b''
1 """Utility for calling pandoc"""
1 """Utility for calling pandoc"""
2 #-----------------------------------------------------------------------------
2 #-----------------------------------------------------------------------------
3 # Copyright (c) 2013 the IPython Development Team.
3 # Copyright (c) 2013 the IPython Development Team.
4 #
4 #
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6 #
6 #
7 # The full license is in the file COPYING.txt, distributed with this software.
7 # The full license is in the file COPYING.txt, distributed with this software.
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Imports
11 # Imports
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
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
21 from IPython.utils.py3compat import cast_bytes
20 from IPython.utils.py3compat import cast_bytes
22
21
23 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
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
30 This function will raise an error if pandoc is not installed.
38 This function will raise an error if pandoc is not installed.
31 Any error messages generated by pandoc are printed to stderr.
39 Any error messages generated by pandoc are printed to stderr.
32
40
33 Parameters
41 Parameters
34 ----------
42 ----------
35 source : string
43 source : string
36 Input string, assumed to be valid format `from`.
44 Input string, assumed to be valid format `from`.
37 fmt : string
45 fmt : string
38 The name of the input format (markdown, etc.)
46 The name of the input format (markdown, etc.)
39 to : string
47 to : string
40 The name of the output format (html, etc.)
48 The name of the output format (html, etc.)
41
49
42 Returns
50 Returns
43 -------
51 -------
44 out : unicode
52 out : unicode
45 Output as returned by pandoc.
53 Output as returned by pandoc.
46 """
54 """
47 command = ['pandoc', '-f', fmt, '-t', to]
55 command = ['pandoc', '-f', fmt, '-t', to]
48 if extra_args:
56 if extra_args:
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
53 )
61 )
54 except OSError:
62 except OSError as e:
55 sys.exit("ERROR: Unable to launch pandoc. Please install pandoc:\n"
63 raise PandocMissing(
56 "(http://johnmacfarlane.net/pandoc/installing.html)")
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"
67 )
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]
60
71
General Comments 0
You need to be logged in to leave comments. Login now