Skip to content
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
93 changes: 41 additions & 52 deletions lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
(function() {
var CoffeeScript, indent, precompile, preprocess;

CoffeeScript = require("coffee-script");

preprocess = require("./preprocessor").preprocess;

indent = require("./util").indent;

exports.precompile = precompile = function(source) {
var script;
script = CoffeeScript.compile(preprocess(source), {
noWrap: true
});
return "function(__obj) {\n if (!__obj) __obj = {};\n var __out = [], __capture = function(callback) {\n var out = __out, result;\n __out = [];\n callback.call(this);\n result = __out.join('');\n __out = out;\n return __safe(result);\n }, __sanitize = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else if (typeof value !== 'undefined' && value != null) {\n return __escape(value);\n } else {\n return '';\n }\n }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;\n __safe = __obj.safe = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else {\n if (!(typeof value !== 'undefined' && value != null)) value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n }\n };\n if (!__escape) {\n __escape = __obj.escape = function(value) {\n return ('' + value)\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\x22/g, '&quot;');\n };\n }\n (function() {\n" + (indent(script, 4)) + "\n }).call(__obj);\n __obj.safe = __objSafe, __obj.escape = __escape;\n return __out.join('');\n}";
};

exports.compile = function(source) {
return new Function("return " + (precompile(source)))();
};

}).call(this);
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(function() {
var compile, eco, precompile, preprocess, _ref;

_ref = require("./compiler"), compile = _ref.compile, precompile = _ref.precompile;

preprocess = require("./preprocessor").preprocess;

module.exports = eco = function(source) {
var _base, _ref2;
if (eco.cache) {
Expand All @@ -10,18 +13,25 @@
return compile(source);
}
};

eco.cache = {};

eco.preprocess = preprocess;

eco.precompile = precompile;

eco.compile = compile;

eco.render = function(source, data) {
return (eco(source))(data);
};

if (require.extensions) {
require.extensions[".eco"] = function(module, filename) {
var source;
source = require("fs").readFileSync(filename, "utf-8");
return module._compile("module.exports = " + (precompile(source)), filename);
};
}

}).call(this);
28 changes: 21 additions & 7 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
(function() {
var Preprocessor, Scanner, util;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

Scanner = require("./scanner");

util = require("./util");

module.exports = Preprocessor = (function() {

Preprocessor.preprocess = function(source) {
var preprocessor;
preprocessor = new Preprocessor(source);
return preprocessor.preprocess();
};

function Preprocessor(source) {
this.scanner = new Scanner(source);
this.output = "";
this.level = 0;
this.options = {};
this.captures = [];
}

Preprocessor.prototype.preprocess = function() {
var _this = this;
while (!this.scanner.done) {
this.scanner.scan(__bind(function(token) {
return this[token[0]].apply(this, token.slice(1));
}, this));
this.scanner.scan(function(token) {
return _this[token[0]].apply(_this, token.slice(1));
});
}
return this.output;
};

Preprocessor.prototype.record = function(line) {
this.output += util.repeat(" ", this.level);
return this.output += line + "\n";
};

Preprocessor.prototype.printString = function(string) {
if (string.length) {
return this.record("__out.push " + (util.inspectString(string)));
}
};

Preprocessor.prototype.beginCode = function(options) {
return this.options = options;
};

Preprocessor.prototype.recordCode = function(code) {
if (code !== "end") {
if (this.options.print) {
Expand All @@ -49,6 +59,7 @@
}
}
};

Preprocessor.prototype.indent = function(capture) {
this.level++;
if (capture) {
Expand All @@ -57,19 +68,22 @@
return this.indent();
}
};

Preprocessor.prototype.dedent = function() {
this.level--;
if (this.level < 0) {
this.fail("unexpected dedent");
}
if (this.level < 0) this.fail("unexpected dedent");
if (this.captures[0] === this.level) {
this.captures.shift();
return this.dedent();
}
};

Preprocessor.prototype.fail = function(message) {
throw "Parse error on line " + this.scanner.lineNo + ": " + message;
};

return Preprocessor;

})();

}).call(this);
Loading