|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +/************************************************************************ |
| 4 | + * |
| 5 | + * page2svg |
| 6 | + * |
| 7 | + * Reads an HTML5 file from stdin that contains math |
| 8 | + * and writes a new HTML5 document to stdout that |
| 9 | + * contains SVG versions of the math instead. |
| 10 | + * |
| 11 | + * ---------------------------------------------------------------------- |
| 12 | + * |
| 13 | + * Copyright (c) 2014 The MathJax Consortium |
| 14 | + * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | + * you may not use this file except in compliance with the License. |
| 17 | + * You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, software |
| 22 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | + * See the License for the specific language governing permissions and |
| 25 | + * limitations under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +var mjAPI = require("../lib/mj-page.js"); |
| 29 | +var fs = require('fs'); |
| 30 | +var jsdom = require('jsdom').jsdom; |
| 31 | + |
| 32 | +var argv = require("yargs") |
| 33 | + .strict() |
| 34 | + .usage("Usage: page2svg [options] < input.html > output.html",{ |
| 35 | + preview: { |
| 36 | + boolean: true, |
| 37 | + describe: "make SVG into a MathJax preview" |
| 38 | + }, |
| 39 | + speech: { |
| 40 | + boolean: true, |
| 41 | + describe: "include speech text" |
| 42 | + }, |
| 43 | + speechrules: { |
| 44 | + default: "mathspeak", |
| 45 | + describe: "ruleset to use for speech text (chromevox or mathspeak)" |
| 46 | + }, |
| 47 | + speechstyle: { |
| 48 | + default: "default", |
| 49 | + describe: "style to use for speech text (default, brief, sbrief)" |
| 50 | + }, |
| 51 | + linebreaks: { |
| 52 | + boolean: true, |
| 53 | + describe: "perform automatic line-breaking" |
| 54 | + }, |
| 55 | + nodollars: { |
| 56 | + boolean: true, |
| 57 | + describe: "don't use single-dollar delimiters" |
| 58 | + }, |
| 59 | + format: { |
| 60 | + default: "AsciiMath,TeX,MathML", |
| 61 | + describe: "input format(s) to look for" |
| 62 | + }, |
| 63 | + eqno: { |
| 64 | + default: "none", |
| 65 | + describe: "equation number style (none, AMS, or all)" |
| 66 | + }, |
| 67 | + ex: { |
| 68 | + default: 6, |
| 69 | + describe: "ex-size in pixels" |
| 70 | + }, |
| 71 | + width: { |
| 72 | + default: 100, |
| 73 | + describe: "width of container in ex" |
| 74 | + }, |
| 75 | + extensions: { |
| 76 | + default: "", |
| 77 | + describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'" |
| 78 | + } |
| 79 | + }) |
| 80 | + .argv; |
| 81 | + |
| 82 | +argv.format = argv.format.split(/ *, */); |
| 83 | +mjAPI.config({extensions: argv.extensions}); |
| 84 | +mjAPI.start(); |
| 85 | + |
| 86 | +// |
| 87 | +// Process an HTML file: |
| 88 | +// |
| 89 | +function processHTML(html,callback) { |
| 90 | + var document = jsdom(html,{features:{FetchExternalResources: false}}); |
| 91 | + var xmlns = getXMLNS(document); |
| 92 | + mjAPI.typeset({ |
| 93 | + html: document.body.innerHTML, |
| 94 | + renderer: "CommonHTML", |
| 95 | + inputs: argv.format, |
| 96 | + equationNumbers: argv.eqno, |
| 97 | + singleDollars: !argv.nodollars, |
| 98 | + addPreview: argv.preview, |
| 99 | + speakText: argv.speech, |
| 100 | + speakRuleset: argv.speechrules.replace(/^chromevox$/i,"default"), |
| 101 | + speakStyle: argv.speechstyle, |
| 102 | + ex: argv.ex, width: argv.width, |
| 103 | + linebreaks: argv.linebreaks, |
| 104 | + xmlns:xmlns |
| 105 | + }, function (result) { |
| 106 | + document.body.innerHTML = result.html; |
| 107 | + document.head.appendChild(document.body.firstChild); |
| 108 | + var HTML = "<!DOCTYPE html>\n"+document.documentElement.outerHTML.replace(/^(\n|\s)*/,""); |
| 109 | + callback(HTML); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +// |
| 114 | +// Look up the MathML namespace from the <html> attributes |
| 115 | +// |
| 116 | +function getXMLNS(document) { |
| 117 | + var html = document.head.parentNode; |
| 118 | + for (var i = 0, m = html.attributes.length; i < m; i++) { |
| 119 | + var attr = html.attributes[i]; |
| 120 | + if (attr.nodeName.substr(0,6) === "xmlns:" && |
| 121 | + attr.nodeValue === "http://www.w3.org/1998/Math/MathML") |
| 122 | + {return attr.nodeName.substr(6)} |
| 123 | + } |
| 124 | + return "mml"; |
| 125 | +} |
| 126 | + |
| 127 | +// |
| 128 | +// Read the input file and collect the file contents |
| 129 | +// When done, process the HTML. |
| 130 | +// |
| 131 | +var html = []; |
| 132 | +process.stdin.on("readable",function (block) { |
| 133 | + var chunk = process.stdin.read(); |
| 134 | + if (chunk) html.push(chunk.toString('utf8')); |
| 135 | +}); |
| 136 | +process.stdin.on("end",function () { |
| 137 | + processHTML(html.join(""), function(html) { |
| 138 | + process.stdout.write(html); |
| 139 | + }); |
| 140 | +}); |
0 commit comments