easy-mathjax
Version:
Facilitate MathJax usage.
178 lines (161 loc) • 5.26 kB
JavaScript
(function() {
var $$, EasyMathJax, extend, remove,
__slice = [].slice,
__hasProp = {}.hasOwnProperty;
$$ = function(selector) {
var el, _i, _len, _ref, _results;
_ref = document.querySelectorAll(selector);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
_results.push(el);
}
return _results;
};
remove = function(node) {
var n, _i, _len, _ref, _results;
if (typeof node === "string") {
node = $$(node);
}
node = [].concat(node);
_ref = [].concat(node);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
n = _ref[_i];
if (n && (n.parentNode != null)) {
_results.push(n.parentNode.removeChild(n));
} else {
_results.push(void 0);
}
}
return _results;
};
extend = function() {
var base, ext, extenders, key, value, _i, _len;
base = arguments[0], extenders = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (typeof base === "object") {
for (_i = 0, _len = extenders.length; _i < _len; _i++) {
ext = extenders[_i];
if (typeof ext === "object") {
for (key in ext) {
if (!__hasProp.call(ext, key)) continue;
value = ext[key];
base[key] = value;
}
}
}
}
return base;
};
EasyMathJax = (function() {
var defaults, matchFontUrl;
matchFontUrl = /url\s*\(\s*['"]([^\)]+fonts\/HTML\-CSS\/([^\/]+\/(?:woff|otf|eot)\/[^-]+\-(?:Regular|Italic|Bold|Bolditalic)\.(?:woff|eot|otf)))[^'"]*['"]\s*\)/g;
EasyMathJax.defaults = defaults = {
url: "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
};
function EasyMathJax(options) {
this.options = extend({}, EasyMathJax.defaults, options);
}
EasyMathJax.prototype.injected = function() {
var el, injected;
el = this.script;
injected = [];
while (el) {
injected.push(el);
el = el.nextElementSibling;
}
return injected;
};
EasyMathJax.prototype.clean = function() {
var s, _i, _len, _ref, _ref1;
remove(this.injected());
remove("#MathJax_Message");
remove("#MathJax_Font_Test");
remove(".MathJax_Preview");
remove((_ref = document.getElementById("MathJax_Hidden")) != null ? _ref.parentNode : void 0);
if (!this.options.debug) {
remove("script[id^=MathJax]");
_ref1 = $$("[id^='MathJax']");
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
s = _ref1[_i];
s.removeAttribute('id');
}
}
return this;
};
EasyMathJax.prototype.css = function() {
var css, el, fixFontUrl, fonts, line, parseLine, _i, _j, _len, _len1, _ref, _ref1;
fonts = {};
css = [];
fixFontUrl = function(line) {
return line.replace(matchFontUrl, function(ma, url, font) {
fonts[font] = url;
return "url('fonts/mathjax/" + font + "')";
});
};
parseLine = function(line) {
if (line.match(/@font\-face/)) {
line = fixFontUrl(line);
}
return css.push(line);
};
_ref = this.injected();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
if ((el != null ? el.tagName.toLowerCase() : void 0) === "style") {
_ref1 = el.innerHTML.split("\n");
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
line = _ref1[_j];
parseLine(line);
}
}
}
return {
contents: css.join("\n"),
fonts: fonts
};
};
EasyMathJax.prototype.inject = function(callback) {
window.MathJax = extend({}, window.MathJax, this.options.config, {
delayStartupUntil: "configured",
skipStartupTypeset: true
});
this.script = document.createElement('script');
this.script.addEventListener("load", (function(_this) {
return function() {
if (_this.options.debug) {
window.MathJax.Hub.Startup.signal.Interest(function(message) {
return console.log("Startup: " + message);
});
window.MathJax.Hub.signal.Interest(function(message) {
return console.log("Hub: " + message);
});
}
window.MathJax.Hub.Register.StartupHook("End", function() {
if (typeof callback === "function") {
return callback();
}
});
return window.MathJax.Hub.Configured();
};
})(this));
this.script.src = this.options.url;
document.head.appendChild(this.script);
return this;
};
EasyMathJax.prototype.render = function(selector, callback) {
var el, _i, _len, _ref;
_ref = $$(selector);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub, el]);
}
if (typeof callback === "function") {
window.MathJax.Hub.Queue(callback);
}
return this;
};
return EasyMathJax;
})();
this.EasyMathJax = EasyMathJax;
}).call(this);