-
Notifications
You must be signed in to change notification settings - Fork 605
/
put-math.js
executable file
·49 lines (43 loc) · 1.43 KB
/
put-math.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
#!/usr/bin/env phantomjs
// Usage: ./put-math.js json_db file1 [file2 ...]
// json_db: existing JSON file that contains the MathML database;
// file1, file2, ...: the output files to modify.
// This replaces all the LaTeX markup in given output files with MathML.
// It does it by searching for LaTeX strings delimited by \( \) or \[ \]
// and looks up the mapping from LaTeX to MathML in the JSON database.
// (c) 2014 Andres Raba, GNU GPL v.3.
var system = require('system'),
fs = require('fs');
// LaTeX is enclosed in \( \) or \[ \] delimiters,
// first pair for inline, second for display math:
var pattern = /\\\([\s\S]+?\\\)|\\\[[\s\S]+?\\\]/g;
if (system.args.length <= 2) {
console.log("Usage: ./put-math.js json_db file1 [file2 ...]");
phantom.exit();
}
else {
var db = system.args[1]; // JSON database
var args = system.args.slice(2); // file1, file2, ...
try {
var mathml = JSON.parse(fs.read(db));
}
catch(error) {
console.log(error);
phantom.exit();
}
args.forEach(function (arg) {
try {
var file = fs.read(arg);
// Replace LaTeX with MathML or paint LaTeX blue
// if mapping not found in JSON database:
var file = file.replace(pattern, function (latex) {
return mathml[latex] || "<span style='color:blue'>" + latex + "</span>";
});
fs.write(arg, file, 'w');
}
catch(error) {
console.log(error);
}
});
phantom.exit();
}