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