Show More
@@ -1,60 +1,71 | |||
|
1 | 1 | """Utility for calling pandoc""" |
|
2 | 2 | #----------------------------------------------------------------------------- |
|
3 | 3 | # Copyright (c) 2013 the IPython Development Team. |
|
4 | 4 | # |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | # |
|
7 | 7 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Imports |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | 16 | # Stdlib imports |
|
17 | import sys | |
|
18 | 17 | import subprocess |
|
19 | 18 | |
|
20 | 19 | # IPython imports |
|
21 | 20 | from IPython.utils.py3compat import cast_bytes |
|
22 | 21 | |
|
23 | 22 | #----------------------------------------------------------------------------- |
|
24 | 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 | 35 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
28 | 36 | """Convert an input string in format `from` to format `to` via pandoc. |
|
29 | 37 | |
|
30 | 38 | This function will raise an error if pandoc is not installed. |
|
31 | 39 | Any error messages generated by pandoc are printed to stderr. |
|
32 | 40 | |
|
33 | 41 | Parameters |
|
34 | 42 | ---------- |
|
35 | 43 | source : string |
|
36 | 44 | Input string, assumed to be valid format `from`. |
|
37 | 45 | fmt : string |
|
38 | 46 | The name of the input format (markdown, etc.) |
|
39 | 47 | to : string |
|
40 | 48 | The name of the output format (html, etc.) |
|
41 | 49 | |
|
42 | 50 | Returns |
|
43 | 51 | ------- |
|
44 | 52 | out : unicode |
|
45 | 53 | Output as returned by pandoc. |
|
46 | 54 | """ |
|
47 | 55 | command = ['pandoc', '-f', fmt, '-t', to] |
|
48 | 56 | if extra_args: |
|
49 | 57 | command.extend(extra_args) |
|
50 | 58 | try: |
|
51 | 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 | 68 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
58 | 69 | out = out.decode(encoding, 'replace') |
|
59 | 70 | return out[:-1] |
|
60 | 71 |
General Comments 0
You need to be logged in to leave comments.
Login now