-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgofmml.js
108 lines (103 loc) · 2.21 KB
/
svgofmml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /usr/bin/env node
/*************************************************************************
*
* svgofxml
*
* Uses MathJax to convert a MathML XML to SVG.
*
* ----------------------------------------------------------------------
*/
var mjAPI = require('mathjax-node-sre');
var argv = require('yargs')
.strict()
.usage('$0 [options] xmlfile > file.svg')
.options({
linebreaks: {
boolean: false,
describe: 'perform automatic line-breaking',
},
speech: {
boolean: true,
default: true,
describe: 'include speech text',
},
font: {
default: 'TeX',
describe: 'web font to use',
},
ex: {
default: 6,
describe: 'ex-size in pixels',
},
width: {
default: 0,
describe: 'width of container in ex',
},
extensions: {
default: '',
describe: "extra MathJax extensions e.g. 'Safe,TeX/noUndefined'",
},
out: {
default: '',
describe: 'write to file instead of stdout',
},
}).argv;
fs = require('fs');
function proc(err, data) {
if (err) {
return console.log(err);
} else if (data.test(/\s*<\s*\?/)) {
// } else {
// mathjax does not like pre proc
JSDOM = require('jsdom');
const DOMParser = new JSDOM.JSDOM('').window.DOMParser;
data = new DOMParser().parseFromString(data, 'text/xml').documentElement.outerHTML;
}
let conf = {
math: data,
format: 'MathML',
svg: true,
speakText: argv.speech,
ex: argv.ex,
width: argv.width,
displayAlign: 'left',
useGlobalCache: false,
useFontCache: false,
// addMMLclasses: true,
linebreaks: argv.linebreaks,
};
mjAPI.typeset(conf, function (data) {
if (data.errors) {
console.error(data.errors);
} else if (argv.out) {
fs.writeFile(argv.out, data.svg, function (err) {
if (err) {
return console.error(err);
}
});
} else {
console.log(data.svg);
}
});
}
if (1) {
if (argv.font === 'STIX') argv.font = 'STIX-Web';
mjAPI.config({
MathJax: {
SVG: {
addMMLclasses: true,
displayAlign: 'left',
useGlobalCache: false,
useFontCache: false,
font: argv.font,
},
},
extensions: argv.extensions,
});
mjAPI.start();
}
if (argv._.length < 1 || argv._[0] == '-') {
proc(false, fs.readFileSync(0, 'utf-8'));
} else {
fs.readFile(argv._[0], 'utf8', proc);
}