Skip to content

Added the ability to supply an environment and additional command line options. #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ In addition, you can also specify the following additional parameters via the `o

* `command`: An optional override for the latex command. By default calls `latex`.
* `format`: Either "pdf" or "dvi". By default returns a pdf.

* `args`: An optional array of additional command line arguments to be supplied to the latex/pdflatex command, i.e. `['-src-specials']`
* `env`: An optional environment object to be used in place of `process.env`. If not specified, `process.env` will be used by default.

The function returns a readable Stream object representing a LaTeX encoded document in either PDF or [DVI format](http://en.wikipedia.org/wiki/Device_independent_file_format). If there were errors in the syntax of the document, they will be raised as errors on this Stream object.

Credits
Expand Down
Binary file removed examples/.DS_Store
Binary file not shown.
89 changes: 49 additions & 40 deletions texwrapper.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var spawn = require("child_process").spawn;
var path = require("path");
var fs = require("fs");
var fse = require("fs-extra");
var temp = require("temp");
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;
var directory_built = false;
var directory_err = null;
var directory_wait = [];
var directory_path = "/tmp";
var directory_count = 0;
temp.mkdir("node-latex", function(err, dirpath) {
if(!err) {
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) {
for (var i = 0; i < directory_wait.length; ++i) {
directory_wait[i]();
}
directory_wait.length = 0;
Expand All @@ -29,20 +29,20 @@ temp.mkdir("node-latex", function(err, dirpath) {
//Waits for directory to be built
function awaitDir(cb) {
function makeLocalDir() {
if(directory_err) {
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) {
if (err) {
cb(err, null);
return;
}
cb(null, temp_path);
});
}
if(directory_built) {
if (directory_built) {
makeLocalDir();
} else {
directory_wait.push(makeLocalDir);
Expand All @@ -53,7 +53,7 @@ function awaitDir(cb) {
function handleErrors(dirpath, result) {
var log_file = path.join(dirpath, "texput.log");
fs.exists(log_file, function(exists) {
if(!exists) {
if (!exists) {
fse.remove(dirpath);
result.emit("error", new Error("Error running LaTeX"));
return;
Expand All @@ -63,15 +63,15 @@ function handleErrors(dirpath, result) {
var err = [];
log.on("data", function(data) {
var lines = data.toString().split("\n");
for(var i=0; i<lines.length; ++i) {
for (var i = 0; i < lines.length; ++i) {
var l = lines[i];
if(l.length > 0 && l.charAt(0) === "!") {
if (l.length > 0 && l.charAt(0) === "!") {
err.push(lines[i]);
}
}
});
log.on("end", function() {
if(err.length > 0) {
if (err.length > 0) {
err.unshift("LaTeX Syntax Error");
result.emit("error", new Error(err.join("\n")));
} else {
Expand All @@ -83,54 +83,63 @@ function handleErrors(dirpath, result) {

//Converts a expression into a LaTeX image
module.exports = function(doc, options) {
if(!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) {
if (err) {
error(err);
return;
}

// Create command line args
var moreArgs = options.args || [];

var allArgs = moreArgs.concat([
"-interaction=nonstopmode",
"texput.tex"
]);

// If env is specified in options, use it, otherwise use process.env.
var env = options.env || process.env;

//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"
], {
var tex = spawn(tex_command, allArgs, {
cwd: dirpath,
env: process.env
env: env
});

// Let the user know if LaTeX couldn't be found
tex.on('error', function(err) {
if (err.code === 'ENOENT') {
if (err.code === 'ENOENT') {
console.error("\nThere was an error spawning " + tex_command + ". \n"
+ "Please make sure your LaTeX distribution is"
+ "properly installed.\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) {
if (exists) {
var stream = fs.createReadStream(output_file);
stream.on("close", function() {
fse.remove(dirpath);
Expand All @@ -142,21 +151,21 @@ module.exports = function(doc, options) {
});
});
});
if(typeof doc === "string" || doc instanceof Buffer) {

if (typeof doc === "string" || doc instanceof Buffer) {
tex_file.end(doc);
} else if(doc instanceof Array) {
for(var i=0; i<doc.length; ++i) {
} 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) {
} else if (doc.pipe) {
doc.pipe(tex_file);
} else {
error(new Error("Invalid document"));
return;
}
});

return result;
}