##// END OF EJS Templates
Switch if/else order
Jonathan Frederic -
Show More
@@ -1,139 +1,139 b''
1 """Markdown filters
1 """Markdown filters
2 This file contains a collection of utility filters for dealing with
2 This file contains a collection of utility filters for dealing with
3 markdown within Jinja templates.
3 markdown within Jinja templates.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # Stdlib imports
18 # Stdlib imports
19 import os
19 import os
20 import subprocess
20 import subprocess
21 import warnings
21 import warnings
22 from io import TextIOWrapper, BytesIO
22 from io import TextIOWrapper, BytesIO
23
23
24 # IPython imports
24 # IPython imports
25 from IPython.nbconvert.utils.pandoc import pandoc
25 from IPython.nbconvert.utils.pandoc import pandoc
26 from IPython.nbconvert.utils.exceptions import ConversionException
26 from IPython.nbconvert.utils.exceptions import ConversionException
27 from IPython.utils.process import get_output_error_code
27 from IPython.utils.process import get_output_error_code
28 from IPython.utils.py3compat import cast_bytes
28 from IPython.utils.py3compat import cast_bytes
29 from IPython.utils.version import check_version
29 from IPython.utils.version import check_version
30
30
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 # Functions
32 # Functions
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 marked = os.path.join(os.path.dirname(__file__), "marked.js")
34 marked = os.path.join(os.path.dirname(__file__), "marked.js")
35 _node = None
35 _node = None
36
36
37 __all__ = [
37 __all__ = [
38 'markdown2html',
38 'markdown2html',
39 'markdown2html_pandoc',
39 'markdown2html_pandoc',
40 'markdown2html_marked',
40 'markdown2html_marked',
41 'markdown2latex',
41 'markdown2latex',
42 'markdown2rst',
42 'markdown2rst',
43 ]
43 ]
44
44
45 class NodeJSMissing(ConversionException):
45 class NodeJSMissing(ConversionException):
46 """Exception raised when node.js is missing."""
46 """Exception raised when node.js is missing."""
47 pass
47 pass
48
48
49 def markdown2latex(source):
49 def markdown2latex(source):
50 """Convert a markdown string to LaTeX via pandoc.
50 """Convert a markdown string to LaTeX via pandoc.
51
51
52 This function will raise an error if pandoc is not installed.
52 This function will raise an error if pandoc is not installed.
53 Any error messages generated by pandoc are printed to stderr.
53 Any error messages generated by pandoc are printed to stderr.
54
54
55 Parameters
55 Parameters
56 ----------
56 ----------
57 source : string
57 source : string
58 Input string, assumed to be valid markdown.
58 Input string, assumed to be valid markdown.
59
59
60 Returns
60 Returns
61 -------
61 -------
62 out : string
62 out : string
63 Output as returned by pandoc.
63 Output as returned by pandoc.
64 """
64 """
65 return pandoc(source, 'markdown', 'latex')
65 return pandoc(source, 'markdown', 'latex')
66
66
67 def markdown2html(source):
67 def markdown2html(source):
68 """Convert a markdown string to HTML"""
68 """Convert a markdown string to HTML"""
69 global _node
69 global _node
70 if _node is None:
70 if _node is None:
71 # prefer md2html via marked if node.js >= 0.9.12 is available
71 # prefer md2html via marked if node.js >= 0.9.12 is available
72 # node is called nodejs on debian, so try that first
72 # node is called nodejs on debian, so try that first
73 _node = 'nodejs'
73 _node = 'nodejs'
74 if not _verify_node(_node):
74 if not _verify_node(_node):
75 _node = 'node'
75 _node = 'node'
76 if not _verify_node(_node):
76 if not _verify_node(_node):
77 warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" +
77 warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" +
78 "Nbconvert will try to use Pandoc instead.")
78 "Nbconvert will try to use Pandoc instead.")
79 _node = False
79 _node = False
80 if not _node:
80 if _node:
81 return markdown2html_pandoc(source)
82 else:
83 return markdown2html_marked(source)
81 return markdown2html_marked(source)
82 else:
83 return markdown2html_pandoc(source)
84
84
85 def markdown2html_pandoc(source):
85 def markdown2html_pandoc(source):
86 """Convert a markdown string to HTML via pandoc"""
86 """Convert a markdown string to HTML via pandoc"""
87 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
87 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
88
88
89 def markdown2html_marked(source, encoding='utf-8'):
89 def markdown2html_marked(source, encoding='utf-8'):
90 """Convert a markdown string to HTML via marked"""
90 """Convert a markdown string to HTML via marked"""
91 command = [_node, marked]
91 command = [_node, marked]
92 try:
92 try:
93 p = subprocess.Popen(command,
93 p = subprocess.Popen(command,
94 stdin=subprocess.PIPE, stdout=subprocess.PIPE
94 stdin=subprocess.PIPE, stdout=subprocess.PIPE
95 )
95 )
96 except OSError as e:
96 except OSError as e:
97 raise NodeJSMissing(
97 raise NodeJSMissing(
98 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
98 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
99 "Please check that Node.js is installed."
99 "Please check that Node.js is installed."
100 )
100 )
101 out, _ = p.communicate(cast_bytes(source, encoding))
101 out, _ = p.communicate(cast_bytes(source, encoding))
102 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
102 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
103 return out.rstrip('\n')
103 return out.rstrip('\n')
104
104
105 def markdown2rst(source):
105 def markdown2rst(source):
106 """Convert a markdown string to LaTeX via pandoc.
106 """Convert a markdown string to LaTeX via pandoc.
107
107
108 This function will raise an error if pandoc is not installed.
108 This function will raise an error if pandoc is not installed.
109 Any error messages generated by pandoc are printed to stderr.
109 Any error messages generated by pandoc are printed to stderr.
110
110
111 Parameters
111 Parameters
112 ----------
112 ----------
113 source : string
113 source : string
114 Input string, assumed to be valid markdown.
114 Input string, assumed to be valid markdown.
115
115
116 Returns
116 Returns
117 -------
117 -------
118 out : string
118 out : string
119 Output as returned by pandoc.
119 Output as returned by pandoc.
120 """
120 """
121 return pandoc(source, 'markdown', 'rst')
121 return pandoc(source, 'markdown', 'rst')
122
122
123 def _verify_node(cmd):
123 def _verify_node(cmd):
124 """Verify that the node command exists and is at least the minimum supported
124 """Verify that the node command exists and is at least the minimum supported
125 version of node.
125 version of node.
126
126
127 Parameters
127 Parameters
128 ----------
128 ----------
129 cmd : string
129 cmd : string
130 Node command to verify (i.e 'node')."""
130 Node command to verify (i.e 'node')."""
131 try:
131 try:
132 out, err, return_code = get_output_error_code([cmd, '--version'])
132 out, err, return_code = get_output_error_code([cmd, '--version'])
133 except OSError:
133 except OSError:
134 # Command not found
134 # Command not found
135 return False
135 return False
136 if return_code:
136 if return_code:
137 # Command error
137 # Command error
138 return False
138 return False
139 return check_version(out.lstrip('v'), '0.9.12')
139 return check_version(out.lstrip('v'), '0.9.12')
General Comments 0
You need to be logged in to leave comments. Login now