From c6c0976fb3a0c8d8244d9a697d072c7ce5d61349 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Thu, 17 Jan 2013 00:28:35 +0400 Subject: [PATCH] compile remastered. +haml prefilter dirty fix --- compile.js | 106 +++++++++++++++++++++++---------------------- src/compile.coffee | 90 +++++++++++++++++++------------------- 2 files changed, 99 insertions(+), 97 deletions(-) diff --git a/compile.js b/compile.js index ffd6ab8..883b2ff 100644 --- a/compile.js +++ b/compile.js @@ -2,70 +2,81 @@ (function() { module.exports = function(data, finalcb) { - var doT, flow, fs, path, readFile, readItem; + var any_error, child, doT, flow, fs, path, readFile, readItem; fs = require('fs'); path = require('path'); flow = require('flow'); doT = require('./doT.js'); + child = require('child_process'); + any_error = function(results) { + var r, _i, _len; + for (_i = 0, _len = results.length; _i < _len; _i++) { + r = results[_i]; + if (r[0]) { + return r[0]; + } + } + return null; + }; readItem = function(item, callback) { return flow.exec(function() { return fs.stat(item, this); }, function(err, stat) { - var cb; + var item_cb; if (err) { - process.stderr.write(err); return this(err); - } else if (stat.isDirectory()) { - cb = this; - return flow.exec(function() { - return fs.readdir(item, this); - }, function(err, files) { - var _this = this; - if (err) { - process.stderr.write(err); - return this(err); - } else { - return files.forEach(function(file) { - return readItem(path.join(item, file), _this.MULTI()); - }); - } - }, function() { - return cb(null); - }); - } else { + } + if (!stat.isDirectory()) { return readFile(item, this); } - }, function() { - return callback(null); + item_cb = this; + return flow.exec(function() { + return fs.readdir(item, this); + }, function(err, files) { + var _this = this; + if (err) { + return this.MULTI(err); + } + return files.forEach(function(file) { + return readItem(path.join(item, file), _this.MULTI()); + }); + }, function(results) { + return item_cb(any_error(results)); + }); + }, function(err) { + return callback(err); }); }; readFile = function(file, callback) { return flow.exec(function() { - return fs.readFile(file, this); + if (file.match(/.haml$/)) { + child.exec("haml '" + file + "'", this); + return file = path.basename(file, '.haml'); + } else { + return fs.readFile(file, this); + } }, function(err, text) { - var f, id, rel; + var dot_err, f, id, rel; if (err) { - process.stderr.write("Error reading file '" + file + "': '" + err + "\n"); return this(err); - } else { - id = path.basename(file, path.extname(file)); - if (data.base) { - rel = path.relative(data.base, path.dirname(file)).replace(/\//g, '.'); - if (rel) { - id = "" + rel + "." + id; - } - } - try { - f = doT.compile(text); - doT.addCached(id, f); - return this(null, f); - } catch (e) { - process.stderr.write("Error compiling file '" + file + "': '" + e + "'\n"); - return this(e); + } + id = path.basename(file, path.extname(file)); + if (data.base) { + rel = path.relative(data.base, path.dirname(file)).replace(/\//g, '.'); + if (rel) { + id = "" + rel + "." + id; } } - }, function() { - return callback(null); + f = dot_err = null; + try { + f = doT.compile(text); + doT.addCached(id, f); + } catch (e) { + dot_err = e; + } + return this(dot_err, f); + }, function(err, f) { + return callback(err, f); }); }; return flow.exec(function() { @@ -74,17 +85,10 @@ return readItem(val, _this.MULTI()); }); }, function(results) { - var r, _i, _len; if (!finalcb) { return; } - for (_i = 0, _len = results.length; _i < _len; _i++) { - r = results[_i]; - if (r[0]) { - return finalcb(r[0], doT.exportCached()); - } - } - return finalcb(null, doT.exportCached()); + return finalcb(any_error(results), doT.exportCached()); }); }; diff --git a/src/compile.coffee b/src/compile.coffee index d1c49d2..219a580 100644 --- a/src/compile.coffee +++ b/src/compile.coffee @@ -3,63 +3,61 @@ module.exports = (data, finalcb) -> path = require 'path' flow = require 'flow' doT = require './doT.js' + child = require 'child_process' - readItem = ( item, callback ) -> + any_error = (results) -> + for r in results + return r[0] if r[0] + null + + readItem = (item, callback) -> flow.exec( - -> - fs.stat item, @ - ( err, stat ) -> - if err - process.stderr.write err - @ err - else if stat.isDirectory() - cb = @ - flow.exec( - -> - fs.readdir item, @ - ( err, files ) -> - if err - process.stderr.write err - @ err - else - files.forEach ( file ) => - readItem path.join( item, file ), @MULTI() - -> cb null - ) - else - readFile item, @ - -> callback null + -> fs.stat item, @ + (err, stat) -> + return @ err if err + return readFile item, @ unless stat.isDirectory() + item_cb = @ + flow.exec( + -> fs.readdir item, @ + (err, files) -> + return @MULTI err if err + files.forEach (file) => + readItem path.join(item, file), @MULTI() + (results) -> item_cb any_error results + ) + (err) -> + callback err ) - readFile = ( file, callback ) -> + readFile = (file, callback) -> flow.exec( -> - fs.readFile file, @ - ( err, text ) -> - if err - process.stderr.write "Error reading file '#{file}': '#{err}\n" - @ err + if file.match /.haml$/ + child.exec "haml '#{file}'", @ + file = path.basename file, '.haml' else - id = path.basename file, path.extname file - if data.base - rel = path.relative(data.base, path.dirname file) - .replace /\//g, '.' - id = "#{rel}.#{id}" if rel - try - f = doT.compile text - doT.addCached id, f - @ null, f - catch e - process.stderr.write "Error compiling file '#{file}': '#{e}'\n" - @ e - -> callback(null) + fs.readFile file, @ + (err, text) -> + return @ err if err + id = path.basename file, path.extname file + if data.base + rel = path.relative(data.base, path.dirname file) + .replace /\//g, '.' + id = "#{rel}.#{id}" if rel + f = dot_err = null + try + f = doT.compile text + doT.addCached id, f + catch e + dot_err = e + @ dot_err, f + (err, f) -> + callback err, f ) flow.exec( -> data.files.forEach ( val, i ) => readItem val, @MULTI() (results) -> return unless finalcb - for r in results - return finalcb r[0], doT.exportCached() if r[0] - finalcb null, doT.exportCached() + finalcb any_error(results), doT.exportCached() )