Show More
|
1 | NO CONTENT: new file 100644 |
@@ -1,135 +1,128 b'' | |||
|
1 | 1 | """Utility for calling pandoc""" |
|
2 | 2 | #----------------------------------------------------------------------------- |
|
3 |
# Copyright (c) 201 |
|
|
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 | ||
|
14 | 13 | from __future__ import print_function |
|
15 | 14 | |
|
16 | 15 | # Stdlib imports |
|
17 | 16 | import subprocess |
|
18 | 17 | import re |
|
19 | 18 | import warnings |
|
20 | 19 | from io import TextIOWrapper, BytesIO |
|
21 | 20 | |
|
22 | 21 | # IPython imports |
|
23 | 22 | from IPython.utils.py3compat import cast_bytes |
|
24 | 23 | from IPython.utils.version import check_version |
|
25 | 24 | from IPython.utils.process import is_cmd_found, FindCmdError |
|
26 | 25 | |
|
27 | ||
|
28 | 26 | from .exceptions import ConversionException |
|
29 | 27 | |
|
30 | 28 | #----------------------------------------------------------------------------- |
|
31 | 29 | # Classes and functions |
|
32 | 30 | #----------------------------------------------------------------------------- |
|
33 | 31 | _minimal_version = "1.12.1" |
|
34 | 32 | |
|
35 | 33 | def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'): |
|
36 | 34 | """Convert an input string in format `from` to format `to` via pandoc. |
|
37 | 35 | |
|
38 | 36 | Parameters |
|
39 | 37 | ---------- |
|
40 | 38 | source : string |
|
41 | 39 | Input string, assumed to be valid format `from`. |
|
42 | 40 | fmt : string |
|
43 | 41 | The name of the input format (markdown, etc.) |
|
44 | 42 | to : string |
|
45 | 43 | The name of the output format (html, etc.) |
|
46 | 44 | |
|
47 | 45 | Returns |
|
48 | 46 | ------- |
|
49 | 47 | out : unicode |
|
50 | 48 | Output as returned by pandoc. |
|
51 | 49 | |
|
52 | 50 | Exceptions |
|
53 | 51 | ---------- |
|
54 | 52 | This function will raise PandocMissing if pandoc is not installed. |
|
55 | 53 | Any error messages generated by pandoc are printed to stderr. |
|
56 | 54 | |
|
57 | 55 | """ |
|
58 | 56 | cmd = ['pandoc', '-f', fmt, '-t', to] |
|
59 | 57 | if extra_args: |
|
60 | 58 | cmd.extend(extra_args) |
|
61 | 59 | |
|
62 | 60 | # this will raise an exception that will pop us out of here |
|
63 | 61 | check_pandoc_version() |
|
64 | 62 | |
|
65 | 63 | # we can safely continue |
|
66 | 64 | p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
67 | 65 | out, _ = p.communicate(cast_bytes(source, encoding)) |
|
68 | 66 | out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() |
|
69 | 67 | return out.rstrip('\n') |
|
70 | 68 | |
|
71 | 69 | |
|
72 | 70 | def get_pandoc_version(): |
|
73 | 71 | """Gets the Pandoc version if Pandoc is installed. |
|
74 | 72 | |
|
75 | 73 | Return |
|
76 | 74 | ------ |
|
77 | 75 | If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value. |
|
78 | 76 | If the minimal version is met, it will return the cached version and stop probing Pandoc |
|
79 | 77 | (unless `clean_cache()` is called). |
|
80 | 78 | |
|
81 | 79 | Exceptions |
|
82 | 80 | ---------- |
|
83 | 81 | PandocMissing will be raised if pandoc is unavailable. |
|
84 | 82 | """ |
|
85 | 83 | global __version |
|
86 | 84 | |
|
87 |
if __version is |
|
|
88 | return __version | |
|
89 | else: | |
|
85 | if __version is None: | |
|
90 | 86 | if not is_cmd_found('pandoc'): |
|
91 | 87 | raise PandocMissing() |
|
92 | 88 | |
|
93 | 89 | out = subprocess.check_output( ['pandoc', '-v'], universal_newlines=True) |
|
94 | 90 | pv_re = re.compile(r'(\d{0,3}\.\d{0,3}\.\d{0,3})') |
|
95 | 91 | __version = pv_re.search(out).group(0) |
|
96 |
|
|
|
92 | return __version | |
|
97 | 93 | |
|
98 | 94 | |
|
99 | 95 | def check_pandoc_version(): |
|
100 | 96 | """Returns True if minimal pandoc version is met. |
|
101 | 97 | |
|
102 | 98 | Exceptions |
|
103 | 99 | ---------- |
|
104 | 100 | PandocMissing will be raised if pandoc is unavailable. |
|
105 | 101 | """ |
|
106 | 102 | v = get_pandoc_version() |
|
107 | 103 | ok = check_version(v , _minimal_version ) |
|
108 | 104 | if not ok: |
|
109 | 105 | warnings.warn( "You are using an old version of pandoc (%s)\n" % v + |
|
110 | 106 | "Recommended version is %s.\nTry updating." % _minimal_version + |
|
111 | 107 | "http://johnmacfarlane.net/pandoc/installing.html.\nContinuing with doubts...", |
|
112 | 108 | RuntimeWarning, stacklevel=2) |
|
113 | 109 | return ok |
|
114 | 110 | |
|
115 | 111 | #----------------------------------------------------------------------------- |
|
116 | 112 | # Exception handling |
|
117 | 113 | #----------------------------------------------------------------------------- |
|
118 | 114 | class PandocMissing(ConversionException): |
|
119 | 115 | """Exception raised when Pandoc is missing. """ |
|
120 | 116 | def __init__(self, *args, **kwargs): |
|
121 | 117 | super(PandocMissing, self).__init__( "Pandoc wasn't found.\n" + |
|
122 | 118 | "Please check that pandoc is installed:\n" + |
|
123 | 119 | "http://johnmacfarlane.net/pandoc/installing.html" ) |
|
124 | 120 | |
|
125 | 121 | #----------------------------------------------------------------------------- |
|
126 | 122 | # Internal state management |
|
127 | 123 | #----------------------------------------------------------------------------- |
|
128 | 124 | def clean_cache(): |
|
129 | 125 | global __version |
|
130 | 126 | __version = None |
|
131 | 127 | |
|
132 | 128 | __version = None |
|
133 | ||
|
134 | ||
|
135 |
@@ -1,63 +1,62 b'' | |||
|
1 | 1 | """Test Pandoc module""" |
|
2 | ||
|
3 | 2 | #----------------------------------------------------------------------------- |
|
4 | 3 | # Copyright (C) 2014 The IPython Development Team |
|
5 | 4 | # |
|
6 | 5 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | 6 | # the file COPYING, distributed as part of this software. |
|
8 | 7 | #----------------------------------------------------------------------------- |
|
9 | 8 | |
|
10 | 9 | #----------------------------------------------------------------------------- |
|
11 | 10 | # Imports |
|
12 | 11 | #----------------------------------------------------------------------------- |
|
13 | 12 | import os |
|
14 | 13 | |
|
15 | 14 | from IPython.testing import decorators as dec |
|
16 | 15 | |
|
17 | 16 | from .base import TestsBase |
|
18 | 17 | from ..utils import pandoc |
|
19 | 18 | |
|
20 | 19 | #----------------------------------------------------------------------------- |
|
21 | 20 | # Classes and functions |
|
22 | 21 | #----------------------------------------------------------------------------- |
|
23 | 22 | class TestPandoc(TestsBase): |
|
24 | 23 | """Collection of Pandoc tests""" |
|
25 | 24 | |
|
26 | 25 | def __init__(self, *args, **kwargs): |
|
27 | 26 | super(TestPandoc, self).__init__(*args, **kwargs) |
|
28 | 27 | self.original_env = os.environ.copy() |
|
29 | 28 | |
|
30 | 29 | @dec.onlyif_cmds_exist('pandoc') |
|
31 | 30 | def test_pandoc_available(self): |
|
32 | 31 | """ Test behaviour that pandoc functions raise PandocMissing as documented """ |
|
33 | 32 | pandoc.clean_cache() |
|
34 | 33 | |
|
35 | 34 | os.environ["PATH"] = "" |
|
36 | 35 | assert pandoc_function_raised_missing(pandoc.get_pandoc_version) == True |
|
37 | 36 | assert pandoc_function_raised_missing(pandoc.check_pandoc_version) == True |
|
38 | 37 | assert pandoc_function_raised_missing(pandoc.pandoc, "", "markdown", "html") == True |
|
39 | 38 | |
|
40 | 39 | # original_env["PATH"] should contain pandoc |
|
41 | 40 | os.environ["PATH"] = self.original_env["PATH"] |
|
42 | 41 | assert pandoc_function_raised_missing(pandoc.get_pandoc_version) == False |
|
43 | 42 | assert pandoc_function_raised_missing(pandoc.check_pandoc_version) == False |
|
44 | 43 | assert pandoc_function_raised_missing(pandoc.pandoc, "", "markdown", "html") == False |
|
45 | 44 | |
|
46 | 45 | @dec.onlyif_cmds_exist('pandoc') |
|
47 | 46 | def test_minimal_version(self): |
|
48 | 47 | original_minversion = pandoc._minimal_version |
|
49 | 48 | |
|
50 | 49 | pandoc._minimal_version = "120.0" |
|
51 | 50 | assert not pandoc.check_pandoc_version() |
|
52 | 51 | |
|
53 | 52 | pandoc._minimal_version = pandoc.get_pandoc_version() |
|
54 | 53 | assert pandoc.check_pandoc_version() |
|
55 | 54 | |
|
56 | 55 | |
|
57 | 56 | def pandoc_function_raised_missing(f, *args, **kwargs): |
|
58 | 57 | try: |
|
59 | 58 | f(*args, **kwargs) |
|
60 | 59 | except pandoc.PandocMissing: |
|
61 | 60 | return True |
|
62 | 61 | else: |
|
63 | 62 | return False |
General Comments 0
You need to be logged in to leave comments.
Login now