Skip to content

Commit

Permalink
tinkering with event flow
Browse files Browse the repository at this point in the history
  • Loading branch information
jostylr committed Nov 4, 2013
1 parent 5055f14 commit 5f595f9
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 222 deletions.
123 changes: 71 additions & 52 deletions bin/literate-programming.js
Expand Up @@ -7,8 +7,13 @@ var fs = require('fs');
var Doc = require('../lib/literate-programming').Doc;
var path = require('path');

var doc = new Doc();

var emitter = doc.emitter;
var env = doc.environment = {};

program
.version('0.7.2')
.version('0.8.0-pre')
.usage('[options] <file> <arg1> ...')
.option('-o --output <root>', 'Root directory for output')
.option('-i --input <root>', 'Root directory for input')
Expand All @@ -26,24 +31,17 @@ if ((! program.args[0]) ) {
process.exit();
}

var dir = program.dir || program.root || process.cwd();
var indir = program.change || program.root || process.cwd();
var originalroot = process.cwd();
if (indir) {
process.chdir(indir);
}

var verbose = program.verbose || 0;
env.output = program.output || program.root || process.cwd();
env.input = program.input || program.root || process.cwd();
env.originalroot = process.cwd();

var md;
try {
md = fs.readFileSync(program.args[0], 'utf8');
} catch (e) {
console.log("Not readable file " + program.args[0]);
md = "";
if (env.input !== env.originalroot) {
process.chdir(env.input);
}

var inputs = program.args.slice(1);
env.verbose = program.verbose || 0;

env.inputs = program.args.slice(1);

var postCompile;

Expand Down Expand Up @@ -72,17 +70,17 @@ postCompile.push = function (arr) {
postCompile.steps = [];

if (program.preview) {
postCompile.push([function (text, next) {
emitter.on("file directive", [doc, function (text, next) {
var passin = this;
var doc = passin.doc;
if (passin.action && passin.action.filename) {
var fname = passin.action.filename;
doc.log(fname + ": " + text.length + "\n"+text.match(/^([^\n]*)(?:\n|$)/)[1]);
}
next(text);
}, {}]);
}]);
} else if (program.diff) {
postCompile.push([function (text, next, obj) {
emitter.on("file directive", [doc, function (text, next, obj) {
var passin = this;
var doc = passin.doc;
var fname = passin.action.filename;
Expand All @@ -94,38 +92,60 @@ if (program.preview) {

doc.log(fname + " diff not activated yet ");
next(text);
}, {dir:dir}]);
}]);
} else {
postCompile.push([function (text, next, obj) {
var passin = this;
var doc = passin.doc;
if (passin.action && passin.action.filename) {
var fname = passin.action.filename;
emitter.on("file directive", [doc, function (data, emitter, ev) {
var filename = data.filename,
cpath = data.cpath,
commands = data.commands,
n = commands.length,
namespace, handler,
shared = {}
;

process.chdir(originalroot);
if (obj.dir) {
process.chdir(dir);
}
var cb = function (err) {
if (err) {
doc.log("Error in saving file " + fname + ": " + err.message);
} else {
doc.log("File "+ fname + " saved");
}
next(text);
};
namespace = cpath+"->"+filename;

fs.writeFile(fname, text, 'utf8', cb);
} else {
next(text);
handler = emitter.when(namespace + "--block compiled", namespace+"--block ready for saving");

function (wtext, emitter, ev) {
var doc = this,
text = wtext.text;

if (passin.action && passin.action.filename) {
var fname = passin.action.filename;

if (env.output !== env.originalroot) {
process.chdir(env.output);
}

process.chdir(originalroot);
if (obj.dir) {
process.chdir(dir);
}
var cb = function (err) {
if (err) {
doc.log("Error in saving file " + fname + ": " + err.message);
} else {
doc.log("File "+ fname + " saved");
}
next(text);
};

fs.writeFile(fname, text, 'utf8', cb);
} else {
next(text);
}
}
}, {dir: dir}]);

emitter.on(namespace+"--block ready for saving");

}]);
}

var standardPlugins, plugins;

if (!program.free) {
standardPlugins = require('literate-programming-standard');
env.standardPlugins = require('literate-programming-standard');
var original = process.cwd();
var files;

Expand All @@ -151,7 +171,7 @@ if (!program.free) {
}

if (!program.quiet) {
postCompile.push([function (text, next) {
emitter.on("compilation done", function (text, next) {
var doc = this.doc;
var logitem;
var i, n = doc.logarr.length;
Expand All @@ -162,17 +182,16 @@ if (!program.quiet) {
}
}
next(text);
}, {}]);
});
}

postCompile.push([function (text, next) {
var doc = this.doc;
try {
delete doc.actions[this.action.msg];
} catch (e) {
}
next(text);
}, {}]);
fs.readFile(program.args[0], 'utf8', function (err, text) {
if (err) {
emitter.emit("error in reading file", [err, program.args[0]]);
} else {
emitter.emit("text received", text);
}
});

var doc = new Doc(md, {
standardPlugins : standardPlugins,
Expand Down

0 comments on commit 5f595f9

Please sign in to comment.