Show More
@@ -1,140 +1,142 b'' | |||
|
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 subprocess |
|
18 | 18 | import re |
|
19 | 19 | import warnings |
|
20 | 20 | from io import TextIOWrapper, BytesIO |
|
21 | 21 | |
|
22 | 22 | # IPython imports |
|
23 | 23 | from IPython.utils.py3compat import cast_bytes |
|
24 | 24 | |
|
25 | 25 | from .exceptions import ConversionException |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | #---------------------------------------------------------------------------- |
|
29 | 29 | # Preliminary checks. |
|
30 | 30 | # Not finding Pandoc is not always fatal so only a warning is issued at the |
|
31 | 31 | # module root level so that the import of this module is not fatal. |
|
32 | 32 | #---------------------------------------------------------------------------- |
|
33 | 33 | |
|
34 | 34 | class PandocMissing(ConversionException): |
|
35 | 35 | """Exception raised when Pandoc is missing. """ |
|
36 | 36 | def __init__(self, cmd, exc, *args, **kwargs): |
|
37 | 37 | super(PandocMissing, self).__init__( "The command '%s' returned an error: %s.\n" %(" ".join(cmd), exc) + |
|
38 | 38 | "Please check that pandoc is installed:\n" + |
|
39 | 39 | "http://johnmacfarlane.net/pandoc/installing.html" ) |
|
40 | 40 | |
|
41 | def pandoc_available(failmode="return", warn=False): | |
|
41 | def pandoc_available(failmode="return", warn=False, alt=None): | |
|
42 | 42 | """Is pandoc available. Only tries to call Pandoc |
|
43 | 43 | and inform you that it succeeded or failed. |
|
44 | 44 | |
|
45 | 45 | Parameters |
|
46 | 46 | ---------- |
|
47 | 47 | - failmode : string |
|
48 | 48 | either "return" or "raise". If "return" and pandoc |
|
49 | 49 | is not available, will return (False, e) where e is |
|
50 | 50 | the exception returned by subprocess.check_call. |
|
51 | 51 | - warn : bool |
|
52 | 52 | issue a user warning if pandoc is not available. |
|
53 | - alt: list of strings | |
|
54 | command to print in the error (not used as actual call) | |
|
53 | 55 | |
|
54 | 56 | Return |
|
55 | 57 | ------ |
|
56 | 58 | out : (Bool, Exception) |
|
57 | 59 | On success will return (True, None). On failure and failmode=="return" |
|
58 | 60 | will return (False, OSError instance) |
|
59 | 61 | """ |
|
60 | 62 | |
|
61 | 63 | cmd = ["pandoc", "-v"] |
|
62 | 64 | |
|
63 | 65 | try: |
|
64 | 66 | out = subprocess.check_output(cmd, universal_newlines=True) |
|
65 | 67 | return True, None |
|
66 |
except OSError |
|
|
68 | except OSError as e: | |
|
67 | 69 | if warn: |
|
68 | 70 | warnings.warn( |
|
69 | "Pandoc cannot be found (call %s failed).\n" % " ".join(cmd) + | |
|
71 | "Pandoc cannot be found (calling %s failed).\n" % " ".join(alt or cmd) + | |
|
70 | 72 | "Please check that pandoc is installed:\n" + |
|
71 | 73 | "http://johnmacfarlane.net/pandoc/installing.html" |
|
72 | 74 | ) |
|
73 | 75 | |
|
74 | 76 | if failmode == "return": |
|
75 | 77 | return False, e |
|
76 | 78 | else: |
|
77 | raise PandocMissing(cmd, e) | |
|
79 | raise PandocMissing(alt or cmd, e) | |
|
78 | 80 | |
|
79 | 81 | |
|
80 | 82 | #----------------------------------------------------------------------------- |
|
81 | 83 | # Classes and functions |
|
82 | 84 | #----------------------------------------------------------------------------- |
|
83 | 85 | |
|
84 | 86 | |
|
85 | 87 | minimal_version = "1.12.1" |
|
86 | 88 | |
|
87 | 89 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
88 | 90 | """Convert an input string in format `from` to format `to` via pandoc. |
|
89 | 91 | |
|
90 | 92 | This function will raise an error if pandoc is not installed. |
|
91 | 93 | Any error messages generated by pandoc are printed to stderr. |
|
92 | 94 | |
|
93 | 95 | Parameters |
|
94 | 96 | ---------- |
|
95 | 97 | source : string |
|
96 | 98 | Input string, assumed to be valid format `from`. |
|
97 | 99 | fmt : string |
|
98 | 100 | The name of the input format (markdown, etc.) |
|
99 | 101 | to : string |
|
100 | 102 | The name of the output format (html, etc.) |
|
101 | 103 | |
|
102 | 104 | Returns |
|
103 | 105 | ------- |
|
104 | 106 | out : unicode |
|
105 | 107 | Output as returned by pandoc. |
|
106 | 108 | """ |
|
107 | 109 | cmd = ['pandoc', '-f', fmt, '-t', to] |
|
108 | 110 | if extra_args: |
|
109 | 111 | cmd.extend(extra_args) |
|
110 | 112 | |
|
111 | 113 | # if pandoc is missing let the exception bubble us out of here |
|
112 | pandoc_available(failmode="raise") | |
|
114 | pandoc_available(failmode="raise", alt=cmd) | |
|
113 | 115 | |
|
114 | 116 | # we can safely continue |
|
115 | 117 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
116 | 118 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
117 | 119 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() |
|
118 | 120 | return out.rstrip('\n') |
|
119 | 121 | |
|
120 | 122 | |
|
121 | 123 | def get_pandoc_version(): |
|
122 | 124 | """Gets the Pandoc version if Pandoc is installed.""" |
|
123 | 125 | try: |
|
124 | 126 | return pandoc.version |
|
125 | 127 | except AttributeError: |
|
126 | 128 | out = pandoc("None", "None", "None", ["-v"]) |
|
127 | 129 | pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') |
|
128 | 130 | pandoc.version = pv_re.search(out).group(0) |
|
129 | 131 | return pandoc.version |
|
130 | 132 | |
|
131 | 133 | def check_pandoc_version(): |
|
132 | 134 | """Returns True if minimal pandoc version is met""" |
|
133 | 135 | return get_pandoc_version() >= minimal_version |
|
134 | 136 | |
|
135 | 137 | if pandoc_available(warn=True)[0]: |
|
136 | 138 | if(not check_pandoc_version()): |
|
137 | 139 | warnings.warn( "You are using an old version of pandoc (%s)\n" % pandoc.version + |
|
138 | 140 | "Recommended version is %s.\nTry updating." % minimal_version + |
|
139 | 141 | "http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", |
|
140 | 142 | RuntimeWarning, stacklevel=2) |
General Comments 0
You need to be logged in to leave comments.
Login now