You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.1 KiB
174 lines
5.1 KiB
#! /usr/bin/env node
|
|
|
|
/************************************************************************
|
|
*
|
|
* page2svg
|
|
*
|
|
* Reads an HTML5 file from stdin that contains math
|
|
* and writes a new HTML5 document to stdout that
|
|
* contains SVG 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: page2svg [options] < input.html > output.html",{
|
|
preview: {
|
|
boolean: true,
|
|
describe: "make SVG 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"
|
|
},
|
|
nofontcache: {
|
|
boolean: true,
|
|
describe: "don't use <defs> and <use> tags for fonts"
|
|
},
|
|
localcache: {
|
|
boolean: true,
|
|
describe: "cache fonts for each equation separately"
|
|
},
|
|
format: {
|
|
default: "AsciiMath,TeX,MathML",
|
|
describe: "input format(s) to look for"
|
|
},
|
|
eqno: {
|
|
default: "none",
|
|
describe: "equation number style (none, AMS, or all)"
|
|
},
|
|
img: {
|
|
default: "",
|
|
describe: "make external svg images with this name prefix"
|
|
},
|
|
font: {
|
|
default: "TeX",
|
|
describe: "web font to use"
|
|
},
|
|
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(/ *, */);
|
|
if (argv.font === "STIX") argv.font = "STIX-Web";
|
|
mjAPI.config({MathJax: {SVG: {font: argv.font}}, 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: (argv.img == "" ? "SVG" : "IMG"),
|
|
inputs: argv.format,
|
|
equationNumbers: argv.eqno,
|
|
singleDollars: !argv.nodollars,
|
|
useFontCache: !argv.nofontcache,
|
|
useGlobalCache: !argv.localcache,
|
|
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);
|
|
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);
|
|
});
|
|
}
|
|
|
|
//
|
|
// 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.name.substr(0,6) === "xmlns:" &&
|
|
attr.value === "http://www.w3.org/1998/Math/MathML")
|
|
{return attr.name.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);
|
|
});
|
|
});
|