Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Rebuild JS
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed Feb 13, 2011
1 parent b5d0075 commit 6222e41
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 43 deletions.
18 changes: 18 additions & 0 deletions lib/eco/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function() {
var eco, fs;
fs = "fs";
eco = require("eco");
exports.run = function() {
var source, stdin;
source = "";
stdin = process.openStdin();
stdin.on("data", function(buffer) {
if (buffer) {
return source += buffer.toString();
}
});
return stdin.on("end", function() {
return console.log(eco.compile(source));
});
};
}).call(this);
26 changes: 16 additions & 10 deletions lib/eco/compiler.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
(function() {
var CoffeeScript, compile, indent, preprocess;
var CoffeeScript, compile, eco, indent, preprocess;
CoffeeScript = require("coffee-script");
preprocess = require("eco/preprocessor").preprocess;
indent = require("eco/util").indent;
exports.compile = compile = function(source) {
var script;
module.exports = eco = function(source) {
var module;
(new Function("module", compile(source)))(module = {});
return module.exports;
};
eco.preprocess = preprocess;
eco.compile = compile = function(source, options) {
var identifier, script, _ref;
identifier = (_ref = options != null ? options.identifier : void 0) != null ? _ref : "module.exports";
if (!identifier.match(/\./)) {
identifier = "var " + identifier;
}
script = CoffeeScript.compile(preprocess(source), {
noWrap: true
});
return "module.exports = function(__obj) {\n var _safe = function(value) {\n if (typeof value === 'undefined' && value == null)\n value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n };\n return (function() {\n var __out = [], __self = this, _print = function(value) {\n if (typeof value !== 'undefined' && value != null)\n __out.push(value.ecoSafe ? value : __self.escape(value));\n }, _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 };\n" + (indent(script, 4)) + "\n return __out.join('');\n }).call((function() {\n var obj = {\n escape: function(value) {\n return ('' + value)\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\"/g, '&quot;');\n },\n safe: _safe\n }, key;\n for (key in __obj) obj[key] = __obj[key];\n return obj;\n })());\n};";
return "" + identifier + " = 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.render = function(source, data) {
var module, template;
module = {};
template = new Function("module", compile(source));
template(module);
return module.exports(data);
eco.render = function(source, data) {
return (eco(source))(data);
};
if (require.extensions) {
require.extensions[".eco"] = function(module, filename) {
Expand Down
29 changes: 14 additions & 15 deletions lib/eco/preprocessor.js

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

31 changes: 17 additions & 14 deletions lib/eco/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
var Scanner, StringScanner, trim;
StringScanner = require("strscan").StringScanner;
trim = require("eco/util").trim;
exports.scan = function(source) {
var scanner, tokens;
tokens = [];
scanner = new Scanner(source);
while (!scanner.done) {
scanner.scan(function(token) {
return tokens.push(token);
});
}
return tokens;
};
exports.Scanner = Scanner = (function() {
module.exports = Scanner = (function() {
Scanner.modePatterns = {
data: /(.*?)(<%(([=-])?)|\n|$)/,
data: /(.*?)(<%%|<%(([=-])?)|\n|$)/,
code: /(.*?)(((:|(->|=>))\s*)?%>|\n|$)/
};
Scanner.dedentablePattern = /^(end|when|else|catch|finally)(?:\W|$)/;
Scanner.scan = function(source) {
var scanner, tokens;
tokens = [];
scanner = new Scanner(source);
while (!scanner.done) {
scanner.scan(function(token) {
return tokens.push(token);
});
}
return tokens;
};
function Scanner(source) {
this.source = source.replace(/\r\n?/g, "\n");
this.scanner = new StringScanner(this.source);
Expand Down Expand Up @@ -56,7 +56,10 @@
return this.arrow = this.scanner.getCapture(4);
};
Scanner.prototype.scanData = function(callback) {
if (this.tail === "\n") {
if (this.tail === "<%%") {
this.buffer += "<%";
return this.scan(callback);
} else if (this.tail === "\n") {
this.buffer += this.tail;
this.lineNo++;
return this.scan(callback);
Expand Down
30 changes: 27 additions & 3 deletions lib/eco/util.js

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

File renamed without changes.
2 changes: 1 addition & 1 deletion src/eco/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ exports.inspectString = (string) ->
code = character.charCodeAt(0).toString(16)
code = "0#{code}" if code.length is 1
"\\u00#{code}"
"'#{contents.replace /'/g, '\\\''}'"
"'" + contents.replace(/'/g, '\\\'') + "'"

0 comments on commit 6222e41

Please sign in to comment.