Show More
@@ -1,141 +1,141 b'' | |||
|
1 | 1 | """Utility for calling pandoc""" |
|
2 | 2 | #----------------------------------------------------------------------------- |
|
3 | 3 | # Copyright (c) 2014 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 | from __future__ import print_function |
|
14 | 14 | |
|
15 | 15 | # Stdlib imports |
|
16 | 16 | import subprocess |
|
17 | 17 | import warnings |
|
18 | 18 | import re |
|
19 | 19 | from io import TextIOWrapper, BytesIO |
|
20 | 20 | |
|
21 | 21 | # IPython imports |
|
22 | 22 | from IPython.utils.py3compat import cast_bytes |
|
23 | 23 | from IPython.utils.version import check_version |
|
24 | 24 | from IPython.utils.process import is_cmd_found, FindCmdError |
|
25 | 25 | |
|
26 | 26 | from .exceptions import ConversionException |
|
27 | 27 | |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | # Classes and functions |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | _minimal_version = "1.12.1" |
|
32 | 32 | |
|
33 | 33 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
34 | 34 | """Convert an input string in format `from` to format `to` via pandoc. |
|
35 | 35 | |
|
36 | 36 | Parameters |
|
37 | 37 | ---------- |
|
38 | 38 | source : string |
|
39 | 39 | Input string, assumed to be valid format `from`. |
|
40 | 40 | fmt : string |
|
41 | 41 | The name of the input format (markdown, etc.) |
|
42 | 42 | to : string |
|
43 | 43 | The name of the output format (html, etc.) |
|
44 | 44 | |
|
45 | 45 | Returns |
|
46 | 46 | ------- |
|
47 | 47 | out : unicode |
|
48 | 48 | Output as returned by pandoc. |
|
49 | 49 | |
|
50 | 50 | Raises |
|
51 | 51 | ------ |
|
52 | 52 | PandocMissing |
|
53 | 53 | If pandoc is not installed. |
|
54 | 54 | |
|
55 | 55 | Any error messages generated by pandoc are printed to stderr. |
|
56 | 56 | |
|
57 | 57 | """ |
|
58 | 58 | cmd = ['pandoc', '-f', fmt, '-t', to] |
|
59 | 59 | if extra_args: |
|
60 | 60 | cmd.extend(extra_args) |
|
61 | 61 | |
|
62 | 62 | # this will raise an exception that will pop us out of here |
|
63 | 63 | check_pandoc_version() |
|
64 | 64 | |
|
65 | 65 | # we can safely continue |
|
66 | 66 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
67 | 67 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
68 | 68 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() |
|
69 | 69 | return out.rstrip('\n') |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def get_pandoc_version(): |
|
73 | 73 | """Gets the Pandoc version if Pandoc is installed. |
|
74 | 74 | |
|
75 | 75 | If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value. |
|
76 | 76 | If the minimal version is met, it will return the cached version and stop probing Pandoc |
|
77 | 77 | (unless :func:`clean_cache()` is called). |
|
78 | 78 | |
|
79 | 79 | Raises |
|
80 | 80 | ------ |
|
81 | 81 | PandocMissing |
|
82 | 82 | If pandoc is unavailable. |
|
83 | 83 | """ |
|
84 | 84 | global __version |
|
85 | 85 | |
|
86 | 86 | if __version is None: |
|
87 | 87 | if not is_cmd_found('pandoc'): |
|
88 | 88 | raise PandocMissing() |
|
89 | 89 | |
|
90 | 90 | out = subprocess.check_output(['pandoc', '-v'], |
|
91 | 91 | universal_newlines=True) |
|
92 | 92 | out_lines = out.splitlines() |
|
93 | 93 | version_pattern = re.compile(r"^\d+(\.\d+){1,}$") |
|
94 | 94 | for tok in out_lines[0].split(): |
|
95 | 95 | if version_pattern.match(tok): |
|
96 | 96 | __version = tok |
|
97 | 97 | break |
|
98 | 98 | return __version |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | def check_pandoc_version(): |
|
102 | 102 | """Returns True if minimal pandoc version is met. |
|
103 | 103 | |
|
104 | 104 | Raises |
|
105 | 105 | ------ |
|
106 | 106 | PandocMissing |
|
107 | 107 | If pandoc is unavailable. |
|
108 | 108 | """ |
|
109 | 109 | v = get_pandoc_version() |
|
110 | 110 | if v is None: |
|
111 | 111 | warnings.warn("Sorry, we cannot determine the version of pandoc.\n" |
|
112 | 112 | "Please consider reporting this issue and include the" |
|
113 | 113 | "output of pandoc --version.\nContinuing...", |
|
114 | 114 | RuntimeWarning, stacklevel=2) |
|
115 | return False | |
|
115 | return False | |
|
116 | 116 | ok = check_version(v , _minimal_version ) |
|
117 | 117 | if not ok: |
|
118 | 118 | warnings.warn( "You are using an old version of pandoc (%s)\n" % v + |
|
119 | 119 | "Recommended version is %s.\nTry updating." % _minimal_version + |
|
120 | 120 | "http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", |
|
121 | 121 | RuntimeWarning, stacklevel=2) |
|
122 | 122 | return ok |
|
123 | 123 | |
|
124 | 124 | #----------------------------------------------------------------------------- |
|
125 | 125 | # Exception handling |
|
126 | 126 | #----------------------------------------------------------------------------- |
|
127 | 127 | class PandocMissing(ConversionException): |
|
128 | 128 | """Exception raised when Pandoc is missing. """ |
|
129 | 129 | def __init__(self, *args, **kwargs): |
|
130 | 130 | super(PandocMissing, self).__init__( "Pandoc wasn't found.\n" + |
|
131 | 131 | "Please check that pandoc is installed:\n" + |
|
132 | 132 | "http://johnmacfarlane.net/pandoc/installing.html" ) |
|
133 | 133 | |
|
134 | 134 | #----------------------------------------------------------------------------- |
|
135 | 135 | # Internal state management |
|
136 | 136 | #----------------------------------------------------------------------------- |
|
137 | 137 | def clean_cache(): |
|
138 | 138 | global __version |
|
139 | 139 | __version = None |
|
140 | 140 | |
|
141 | 141 | __version = None |
General Comments 0
You need to be logged in to leave comments.
Login now