##// END OF EJS Templates
Split logic onto multiple lines.
Jonathan Frederic -
Show More
@@ -1,128 +1,131
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 from io import TextIOWrapper, BytesIO
21 from io import TextIOWrapper, BytesIO
22
22
23 # IPython imports
23 # IPython imports
24 from IPython.nbconvert.utils.pandoc import pandoc
24 from IPython.nbconvert.utils.pandoc import pandoc
25 from IPython.nbconvert.utils.exceptions import ConversionException
25 from IPython.nbconvert.utils.exceptions import ConversionException
26 from IPython.utils.process import get_output_error_code
26 from IPython.utils.process import get_output_error_code
27 from IPython.utils.py3compat import cast_bytes
27 from IPython.utils.py3compat import cast_bytes
28 from IPython.utils.version import check_version
28 from IPython.utils.version import check_version
29
29
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31 # Functions
31 # Functions
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 marked = os.path.join(os.path.dirname(__file__), "marked.js")
33 marked = os.path.join(os.path.dirname(__file__), "marked.js")
34
34
35 __all__ = [
35 __all__ = [
36 'markdown2html',
36 'markdown2html',
37 'markdown2html_pandoc',
37 'markdown2html_pandoc',
38 'markdown2html_marked',
38 'markdown2html_marked',
39 'markdown2latex',
39 'markdown2latex',
40 'markdown2rst',
40 'markdown2rst',
41 ]
41 ]
42
42
43 class NodeJSMissing(ConversionException):
43 class NodeJSMissing(ConversionException):
44 """Exception raised when node.js is missing."""
44 """Exception raised when node.js is missing."""
45 pass
45 pass
46
46
47 def markdown2latex(source):
47 def markdown2latex(source):
48 """Convert a markdown string to LaTeX via pandoc.
48 """Convert a markdown string to LaTeX via pandoc.
49
49
50 This function will raise an error if pandoc is not installed.
50 This function will raise an error if pandoc is not installed.
51 Any error messages generated by pandoc are printed to stderr.
51 Any error messages generated by pandoc are printed to stderr.
52
52
53 Parameters
53 Parameters
54 ----------
54 ----------
55 source : string
55 source : string
56 Input string, assumed to be valid markdown.
56 Input string, assumed to be valid markdown.
57
57
58 Returns
58 Returns
59 -------
59 -------
60 out : string
60 out : string
61 Output as returned by pandoc.
61 Output as returned by pandoc.
62 """
62 """
63 return pandoc(source, 'markdown', 'latex')
63 return pandoc(source, 'markdown', 'latex')
64
64
65 def markdown2html_pandoc(source):
65 def markdown2html_pandoc(source):
66 """Convert a markdown string to HTML via pandoc"""
66 """Convert a markdown string to HTML via pandoc"""
67 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
67 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
68
68
69 def markdown2html_marked(source, encoding='utf-8'):
69 def markdown2html_marked(source, encoding='utf-8'):
70 """Convert a markdown string to HTML via marked"""
70 """Convert a markdown string to HTML via marked"""
71 command = [node_cmd, marked]
71 command = [node_cmd, marked]
72 try:
72 try:
73 p = subprocess.Popen(command,
73 p = subprocess.Popen(command,
74 stdin=subprocess.PIPE, stdout=subprocess.PIPE
74 stdin=subprocess.PIPE, stdout=subprocess.PIPE
75 )
75 )
76 except OSError as e:
76 except OSError as e:
77 raise NodeJSMissing(
77 raise NodeJSMissing(
78 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
78 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
79 "Please check that Node.js is installed."
79 "Please check that Node.js is installed."
80 )
80 )
81 out, _ = p.communicate(cast_bytes(source, encoding))
81 out, _ = p.communicate(cast_bytes(source, encoding))
82 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
82 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
83 return out.rstrip('\n')
83 return out.rstrip('\n')
84
84
85 def markdown2rst(source):
85 def markdown2rst(source):
86 """Convert a markdown string to LaTeX via pandoc.
86 """Convert a markdown string to LaTeX via pandoc.
87
87
88 This function will raise an error if pandoc is not installed.
88 This function will raise an error if pandoc is not installed.
89 Any error messages generated by pandoc are printed to stderr.
89 Any error messages generated by pandoc are printed to stderr.
90
90
91 Parameters
91 Parameters
92 ----------
92 ----------
93 source : string
93 source : string
94 Input string, assumed to be valid markdown.
94 Input string, assumed to be valid markdown.
95
95
96 Returns
96 Returns
97 -------
97 -------
98 out : string
98 out : string
99 Output as returned by pandoc.
99 Output as returned by pandoc.
100 """
100 """
101 return pandoc(source, 'markdown', 'rst')
101 return pandoc(source, 'markdown', 'rst')
102
102
103 def _verify_node(cmd):
103 def _verify_node(cmd):
104 """Verify that the node command exists and is at least the minimum supported
104 """Verify that the node command exists and is at least the minimum supported
105 version of node.
105 version of node.
106
106
107 Parameters
107 Parameters
108 ----------
108 ----------
109 cmd : string
109 cmd : string
110 Node command to verify (i.e 'node')."""
110 Node command to verify (i.e 'node')."""
111 try:
111 try:
112 out, err, return_code = get_output_error_code([cmd, '--version'])
112 out, err, return_code = get_output_error_code([cmd, '--version'])
113 except OSError:
113 except OSError:
114 # Command not found
114 # Command not found
115 return False
115 return False
116 return return_code == 0 and check_version(out.lstrip('v'), '0.9.12')
116 if return_code:
117 # Command error
118 return False
119 return check_version(out.lstrip('v'), '0.9.12')
117
120
118 # prefer md2html via marked if node.js >= 0.9.12 is available
121 # prefer md2html via marked if node.js >= 0.9.12 is available
119 # node is called nodejs on debian, so try that first
122 # node is called nodejs on debian, so try that first
120 node_cmd = 'nodejs'
123 node_cmd = 'nodejs'
121 if _verify_node(node_cmd):
124 if _verify_node(node_cmd):
122 markdown2html = markdown2html_marked
125 markdown2html = markdown2html_marked
123 else:
126 else:
124 node_cmd = 'node'
127 node_cmd = 'node'
125 if _verify_node(node_cmd):
128 if _verify_node(node_cmd):
126 markdown2html = markdown2html_marked
129 markdown2html = markdown2html_marked
127 else:
130 else:
128 markdown2html = markdown2html_pandoc
131 markdown2html = markdown2html_pandoc
General Comments 0
You need to be logged in to leave comments. Login now