##// END OF EJS Templates
add marked.js entry point for md2html in nbconvert...
MinRK -
Show More
@@ -0,0 +1,54
1 // Node.js script for markdown to html conversion
2 // This applies the same math extraction and marked settings
3 // that we use in the live notebook.
4
5 // IPython static_path dir relative to here:
6 var static_path = __dirname + "/../../html/static/";
7
8 var fs = require('fs');
9 var IPython;
10 // marked can be loaded with require,
11 // the others must be execfiled
12 var marked = require(static_path + 'components/marked/lib/marked.js');
13
14 eval(fs.readFileSync(static_path + "components/highlight.js/build/highlight.pack.js", 'utf8'));
15 eval(fs.readFileSync(static_path + "base/js/namespace.js", 'utf8'));
16
17 eval(fs.readFileSync(static_path + "base/js/utils.js", 'utf8'));
18 eval(fs.readFileSync(static_path + "notebook/js/mathjaxutils.js", 'utf8'));
19
20 // this is copied from notebook.main. Should it be moved somewhere we can reuse it?
21 marked.setOptions({
22 gfm : true,
23 tables: true,
24 langPrefix: "language-",
25 highlight: function(code, lang) {
26 if (!lang) {
27 // no language, no highlight
28 return code;
29 }
30 var highlighted;
31 try {
32 highlighted = hljs.highlight(lang, code, false);
33 } catch(err) {
34 highlighted = hljs.highlightAuto(code);
35 }
36 return highlighted.value;
37 }
38 });
39
40 // read the markdown from stdin
41 var md='';
42 process.stdin.on("data", function (data) {
43 md += data;
44 });
45
46 // perform the md2html transform once stdin is complete
47 process.stdin.on("end", function () {
48 var text_and_math = IPython.mathjaxutils.remove_math(md);
49 var text = text_and_math[0];
50 var math = text_and_math[1];
51 var html = marked.parser(marked.lexer(text));
52 html = IPython.mathjaxutils.replace_math(html, math);
53 process.stdout.write(html);
54 });
@@ -16,6 +16,7 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 os
19 import subprocess
20 import subprocess
20 from io import TextIOWrapper, BytesIO
21 from io import TextIOWrapper, BytesIO
21
22
@@ -28,6 +29,7 from IPython.utils.py3compat import cast_bytes
28 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
29 # Functions
30 # Functions
30 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32 marked = os.path.join(os.path.dirname(__file__), "marked.js")
31
33
32 __all__ = [
34 __all__ = [
33 'markdown2html',
35 'markdown2html',
@@ -37,8 +39,8 __all__ = [
37 'markdown2rst',
39 'markdown2rst',
38 ]
40 ]
39
41
40 class MarkedMissing(ConversionException):
42 class NodeJSMissing(ConversionException):
41 """Exception raised when Marked is missing."""
43 """Exception raised when node.js is missing."""
42 pass
44 pass
43
45
44 def markdown2latex(source):
46 def markdown2latex(source):
@@ -65,16 +67,15 def markdown2html_pandoc(source):
65
67
66 def markdown2html_marked(source, encoding='utf-8'):
68 def markdown2html_marked(source, encoding='utf-8'):
67 """Convert a markdown string to HTML via marked"""
69 """Convert a markdown string to HTML via marked"""
68 command = ['marked', '--gfm', '--tables']
70 command = ['node', marked]
69 try:
71 try:
70 p = subprocess.Popen(command,
72 p = subprocess.Popen(command,
71 stdin=subprocess.PIPE, stdout=subprocess.PIPE
73 stdin=subprocess.PIPE, stdout=subprocess.PIPE
72 )
74 )
73 except OSError as e:
75 except OSError as e:
74 raise MarkedMissing(
76 raise NodeJSMissing(
75 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
77 "The command '%s' returned an error: %s.\n" % (" ".join(command), e) +
76 "Please check that marked is installed:\n" +
78 "Please check that Node.js is installed."
77 " npm install -g marked"
78 )
79 )
79 out, _ = p.communicate(cast_bytes(source, encoding))
80 out, _ = p.communicate(cast_bytes(source, encoding))
80 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
81 out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
@@ -99,7 +100,7 def markdown2rst(source):
99 return pandoc(source, 'markdown', 'rst')
100 return pandoc(source, 'markdown', 'rst')
100
101
101 try:
102 try:
102 find_cmd('marked')
103 find_cmd('node')
103 except FindCmdError:
104 except FindCmdError:
104 markdown2html = markdown2html_pandoc
105 markdown2html = markdown2html_pandoc
105 else:
106 else:
General Comments 0
You need to be logged in to leave comments. Login now