From 53242d4660674ff126214874528acf8273e4333d Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Thu, 4 Aug 2011 13:35:42 +0100 Subject: [PATCH] use abiword with abicommand under unix, spawn abiword processes under windows --- node/utils/Abiword.js | 132 +++++++++++++++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 27 deletions(-) diff --git a/node/utils/Abiword.js b/node/utils/Abiword.js index 326cb767f..3a99f56be 100644 --- a/node/utils/Abiword.js +++ b/node/utils/Abiword.js @@ -20,21 +20,63 @@ var util = require('util'); var spawn = require('child_process').spawn; +var async = require("async"); var settings = require("./Settings"); +var os = require('os'); -var stdoutBuffer = ""; - -function doConvertTask(task, callback) +//on windows we have to spawn a process for each convertion, cause the plugin abicommand doesn't exist on this platform +if(os.type().indexOf("Windows") > -1) { - //span an abiword process to perform the conversion - var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]); - - //delegate the processing of stdout to another function - abiword.stdout.on('data', function (data) + var stdoutBuffer = ""; + + function doConvertTask(task, callback) { - //add data to buffer - stdoutBuffer+=data.toString(); - }); + //span an abiword process to perform the conversion + var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]); + + //delegate the processing of stdout to another function + abiword.stdout.on('data', function (data) + { + //add data to buffer + stdoutBuffer+=data.toString(); + }); + + //append error messages to the buffer + abiword.stderr.on('data', function (data) + { + stdoutBuffer += data.toString(); + }); + + //throw exceptions if abiword is dieing + abiword.on('exit', function (code) + { + if(code != 0) { + throw "Abiword died with exit code " + code; + } + + if(stdoutBuffer != "") + { + console.log(stdoutBuffer); + } + + callback(); + }); + } + + exports.convertFile = function(srcFile, destFile, type, callback) + { + doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback); + }; +} +//on unix operating systems, we can start abiword with abicommand and communicate with it via stdin/stdout +//thats much faster, about factor 10 +else +{ + //Queue with the converts we have to do + var queue = async.queue(doConvertTask, 1); + + //spawn the abiword process + var abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]); //append error messages to the buffer abiword.stderr.on('data', function (data) @@ -43,22 +85,58 @@ function doConvertTask(task, callback) }); //throw exceptions if abiword is dieing - abiword.on('exit', function (code) + abiword.on('exit', function (code) { - if(code != 0) { - throw "Abiword died with exit code " + code; - } - - if(stdoutBuffer != "") - { - console.log(stdoutBuffer); - } - - callback(); + throw "Abiword died with exit code " + code; }); -} -exports.convertFile = function(srcFile, destFile, type, callback) -{ - doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback); -}; + //delegate the processing of stdout to a other function + abiword.stdout.on('data',onAbiwordStdout); + + var stdoutCallback = null; + var stdoutBuffer = ""; + var firstPrompt = true; + + function onAbiwordStdout(data) + { + //add data to buffer + stdoutBuffer+=data.toString(); + + //we're searching for the prompt, cause this means everything we need is in the buffer + if(stdoutBuffer.search("AbiWord:>") != -1) + { + //filter the feedback message + var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer; + + //reset the buffer + stdoutBuffer = ""; + + //call the callback with the error message + //skip the first prompt + if(stdoutCallback != null && !firstPrompt) + { + stdoutCallback(err); + stdoutCallback = null; + } + + firstPrompt = false; + } + } + + function doConvertTask(task, callback) + { + abiword.stdin.write("convert " + task.srcFile + " " + task.destFile + " " + task.type + "\n"); + + //create a callback that calls the task callback and the caller callback + stdoutCallback = function (err) + { + callback(); + task.callback(err); + }; + } + + exports.convertFile = function(srcFile, destFile, type, callback) + { + queue.push({"srcFile": srcFile, "destFile": destFile, "type": type, "callback": callback}); + }; +}