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