Website/node_modules/mathjax-node/bin/tex2svg-filter

148 lines
4 KiB
JavaScript
Executable file

#! /usr/bin/env node
/****************************************
*
* tex2svg-filter "prefix"
*
* Reads stdin for TeX commands and produces SVG files
* for each expression read.
*
* An expression ends when a blank line is read.
*
* The file names are the prefix followed by a number
* where the number is incremented for each expression.
*
* ----------------------------------------------------------------------
*
* 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-single.js");
var readline = require('readline');
var fs = require('fs');
var argv = require("yargs")
.demand(1).strict()
.usage("Usage: tex2svg-filter [options] 'SVG-file-prefix'",{
linebreaks: {
boolean: true,
describe: "perform automatic line-breaking"
},
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)"
},
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;
if (argv.font === "STIX") argv.font = "STIX-Web";
mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions});
mjAPI.start();
var prefix = argv._[0]; // the file prefix
//
// Create interface for reading lines from stdin
//
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
//
// The accumulated TeX code, and file number
//
var tex = [], n = 0;
//
// Data passed to mj-single-svg to maintain state
//
var state = {};
//
// Save an SVG file:
// Increment the file number and create the file name.
// Set the math to be the combined TeX lines (and clear them).
// Remove dollar signs and set the format.
// Typeset the math, and when finished:
// if there is svg output,
// write the output file,
// and indicate that it is written.
//
function SaveSVG() {
if (tex.length === 0) return;
var data = {
math:"", state:state,
format: "TeX",
svg:true,
ex:argv.ex, width:argv.width,
linebreaks: argv.linebreaks,
speakText: argv.speech,
speakRuleset: argv.speechrules.replace(/^chromevox$/i,"default"),
speakStyle: argv.speechstyle
};
n++; var N = n; while (N.length < 3) {N = "0"+N}
var file = prefix+N+".svg";
var math = tex.join("\n"); tex = [];
if (math.slice(0,2) === "$$" && math.slice(-2) === "$$") {
math = math.slice(2,math.length-2);
} else if (math.slice(0,1) === "$" && math.slice(-1) === "$") {
math = math.slice(1,math.length-1);
data.format = "inline-TeX";
}
data.math = math;
mjAPI.typeset(data,function (result) {
if (result.svg) {
fs.writeFileSync(file,result.svg);
console.log(file+" written");
}
});
}
//
// For each line read from stdin:
// If the line is not empty,
// add it to the list of TeX lines,
// Otherwise
// Save the SVG output.
//
rl.on("line",function (line) {if (line.match(/\S/)) {tex.push(line)} else {SaveSVG()}});
rl.on("close",function () {SaveSVG()});