88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
|
|
var mjAPI = require("./lib/mj-page.js");
|
|
var fs = require('fs');
|
|
var jsdom = require('jsdom').jsdom;
|
|
|
|
mjAPI.start();
|
|
|
|
|
|
var preview = true;
|
|
var speech = true;
|
|
var speechrules = "mathspeak";
|
|
var speechstyle = "default";
|
|
var linebreaks= false;
|
|
var nodollars=false;
|
|
var nofontcache = true;
|
|
var localcache = true;
|
|
var format = ["AsciiMath","TeX","MathML"];
|
|
var eqno = "none";
|
|
var vimg = "SVG";
|
|
var font = "TeX";
|
|
var ex = 6;
|
|
var width = 100;
|
|
|
|
function processHTML(html, callback) {
|
|
console.log("processoHtml = "+html)
|
|
var document = jsdom(html,{features:{FetchExternalResources: false}});
|
|
console.log("afterjsdom")
|
|
var xmlns = getXMLNS(document);
|
|
mjAPI.typeset({
|
|
html: document.body.innerHTML,
|
|
renderer: (vimg == "" ? "SVG" : "IMG"),
|
|
equationNumbers: eqno,
|
|
singleDollars: !nodollars,
|
|
useFontCache: !nofontcache,
|
|
useGlobalCache: !localcache,
|
|
addPreview: preview,
|
|
speakText: speech,
|
|
speakRuleset: speechrules.replace(/^chromevox$/i,"default"),
|
|
speakStyle: speechstyle,
|
|
ex: ex,
|
|
width: width,
|
|
linebreaks: linebreaks,
|
|
xmlns:xmlns
|
|
}, function (result) {
|
|
document.body.innerHTML = result.html;
|
|
document.head.appendChild(document.body.firstChild);
|
|
if (argv.img !== "") {
|
|
var img = document.getElementsByClassName("MathJax_SVG_IMG");
|
|
for (var i = 0, m = img.length; i < m; i++) {
|
|
var N = (i+1).toString(); while (N.length < 4) {N = "0"+N}
|
|
var file = argv.img+N+".svg";
|
|
var svg = [
|
|
'<?xml version="1.0" standalone="no"?>',
|
|
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
|
|
new Buffer(img[i].src.replace(/^.*?,/,""),"base64").toString("utf-8")
|
|
].join("\n");
|
|
fs.writeFileSync(file,svg);
|
|
img[i].src = file;
|
|
}
|
|
}
|
|
var HTML = "<!DOCTYPE html>\n"+document.documentElement.outerHTML.replace(/^(\n|\s)*/,"");
|
|
callback(HTML);
|
|
});
|
|
}
|
|
|
|
function getXMLNS(document) {
|
|
console.log("get XMLNS")
|
|
var html = document.head.parentNode;
|
|
for (var i = 0, m = html.attributes.length; i < m; i++) {
|
|
console.log("XMLNS iter = " + i);
|
|
var attr = html.attributes[i];
|
|
if (attr.nodeName.substr(0,6) === "xmlns:" &&
|
|
attr.nodeValue === "http://www.w3.org/1998/Math/MathML")
|
|
{return attr.nodeName.substr(6)}
|
|
}
|
|
return "mml";
|
|
}
|
|
var html = [];
|
|
process.stdin.on("readable",function (block) {
|
|
var chunk = process.stdin.read();
|
|
if (chunk) html.push(chunk.toString('utf8'));
|
|
});
|
|
process.stdin.on("end",function () {
|
|
processHTML(html.join(""), function(html) {
|
|
process.stdout.write(html);
|
|
});
|
|
});
|