Show More
@@ -18,6 +18,7 b' 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 | |
@@ -31,6 +32,7 b' from IPython.utils.version import check_version' | |||||
31 | # Functions |
|
32 | # Functions | |
32 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
33 | marked = os.path.join(os.path.dirname(__file__), "marked.js") |
|
34 | marked = os.path.join(os.path.dirname(__file__), "marked.js") | |
|
35 | _node = None | |||
34 |
|
36 | |||
35 | __all__ = [ |
|
37 | __all__ = [ | |
36 | 'markdown2html', |
|
38 | 'markdown2html', | |
@@ -62,13 +64,31 b' def markdown2latex(source):' | |||||
62 | """ |
|
64 | """ | |
63 | return pandoc(source, 'markdown', 'latex') |
|
65 | return pandoc(source, 'markdown', 'latex') | |
64 |
|
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 not _node: | |||
|
81 | return markdown2html_pandoc(source) | |||
|
82 | else: | |||
|
83 | return markdown2html_marked(source) | |||
|
84 | ||||
65 | def markdown2html_pandoc(source): |
|
85 | def markdown2html_pandoc(source): | |
66 | """Convert a markdown string to HTML via pandoc""" |
|
86 | """Convert a markdown string to HTML via pandoc""" | |
67 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) |
|
87 | return pandoc(source, 'markdown', 'html', extra_args=['--mathjax']) | |
68 |
|
88 | |||
69 | def markdown2html_marked(source, encoding='utf-8'): |
|
89 | def markdown2html_marked(source, encoding='utf-8'): | |
70 | """Convert a markdown string to HTML via marked""" |
|
90 | """Convert a markdown string to HTML via marked""" | |
71 |
command = [node |
|
91 | command = [_node, marked] | |
72 | try: |
|
92 | try: | |
73 | p = subprocess.Popen(command, |
|
93 | p = subprocess.Popen(command, | |
74 | stdin=subprocess.PIPE, stdout=subprocess.PIPE |
|
94 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |
@@ -117,15 +137,3 b' def _verify_node(cmd):' | |||||
117 | # Command error |
|
137 | # Command error | |
118 | return False |
|
138 | return False | |
119 | return check_version(out.lstrip('v'), '0.9.12') |
|
139 | return check_version(out.lstrip('v'), '0.9.12') | |
120 |
|
||||
121 | # prefer md2html via marked if node.js >= 0.9.12 is available |
|
|||
122 | # node is called nodejs on debian, so try that first |
|
|||
123 | node_cmd = 'nodejs' |
|
|||
124 | if _verify_node(node_cmd): |
|
|||
125 | markdown2html = markdown2html_marked |
|
|||
126 | else: |
|
|||
127 | node_cmd = 'node' |
|
|||
128 | if _verify_node(node_cmd): |
|
|||
129 | markdown2html = markdown2html_marked |
|
|||
130 | else: |
|
|||
131 | markdown2html = markdown2html_pandoc |
|
General Comments 0
You need to be logged in to leave comments.
Login now