Show More
@@ -18,18 +18,21 from __future__ import print_function | |||||
18 | # Stdlib imports |
|
18 | # Stdlib imports | |
19 | import os |
|
19 | import os | |
20 | import subprocess |
|
20 | import subprocess | |
|
21 | import warnings | |||
21 | from io import TextIOWrapper, BytesIO |
|
22 | from io import TextIOWrapper, BytesIO | |
22 |
|
23 | |||
23 | # IPython imports |
|
24 | # IPython imports | |
24 | from IPython.nbconvert.utils.pandoc import pandoc |
|
25 | from IPython.nbconvert.utils.pandoc import pandoc | |
25 | from IPython.nbconvert.utils.exceptions import ConversionException |
|
26 | from IPython.nbconvert.utils.exceptions import ConversionException | |
26 |
from IPython.utils.process import |
|
27 | from IPython.utils.process import get_output_error_code | |
27 | from IPython.utils.py3compat import cast_bytes |
|
28 | from IPython.utils.py3compat import cast_bytes | |
|
29 | from IPython.utils.version import check_version | |||
28 |
|
30 | |||
29 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
30 | # Functions |
|
32 | # Functions | |
31 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
32 | marked = os.path.join(os.path.dirname(__file__), "marked.js") |
|
34 | marked = os.path.join(os.path.dirname(__file__), "marked.js") | |
|
35 | _node = None | |||
33 |
|
36 | |||
34 | __all__ = [ |
|
37 | __all__ = [ | |
35 | 'markdown2html', |
|
38 | 'markdown2html', | |
@@ -61,13 +64,31 def markdown2latex(source): | |||||
61 | """ |
|
64 | """ | |
62 | return pandoc(source, 'markdown', 'latex') |
|
65 | return pandoc(source, 'markdown', 'latex') | |
63 |
|
66 | |||
|
67 | def markdown2html(source): | |||
|
68 | """Convert a markdown string to HTML""" | |||
|
69 | global _node | |||
|
70 | if _node is None: | |||
|
71 | # prefer md2html via marked if node.js >= 0.9.12 is available | |||
|
72 | # node is called nodejs on debian, so try that first | |||
|
73 | _node = 'nodejs' | |||
|
74 | if not _verify_node(_node): | |||
|
75 | _node = 'node' | |||
|
76 | if not _verify_node(_node): | |||
|
77 | warnings.warn( "Node.js 0.9.12 or later wasn't found.\n" + | |||
|
78 | "Nbconvert will try to use Pandoc instead.") | |||
|
79 | _node = False | |||
|
80 | if _node: | |||
|
81 | return markdown2html_marked(source) | |||
|
82 | else: | |||
|
83 | return markdown2html_pandoc(source) | |||
|
84 | ||||
64 | def markdown2html_pandoc(source): |
|
85 | def markdown2html_pandoc(source): | |
65 | """Convert a markdown string to HTML via pandoc""" |
|
86 | """Convert a markdown string to HTML via pandoc""" | |
66 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) |
|
87 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) | |
67 |
|
88 | |||
68 | def markdown2html_marked(source, encoding='utf-8'): |
|
89 | def markdown2html_marked(source, encoding='utf-8'): | |
69 | """Convert a markdown string to HTML via marked""" |
|
90 | """Convert a markdown string to HTML via marked""" | |
70 |
command = [node |
|
91 | command = [_node, marked] | |
71 | try: |
|
92 | try: | |
72 | p = subprocess.Popen(command, |
|
93 | p = subprocess.Popen(command, | |
73 | stdin=subprocess.PIPE, stdout=subprocess.PIPE |
|
94 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |
@@ -99,18 +120,20 def markdown2rst(source): | |||||
99 | """ |
|
120 | """ | |
100 | return pandoc(source, 'markdown', 'rst') |
|
121 | return pandoc(source, 'markdown', 'rst') | |
101 |
|
122 | |||
102 | # prefer md2html via marked if node.js is available |
|
123 | def _verify_node(cmd): | |
103 | # node is called nodejs on debian, so try that first |
|
124 | """Verify that the node command exists and is at least the minimum supported | |
104 | node_cmd = 'nodejs' |
|
125 | version of node. | |
105 | try: |
|
126 | ||
106 | find_cmd(node_cmd) |
|
127 | Parameters | |
107 | except FindCmdError: |
|
128 | ---------- | |
108 | node_cmd = 'node' |
|
129 | cmd : string | |
|
130 | Node command to verify (i.e 'node').""" | |||
109 | try: |
|
131 | try: | |
110 | find_cmd(node_cmd) |
|
132 | out, err, return_code = get_output_error_code([cmd, '--version']) | |
111 |
except |
|
133 | except OSError: | |
112 | markdown2html = markdown2html_pandoc |
|
134 | # Command not found | |
113 | else: |
|
135 | return False | |
114 | markdown2html = markdown2html_marked |
|
136 | if return_code: | |
115 | else: |
|
137 | # Command error | |
116 | markdown2html = markdown2html_marked |
|
138 | return False | |
|
139 | return check_version(out.lstrip('v'), '0.9.12') |
General Comments 0
You need to be logged in to leave comments.
Login now