diff --git a/README.md b/README.md index a4a5b39..82965a4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/.DS_Store b/examples/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/examples/.DS_Store and /dev/null differ diff --git a/texwrapper.js b/texwrapper.js index c516c18..f8fe12d 100644 --- a/texwrapper.js +++ b/texwrapper.js @@ -1,18 +1,18 @@ -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); }); @@ -20,7 +20,7 @@ temp.mkdir("node-latex", function(err, dirpath) { directory_err = err; directory_path = dirpath; directory_built = true; - for(var i=0; i 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 { @@ -83,15 +83,15 @@ 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) { @@ -99,30 +99,39 @@ module.exports = function(doc, options) { 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"); } }); @@ -130,7 +139,7 @@ module.exports = function(doc, options) { 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); @@ -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