forked from mikolalysenko/node-latex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texwrapper.js
163 lines (151 loc) · 4.2 KB
/
texwrapper.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var spawn = require("child_process").spawn;
var path = require("path");
var fs = require("fs");
var fse = require("fs-extra");
var temp = require("temp");
var through = require("through");
//Eagerly create temporary directory
var directory_built = false
, directory_err = null
, directory_wait = []
, directory_path = "/tmp"
, directory_count = 0;
temp.mkdir("node-latex", function(err, dirpath) {
if(!err) {
process.on("exit", function() {
fse.removeSync(dirpath);
});
}
directory_err = err;
directory_path = dirpath;
directory_built = true;
for(var i=0; i<directory_wait.length; ++i) {
directory_wait[i]();
}
directory_wait.length = 0;
});
//Waits for directory to be built
function awaitDir(cb) {
function makeLocalDir() {
if(directory_err) {
cb(directory_err, null);
return;
}
var temp_path = path.join(directory_path, "" + directory_count++);
fse.mkdirp(temp_path, function(err) {
if(err) {
cb(err, null);
return;
}
cb(null, temp_path);
});
}
if(directory_built) {
makeLocalDir();
} else {
directory_wait.push(makeLocalDir);
}
}
//Send errors downstream to result
function handleErrors(dirpath, result) {
var log_file = path.join(dirpath, "texput.log");
fs.exists(log_file, function(exists) {
if(!exists) {
fse.remove(dirpath);
result.emit("error", new Error("Error running LaTeX"));
return;
}
//Try to crawl through the horrible mess that LaTeX shat upon us
var log = fs.createReadStream(log_file);
var err = [];
log.on("data", function(data) {
var lines = data.toString().split("\n");
for(var i=0; i<lines.length; ++i) {
var l = lines[i];
if(l.length > 0 && l.charAt(0) === "!") {
err.push(lines[i]);
}
}
});
log.on("end", function() {
if(err.length > 0) {
err.unshift("LaTeX Syntax Error");
result.emit("error", new Error(err.join("\n")));
} else {
result.emit("error", new Error("Unspecified LaTeX error"));
}
});
});
}
//Converts a expression into a LaTeX image
module.exports = function(doc, options) {
if(!options) {
options = {};
}
var format = options.format || "pdf";
//LaTeX command
var tex_command = options.command || (format === "pdf" ? "pdflatex" : "latex");
//Create result
var result = through();
awaitDir(function(err, dirpath) {
function error(e) {
result.emit("error", e);
result.destroySoon();
}
if(err) {
error(err);
return;
}
//Write data to tex file
var input_path = path.join(dirpath, "texput.tex");
var tex_file = fs.createWriteStream(input_path);
tex_file.on("close", function() {
//Invoke LaTeX
var tex = spawn(tex_command, [
"-interaction=nonstopmode",
"texput.tex"
], {
cwd: dirpath,
env: process.env,
stdio: ['ignore','ignore','ignore']
});
// Let the user know if LaTeX couldn't be found
tex.on('error', function(err) {
if (err.code === 'ENOENT') {
console.error("\nThere was an error spawning " + tex_command + ". \n"
+ "Please make sure your LaTeX distribution is"
+ "properly installed.\n");
}
});
//Wait for LaTeX to finish its thing
tex.on("exit", function(code, signal) {
var output_file = path.join(dirpath, "texput." + format);
fs.exists(output_file, function(exists) {
if(exists) {
var stream = fs.createReadStream(output_file);
stream.on("close", function() {
fse.remove(dirpath);
});
stream.pipe(result);
} else {
handleErrors(dirpath, result);
}
});
});
});
if(typeof doc === "string" || doc instanceof Buffer) {
tex_file.end(doc);
} else if(doc instanceof Array) {
for(var i=0; i<doc.length; ++i) {
tex_file.write(doc[i]);
}
tex_file.end();
} else if(doc.pipe) {
doc.pipe(tex_file);
} else {
error(new Error("Invalid document"));
return;
}
});
return result;
}