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