Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:w3c/respec into develop
Browse files Browse the repository at this point in the history
* 'develop' of github.com:w3c/respec:
  Feat(hightlight): use new highlighter (#767)
  • Loading branch information
marcoscaceres committed May 20, 2016
2 parents bd7be86 + 37e5a66 commit 8a34619
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1,703 deletions.
77 changes: 37 additions & 40 deletions js/core/highlight.js
@@ -1,46 +1,43 @@

// Module core/highlight
// Does syntax highlighting to all pre and code that have a class of "highlight"

// A potential improvement would be to call cb() immediately and benefit from the asynchronous
// ability of prettyPrint() (but only call msg.pub() in the callback to remain accurate as to
// the end of processing)

// An improvement would be to use web workers to do the highlighting.
"use strict";
define(
["text!core/css/highlight.css", "google-code-prettify"],
function (css, PR) {
return {
run: function (conf, doc, cb, msg) {
msg.pub("start", "core/highlight");
[
"core/utils",
"highlight",
"text!highlightStyles/github.css",
],
function(utils, hljs, ghCss) {
// Opportunistically insert the style into the head to reduce FOUC.
var codeStyle = document.createElement("style");
codeStyle.textContent = ghCss;
var swapStyleOwner = utils.makeOwnerSwapper(codeStyle);
swapStyleOwner(document, document.head);
return {
run: function(conf, doc, cb, msg) {
// Nothing to do
if (conf.noHighlightCSS) {
return cb();
}

if (codeStyle.ownerDocument !== doc) {
swapStyleOwner(doc, doc.head);
}

// fix old classes
var oldies = "sh_css sh_html sh_javascript sh_javascript_dom sh_xml".split(" ");
for (var i = 0, n = oldies.length; i < n; i++) {
var old = oldies[i];
$("." + old).each(function () {
$(this).removeClass(old).addClass("highlight");
msg.pub("warn", "Old highlighting class '" + old + "', use 'highlight' instead.");
});
}
if (doc.querySelector("highlight")) {
msg.pub("warn", "pre elements don't need a 'highlight' class anymore.");
}

// prettify
var $highs = $("pre.highlight, code.highlight")
, done = function () {
msg.pub("end", "core/highlight");
cb();
}
;
if ($highs.length) {
if (!conf.noHighlightCSS) {
$(doc).find("head link").first().before($("<style/>").text(css));
}
$highs.addClass("prettyprint");
PR.prettyPrint(done);
}
else {
done();
}
}
};
}
Array
.from(
doc.querySelectorAll("pre")
)
.forEach(function(element) {
hljs.highlightBlock(element);
});
cb();
}
};
}
);

0 comments on commit 8a34619

Please sign in to comment.