-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtex2svg-page
executable file
·130 lines (118 loc) · 4.01 KB
/
tex2svg-page
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/env node
/*************************************************************************
*
* jsdom/tex2svg-page
*
* Uses MathJax v3 to convert all TeX in an HTML document using jsdom.
*
* ----------------------------------------------------------------------
*
* Copyright (c) 2020 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.
*/
//
// Load the packages needed for MathJax
//
const {mathjax} = require('mathjax-full/js/mathjax.js');
const {TeX} = require('mathjax-full/js/input/tex.js');
const {MathML} = require('mathjax-full/js/input/mathml.js');
const {SVG} = require('mathjax-full/js/output/svg.js');
const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js');
const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js');
const {EnrichHandler} = require('mathjax-full/js/a11y/semantic-enrich.js');
const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js');
require('mathjax-full/js/util/entities/all.js');
//
// Get the command-line arguments
//
const argv = require('yargs')
.demand(1).strict()
.usage('$0 [options] file.html > converted.html')
.options({
em: {
default: 16,
describe: 'em-size in pixels'
},
ex: {
default: 8,
describe: 'ex-size in pixels'
},
packages: {
default: AllPackages.sort().join(', '),
describe: 'the packages to use, e.g. "base, ams"'
},
sre: {
array: true,
nargs: 2,
describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules'
},
speech: {
default: 'shallow',
describe: 'level of speech: deep, shallow, none'
},
fontCache: {
default: 'global',
describe: 'cache type: local, global, none'
}
})
.argv;
const action = require('./action.js');
//
// Read the HTML file
//
const htmlfile = require('fs').readFileSync(argv._[0], 'utf8');
//
// Create DOM adaptor and register it for HTML documents
//
const adaptor = liteAdaptor({fontSize: argv.em});
EnrichHandler(RegisterHTMLHandler(adaptor), new MathML());
//
// Get feature vector for SRE setup. If necessary, compute the path to the
// locale JSON files explicitly.
//
const feature = action.dataPairs(argv.sre);
feature.speech = argv.speech;
feature.json = feature.json ? feature.json :
require.resolve('mathjax-full/es5/sre/mathmaps/base.json').replace(/\/base\.json$/, '');
//
// Create input and output jax and a document using them on the content from the HTML file
//
const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)});
const svg = new SVG({fontCache: argv.fontCache});
const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: svg,
enableEnrichment: true,
sre: feature,
renderActions: action.speechAction
}
);
//
// Typeset the document
//
mathjax.handleRetriesFor(() => html.render()).then(
() => {
//
// If no math was found on the page, remove the stylesheet and font cache (if any)
//
if (Array.from(html.math).length === 0) {
adaptor.remove(html.outputJax.svgStyles);
const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache');
if (cache) adaptor.remove(cache);
}
//
// Output the resulting HTML
//
console.log(adaptor.doctype(html.document));
console.log(adaptor.outerHTML(adaptor.root(html.document)));
}
);