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