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