Merge pull request #6801 from minrk/runMode

use CodeMirror.runMode to highlight in markdown
This commit is contained in:
Thomas Kluyver 2014-11-14 16:52:07 -08:00
commit 17826aa0f9
5 changed files with 60 additions and 43 deletions

View File

@ -11,7 +11,8 @@ define([
'services/sessions/session',
'notebook/js/celltoolbar',
'components/marked/lib/marked',
'highlight',
'codemirror/lib/codemirror',
'codemirror/addon/runmode/runmode',
'notebook/js/mathjaxutils',
'base/js/keyboard',
'notebook/js/tooltip',
@ -29,7 +30,8 @@ define([
session,
celltoolbar,
marked,
hljs,
CodeMirror,
runMode,
mathjaxutils,
keyboard,
tooltip,
@ -83,19 +85,37 @@ define([
marked.setOptions({
gfm : true,
tables: true,
langPrefix: "language-",
highlight: function(code, lang) {
// FIXME: probably want central config for CodeMirror theme when we have js config
langPrefix: "cm-s-ipython language-",
highlight: function(code, lang, callback) {
if (!lang) {
// no language, no highlight
return code;
if (callback) {
callback(null, code);
return;
} else {
return code;
}
}
var highlighted;
try {
highlighted = hljs.highlight(lang, code, false);
} catch(err) {
highlighted = hljs.highlightAuto(code);
}
return highlighted.value;
utils.requireCodeMirrorMode(lang, function () {
var el = document.createElement("div");
mode = CodeMirror.getMode({}, lang);
if (!mode) {
console.log("No CodeMirror mode: " + lang);
callback(null, code);
return;
}
try {
CodeMirror.runMode(code, mode, el);
callback(null, el.innerHTML);
} catch (err) {
console.log("Failed to highlight " + lang + " code", error);
callback(err, code);
}
}, function (err) {
console.log("No CodeMirror mode: " + lang);
callback(err, code);
});
}
});
}

View File

@ -552,9 +552,10 @@ define([
var text_and_math = mathjaxutils.remove_math(markdown);
var text = text_and_math[0];
var math = text_and_math[1];
var html = marked.parser(marked.lexer(text));
html = mathjaxutils.replace_math(html, math);
toinsert.append(html);
marked(text, function (err, html) {
html = mathjaxutils.replace_math(html, math);
toinsert.append(html);
});
element.append(toinsert);
return toinsert;
};

View File

@ -241,34 +241,35 @@ define([
MarkdownCell.prototype.render = function () {
var cont = TextCell.prototype.render.apply(this);
if (cont) {
var that = this;
var text = this.get_text();
var math = null;
if (text === "") { text = this.placeholder; }
var text_and_math = mathjaxutils.remove_math(text);
text = text_and_math[0];
math = text_and_math[1];
var html = marked.parser(marked.lexer(text));
html = mathjaxutils.replace_math(html, math);
html = security.sanitize_html(html);
html = $($.parseHTML(html));
// add anchors to headings
// console.log(html);
html.find(":header").addBack(":header").each(function (i, h) {
h = $(h);
var hash = h.text().replace(/ /g, '-');
h.attr('id', hash);
h.append(
$('<a/>')
.addClass('anchor-link')
.attr('href', '#' + hash)
.text('¶')
);
})
// links in markdown cells should open in new tabs
html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
this.set_rendered(html);
this.typeset();
this.events.trigger("rendered.MarkdownCell", {cell: this})
marked(text, function (err, html) {
html = mathjaxutils.replace_math(html, math);
html = security.sanitize_html(html);
html = $($.parseHTML(html));
// add anchors to headings
html.find(":header").addBack(":header").each(function (i, h) {
h = $(h);
var hash = h.text().replace(/ /g, '-');
h.attr('id', hash);
h.append(
$('<a/>')
.addClass('anchor-link')
.attr('href', '#' + hash)
.text('¶')
);
});
// links in markdown cells should open in new tabs
html.find("a[href]").not('[href^="#"]').attr("target", "_blank");
that.set_rendered(html);
that.typeset();
that.events.trigger("rendered.MarkdownCell", {cell: that});
});
}
return cont;
};

View File

@ -27,7 +27,6 @@
bootstrap: 'components/bootstrap/js/bootstrap.min',
bootstraptour: 'components/bootstrap-tour/build/js/bootstrap-tour.min',
jqueryui: 'components/jquery-ui/ui/minified/jquery-ui.min',
highlight: 'components/highlight.js/build/highlight.pack',
moment: "components/moment/moment",
codemirror: 'components/codemirror',
termjs: "components/term.js/src/term",
@ -52,10 +51,7 @@
jqueryui: {
deps: ["jquery"],
exports: "$"
},
highlight: {
exports: "hljs"
},
}
}
});
</script>

View File

@ -153,7 +153,6 @@ def find_package_data():
pjoin(components, "es6-promise", "*.js"),
pjoin(components, "font-awesome", "fonts", "*.*"),
pjoin(components, "google-caja", "html-css-sanitizer-minified.js"),
pjoin(components, "highlight.js", "build", "highlight.pack.js"),
pjoin(components, "jquery", "jquery.min.js"),
pjoin(components, "jquery-ui", "ui", "minified", "jquery-ui.min.js"),
pjoin(components, "jquery-ui", "themes", "smoothness", "jquery-ui.min.css"),