From 524458c9f79ac0fb0753d4ab408c2e409d34e7c0 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Fri, 13 Feb 2015 10:40:51 +0000 Subject: [PATCH] Allow extensions to be specified on command line Adds an 'extensions' option to config which takes a comma-separated list of extensions to add to the default set. Then adds --extensions parameter to each command-line tool to specify this value. This allows use of MathJax features such as displaying invalid TeX instead of failing to render (--extensions TeX/noUndefined), and stripping out dangerous links from MathML (--extensions Safe). --- bin/am2mml | 7 ++++++- bin/am2png | 6 +++++- bin/am2svg | 6 +++++- bin/mml2mml | 6 +++++- bin/mml2png | 6 +++++- bin/mml2svg | 6 +++++- bin/mml2svg-html5 | 6 +++++- bin/page2mml | 6 +++++- bin/page2png | 6 +++++- bin/page2svg | 6 +++++- bin/tex2mml | 7 ++++++- bin/tex2png | 6 +++++- bin/tex2svg | 6 +++++- bin/tex2svg-filter | 6 +++++- lib/mj-page.js | 18 ++++++++++++++++-- lib/mj-single.js | 20 +++++++++++++++++--- 16 files changed, 105 insertions(+), 19 deletions(-) diff --git a/bin/am2mml b/bin/am2mml index 1ebcc0ec..71bcd43c 100755 --- a/bin/am2mml +++ b/bin/am2mml @@ -43,6 +43,10 @@ var argv = require("yargs") semantics: { boolean: true, describe: "add AsciiMath code in tag" + }, + extensions: { + default: "", + describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'" } }) .argv; @@ -52,7 +56,8 @@ mjAPI.config({ menuSettings: { semantics: argv.semantics } - } + }, + extensions: argv.extensions }); mjAPI.start(); diff --git a/bin/am2png b/bin/am2png index 05af7a39..0328d6c4 100755 --- a/bin/am2png +++ b/bin/am2png @@ -48,12 +48,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); if (argv.dpi === 0) {argv.dpi = argv.ex * 16} // pixels properly sized diff --git a/bin/am2svg b/bin/am2svg index 25916f67..e9f7b7dc 100755 --- a/bin/am2svg +++ b/bin/am2svg @@ -55,12 +55,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); mjAPI.typeset({ diff --git a/bin/mml2mml b/bin/mml2mml index 45e6d794..33daeb0d 100755 --- a/bin/mml2mml +++ b/bin/mml2mml @@ -40,11 +40,15 @@ var argv = require("yargs") speechstyle: { default: "default", describe: "style to use for speech text (default, brief, sbrief)" + }, + extensions: { + default: "", + describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'" } }) .argv; -mjAPI.config({}); +mjAPI.config({extensions: argv.extensions}); mjAPI.start(); mjAPI.typeset({ diff --git a/bin/mml2png b/bin/mml2png index 4e67e14a..d97471db 100755 --- a/bin/mml2png +++ b/bin/mml2png @@ -47,12 +47,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); if (argv.dpi === 0) {argv.dpi = argv.ex * 16} // pixels properly sized diff --git a/bin/mml2svg b/bin/mml2svg index ee7e2af6..fd7348b9 100755 --- a/bin/mml2svg +++ b/bin/mml2svg @@ -55,12 +55,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); mjAPI.typeset({ diff --git a/bin/mml2svg-html5 b/bin/mml2svg-html5 index 7592631c..6788d7aa 100755 --- a/bin/mml2svg-html5 +++ b/bin/mml2svg-html5 @@ -63,12 +63,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); // diff --git a/bin/page2mml b/bin/page2mml index 6ff2e4b4..3ceec26c 100755 --- a/bin/page2mml +++ b/bin/page2mml @@ -71,13 +71,17 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {menuSettings: {semantics: argv.semantics}}, extensions: argv.extensions}); mjAPI.start(); // diff --git a/bin/page2png b/bin/page2png index f9aef1d7..4b526d2a 100755 --- a/bin/page2png +++ b/bin/page2png @@ -83,13 +83,17 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); if (argv.dpi === 0) {argv.dpi = argv.ex * 16} // pixels properly sized diff --git a/bin/page2svg b/bin/page2svg index bd966af5..95de942f 100755 --- a/bin/page2svg +++ b/bin/page2svg @@ -87,13 +87,17 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); // diff --git a/bin/tex2mml b/bin/tex2mml index 029265bd..e6a3f597 100755 --- a/bin/tex2mml +++ b/bin/tex2mml @@ -51,6 +51,10 @@ var argv = require("yargs") notexhints: { boolean: true, describe: "don't add TeX-specific classes" + }, + extensions: { + default: "", + describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'" } }) .argv; @@ -61,7 +65,8 @@ mjAPI.config({ semantics: argv.semantics, texHints: !argv.notexhints } - } + }, + extensions: argv.extensions }); mjAPI.start(); diff --git a/bin/tex2png b/bin/tex2png index 213b679c..1129d4ad 100755 --- a/bin/tex2png +++ b/bin/tex2png @@ -52,12 +52,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); if (argv.dpi === 0) {argv.dpi = argv.ex * 16} // pixels properly sized diff --git a/bin/tex2svg b/bin/tex2svg index de69e79c..30475576 100755 --- a/bin/tex2svg +++ b/bin/tex2svg @@ -59,12 +59,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); mjAPI.typeset({ diff --git a/bin/tex2svg-filter b/bin/tex2svg-filter index e103475a..19c6db46 100755 --- a/bin/tex2svg-filter +++ b/bin/tex2svg-filter @@ -63,12 +63,16 @@ var argv = require("yargs") 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}}}); +mjAPI.config({MathJax: {SVG: {font: argv.font}}, extensions: argv.extensions}); mjAPI.start(); var prefix = argv._[0]; // the file prefix diff --git a/lib/mj-page.js b/lib/mj-page.js index c84794ae..acf8a37e 100644 --- a/lib/mj-page.js +++ b/lib/mj-page.js @@ -36,6 +36,7 @@ var speech = require('speech-rule-engine'); var displayMessages = false; // don't log Message.Set() calls var displayErrors = true; // show error messages on the console var undefinedChar = false; // unknown characters are not saved in the error array +var extensions = ''; // no additional extensions used var defaults = { ex: 6, // ex-size in pixels @@ -341,6 +342,16 @@ function ConfigureMathJax() { } }; + if (extensions) { + // + // Parse added extensions list and add to standard ones + // + var extensionList = extensions.split(/s*,\s*/); + for (var i = 0; i < extensionList.length; i++) { + var matches = extensionList[i].match(/^(.*?)(\.js)?$/); + window.MathJax.extensions.push(matches[1] + '.js'); + } + } if (MathJaxConfig) {Insert(window.MathJax,MathJaxConfig)} } @@ -797,17 +808,20 @@ exports.start = function () {RestartMathJax()} // Configure MathJax and the API // You can pass additional configuration options to MathJax using the // MathJax property, and can set displayErrors and displayMessages -// that control the display of error messages +// that control the display of error messages, and extensions to add +// additional MathJax extensions to the base or to sub-categories. // // E.g. -// mjAPI.congif({ +// mjAPI.config({ // MathJax: {SVG: {font: "STIX-Web"}}, // displayErrors: false +// extensions: 'Safe,TeX/noUndefined' // }); // exports.config = function (config) { if (config.displayMessages != null) {displayMessages = config.displayMessages} if (config.displayErrors != null) {displayErrors = config.displayErrors} if (config.undefinedCharError != null) {undefinedChar = config.undefinedCharError} + if (config.extensions != null) {extensions = config.extensions} if (config.MathJax) {MathJaxConfig = config.MathJax} } diff --git a/lib/mj-single.js b/lib/mj-single.js index 74b09fab..268cf7d2 100644 --- a/lib/mj-single.js +++ b/lib/mj-single.js @@ -37,6 +37,7 @@ var speech = require('speech-rule-engine'); var displayMessages = false; // don't log Message.Set() calls var displayErrors = true; // show error messages on the console var undefinedChar = false; // unknown characters are not saved in the error array +var extensions = ''; // no additional extensions used var defaults = { ex: 6, // ex-size in pixels @@ -315,6 +316,16 @@ function ConfigureMathJax() { } }; + if (extensions) { + // + // Parse added extensions list and add to standard ones + // + var extensionList = extensions.split(/s*,\s*/); + for (var i = 0; i < extensionList.length; i++) { + var matches = extensionList[i].match(/^(.*?)(\.js)?$/); + window.MathJax.extensions.push(matches[1] + '.js'); + } + } if (MathJaxConfig) {Insert(window.MathJax,MathJaxConfig)} } @@ -660,17 +671,20 @@ exports.start = function () {RestartMathJax()} // Configure MathJax and the API // You can pass additional configuration options to MathJax using the // MathJax property, and can set displayErrors and displayMessages -// that control the display of error messages +// that control the display of error messages, and extensions to add +// additional MathJax extensions to the base or to sub-categories. // // E.g. -// mjAPI.congif({ +// mjAPI.config({ // MathJax: {SVG: {font: "STIX-Web"}}, -// displayErrors: false +// displayErrors: false, +// extensions: 'Safe,TeX/noUndefined' // }); // exports.config = function (config) { if (config.displayMessages != null) {displayMessages = config.displayMessages} if (config.displayErrors != null) {displayErrors = config.displayErrors} if (config.undefinedCharError != null) {undefinedChar = config.undefinedCharError} + if (config.extensions != null) {extensions = config.extensions} if (config.MathJax) {MathJaxConfig = config.MathJax} }