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