##// END OF EJS Templates
prefer marked to pandoc for markdown2html...
MinRK -
Show More
@@ -16,10 +16,14 b' markdown within Jinja templates.'
16 from __future__ import print_function
16 from __future__ import print_function
17
17
18 # Stdlib imports
18 # Stdlib imports
19 import sys
20 import subprocess
19 import subprocess
20 from io import TextIOWrapper, BytesIO
21
21
22 # IPython imports
22 from IPython.nbconvert.utils.pandoc import pandoc
23 from IPython.nbconvert.utils.pandoc import pandoc
24 from IPython.nbconvert.utils.exceptions import ConversionException
25 from IPython.utils.process import find_cmd, FindCmdError
26 from IPython.utils.py3compat import cast_bytes
23
27
24 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
25 # Functions
29 # Functions
@@ -27,10 +31,16 b' from IPython.nbconvert.utils.pandoc import pandoc'
27
31
28 __all__ = [
32 __all__ = [
29 'markdown2html',
33 'markdown2html',
34 'markdown2html_pandoc',
35 'markdown2html_marked',
30 'markdown2latex',
36 'markdown2latex',
31 'markdown2rst'
37 'markdown2rst',
32 ]
38 ]
33
39
40 class MarkedMissing(ConversionException):
41 """Exception raised when Marked is missing."""
42 pass
43
34 def markdown2latex(source):
44 def markdown2latex(source):
35 """Convert a markdown string to LaTeX via pandoc.
45 """Convert a markdown string to LaTeX via pandoc.
36
46
@@ -49,11 +59,27 b' def markdown2latex(source):'
49 """
59 """
50 return pandoc(source, 'markdown', 'latex')
60 return pandoc(source, 'markdown', 'latex')
51
61
52
62 def markdown2html_pandoc(source):
53 def markdown2html(source):
54 """Convert a markdown string to HTML via pandoc"""
63 """Convert a markdown string to HTML via pandoc"""
55 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
64 return pandoc(source, 'markdown', 'html', extra_args=['--mathjax'])
56
65
66 def markdown2html_marked(source, encoding='utf-8'):
67 """Convert a markdown string to HTML via marked"""
68 command = ['marked', '--gfm', '--tables']
69 try:
70 p = subprocess.Popen(command,
71 stdin=subprocess.PIPE, stdout=subprocess.PIPE
72 )
73 except OSError as e:
74 raise MarkedMissing(
75 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
76 "Please check that marked is installed:\n" +
77 " npm install -g marked"
78 )
79 out, _ = p.communicate(cast_bytes(source, encoding))
80 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
81 return out.rstrip('\n')
82
57 def markdown2rst(source):
83 def markdown2rst(source):
58 """Convert a markdown string to LaTeX via pandoc.
84 """Convert a markdown string to LaTeX via pandoc.
59
85
@@ -72,3 +98,9 b' def markdown2rst(source):'
72 """
98 """
73 return pandoc(source, 'markdown', 'rst')
99 return pandoc(source, 'markdown', 'rst')
74
100
101 try:
102 find_cmd('marked')
103 except FindCmdError:
104 markdown2html = markdown2html_pandoc
105 else:
106 markdown2html = markdown2html_marked
General Comments 0
You need to be logged in to leave comments. Login now