Website/node_modules/mathjax-node/bin/page2mml

140 lines
3.9 KiB
JavaScript
Executable file

#! /usr/bin/env node
/************************************************************************
*
* page2mml
*
* Reads an HTML5 file from stdin that contains math
* and writes a new HTML5 document to stdout that
* contains MathML versions of the math instead.
*
* ----------------------------------------------------------------------
*
* Copyright (c) 2014 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var mjAPI = require("../lib/mj-page.js");
var fs = require('fs');
var jsdom = require('jsdom').jsdom;
var argv = require("yargs")
.strict()
.usage("Usage: page2mml [options] < input.html > output.html",{
preview: {
boolean: true,
describe: "make MathML into a Mathjax preview"
},
speech: {
boolean: true,
describe: "include speech text"
},
speechrules: {
default: "mathspeak",
describe: "ruleset to use for speech text (chromevox or mathspeak)"
},
speechstyle: {
default: "default",
describe: "style to use for speech text (default, brief, sbrief)"
},
nodollars: {
boolean: true,
describe: "don't use single-dollar delimiters"
},
semantics: {
boolean: true,
describe: "add TeX or AsciiMath code in <semantics> tag"
},
format: {
default: "AsciiMath,TeX,MathML",
describe: "input format(s) to look for"
},
eqno: {
default: "none",
describe: "equation number style (none, AMS, or all)"
},
ex: {
default: 6,
describe: "ex-size in pixels"
},
width: {
default: 100,
describe: "width of container in ex"
},
extensions: {
default: "",
describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'"
}
})
.argv;
argv.format = argv.format.split(/ *, */);
mjAPI.config({MathJax: {menuSettings: {semantics: argv.semantics}}, extensions: argv.extensions});
mjAPI.start();
//
// Process an HTML file:
//
function processHTML(html,callback) {
var document = jsdom(html,{features:{FetchExternalResources: false}});
var xmlns = getXMLNS(document);
mjAPI.typeset({
html:document.body.innerHTML,
renderer:"NativeMML",
inputs: argv.format,
equationNumbers: argv.eqno,
singleDollars: !argv.nodollars,
addPreview: argv.preview,
speakText: argv.speech,
speakRuleset: argv.speechrules.replace(/^chromevox$/i,"default"),
speakStyle: argv.speechstyle,
ex: argv.ex, width: argv.width,
xmlns:xmlns
}, function (result) {
document.body.innerHTML = result.html;
var HTML = "<!DOCTYPE html>\n"+document.documentElement.outerHTML.replace(/^(\n|\s)*/,"");
callback(HTML);
});
}
//
// Look up the MathML namespace from the <html> attributes
//
function getXMLNS(document) {
var html = document.head.parentNode;
for (var i = 0, m = html.attributes.length; i < m; 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";
}
//
// Read the input file and collect the file contents
// When done, process the HTML.
//
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);
});
});