#! /usr/bin/env node /************************************************************************ * * page2html * * Reads an HTML5 file from stdin that contains math * and writes a new HTML5 document to stdout that * contains CommonHTML versions of the math instead. * * ---------------------------------------------------------------------- * * Copyright (c) 2016 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: page2html [options] < input.html > output.html",{ preview: { boolean: true, describe: "make HTML 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)" }, linebreaks: { boolean: true, describe: "perform automatic line-breaking" }, nodollars: { boolean: true, describe: "don't use single-dollar delimiters" }, 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'" }, fontURL: { default: "https://cdn.mathjax.org/mathjax/latest/fonts/HTML-CSS", describe: "the URL to use for web fonts" } }) .argv; argv.format = argv.format.split(/ *, */); mjAPI.config({extensions: argv.extensions, fontURL: argv.fontURL}); 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: "CommonHTML", 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, linebreaks: argv.linebreaks, xmlns:xmlns }, function (result) { document.body.innerHTML = result.html; document.head.appendChild(document.body.firstChild); var HTML = "\n"+document.documentElement.outerHTML.replace(/^(\n|\s)*/,""); callback(HTML); }); } // // Look up the MathML namespace from the 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); }); });