Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:w3c/respec into develop
* 'develop' of github.com:w3c/respec: Feat(hightlight): use new highlighter (#767)
- Loading branch information
Showing
5 changed files
with
75 additions
and
1,703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}; | ||
} | ||
); |
Oops, something went wrong.