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