Show More
@@ -1,56 +1,60 | |||||
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 |
|
17 | import sys | |
18 | import subprocess |
|
18 | import subprocess | |
19 |
|
19 | |||
20 | # IPython imports |
|
20 | # IPython imports | |
21 | from IPython.utils.py3compat import cast_bytes |
|
21 | from IPython.utils.py3compat import cast_bytes | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Classes and functions |
|
24 | # Classes and functions | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
27 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): | |
28 | """Convert an input string in format `from` to format `to` via pandoc. |
|
28 | """Convert an input string in format `from` to format `to` via pandoc. | |
29 |
|
29 | |||
30 | This function will raise an error if pandoc is not installed. |
|
30 | This function will raise an error if pandoc is not installed. | |
31 | Any error messages generated by pandoc are printed to stderr. |
|
31 | Any error messages generated by pandoc are printed to stderr. | |
32 |
|
32 | |||
33 | Parameters |
|
33 | Parameters | |
34 | ---------- |
|
34 | ---------- | |
35 | source : string |
|
35 | source : string | |
36 | Input string, assumed to be valid format `from`. |
|
36 | Input string, assumed to be valid format `from`. | |
37 | fmt : string |
|
37 | fmt : string | |
38 | The name of the input format (markdown, etc.) |
|
38 | The name of the input format (markdown, etc.) | |
39 | to : string |
|
39 | to : string | |
40 | The name of the output format (html, etc.) |
|
40 | The name of the output format (html, etc.) | |
41 |
|
41 | |||
42 | Returns |
|
42 | Returns | |
43 | ------- |
|
43 | ------- | |
44 | out : unicode |
|
44 | out : unicode | |
45 | Output as returned by pandoc. |
|
45 | Output as returned by pandoc. | |
46 | """ |
|
46 | """ | |
47 | command = ['pandoc', '-f', fmt, '-t', to] |
|
47 | command = ['pandoc', '-f', fmt, '-t', to] | |
48 | if extra_args: |
|
48 | if extra_args: | |
49 | command.extend(extra_args) |
|
49 | command.extend(extra_args) | |
50 | p = subprocess.Popen(command, |
|
50 | try: | |
51 | stdin=subprocess.PIPE, stdout=subprocess.PIPE |
|
51 | p = subprocess.Popen(command, | |
52 | ) |
|
52 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |
|
53 | ) | |||
|
54 | except OSError: | |||
|
55 | sys.exit("ERROR: Unable to launch pandoc. Please install pandoc:\n" | |||
|
56 | "(http://johnmacfarlane.net/pandoc/installing.html)") | |||
53 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
57 | out, _ = p.communicate(cast_bytes(source, encoding)) | |
54 | out = out.decode(encoding, 'replace') |
|
58 | out = out.decode(encoding, 'replace') | |
55 | return out[:-1] |
|
59 | return out[:-1] | |
56 |
|
60 |
General Comments 0
You need to be logged in to leave comments.
Login now