Permalink
Browse files

Express.js compatibility

* `eco.compile` now returns a function for Express compatibility
* `eco.precompile` is the old `eco.compile`
* `eco(...)` caches templates; disable with `eco.cache = false`
* `eco.render` uses `eco(...)` for caching
  • Loading branch information...
1 parent 6c3d650 commit 5d62cdd9db455ea6ac3e4a0634779f8161ea5def @sstephenson committed May 29, 2011
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,29 +1,16 @@
(function() {
- var CoffeeScript, compile, eco, indent, preprocess;
+ var CoffeeScript, indent, precompile, preprocess;
CoffeeScript = require("coffee-script");
preprocess = require("./preprocessor").preprocess;
indent = require("./util").indent;
- module.exports = eco = function(source) {
- return new Function("return " + (compile(source)))();
- };
- eco.preprocess = preprocess;
- eco.compile = compile = function(source) {
+ 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}";
};
- eco.render = function(source, data) {
- return (eco(source))(data);
+ exports.compile = function(source) {
+ return new Function("return " + (precompile(source)))();
};
- if (require.extensions) {
- require.extensions[".eco"] = function(module, filename) {
- var source;
- source = require("fs").readFileSync(filename, "utf-8");
- return module._compile("module.exports = " + (compile(source)), filename);
- };
- } else if (require.registerExtension) {
- require.registerExtension(".eco", compile);
- }
}).call(this);
View
@@ -1,2 +1,27 @@
-require("coffee-script");
-module.exports = require("./compiler");
+(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) {
+ return (_ref2 = (_base = eco.cache)[source]) != null ? _ref2 : _base[source] = compile(source);
+ } else {
+ 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);
View
@@ -18,6 +18,7 @@
}
, "devDependencies":
{ "nodeunit": ">=0.5.0"
+ , "express": ">=2.0.0"
, "uglify-js": ">=1.0.0"
}
}
View
@@ -97,7 +97,7 @@ stripExtension = (name) ->
compile = (infile, identifier, name, callback) ->
fs.readFile infile, "utf8", (err, source) ->
return callback err if err
- template = indent eco.compile(source), 2
+ template = indent eco.precompile(source), 2
callback null, """
(function() {
@@ -134,7 +134,7 @@ exports.run = (args = process.argv.slice 2) ->
printUsage() if options.files.length or options.output
process.openStdin()
read process.stdin, (source) ->
- sys.puts eco.compile source
+ sys.puts eco.precompile source
else if options.print
printUsage() if options.files.length isnt 1 or options.output
View
@@ -2,12 +2,7 @@ CoffeeScript = require "coffee-script"
{preprocess} = require "./preprocessor"
{indent} = require "./util"
-module.exports = eco = (source) ->
- do new Function "return #{compile source}"
-
-eco.preprocess = preprocess
-
-eco.compile = compile = (source) ->
+exports.precompile = precompile = (source) ->
script = CoffeeScript.compile preprocess(source), noWrap: true
"""
@@ -56,13 +51,5 @@ eco.compile = compile = (source) ->
}
"""
-eco.render = (source, data) ->
- (eco source) data
-
-if require.extensions
- require.extensions[".eco"] = (module, filename) ->
- source = require("fs").readFileSync filename, "utf-8"
- module._compile "module.exports = #{compile source}", filename
-
-else if require.registerExtension
- require.registerExtension ".eco", compile
+exports.compile = (source) ->
+ do new Function "return #{precompile source}"
View
@@ -0,0 +1,26 @@
+{compile, precompile} = require "./compiler"
+{preprocess} = require "./preprocessor"
+
+
+module.exports = eco = (source) ->
+ if eco.cache
+ eco.cache[source] ?= compile source
+ else
+ compile source
+
+eco.cache = {}
+
+eco.preprocess = preprocess
+
+eco.precompile = precompile
+
+eco.compile = compile
+
+eco.render = (source, data) ->
+ (eco source) data
+
+
+if require.extensions
+ require.extensions[".eco"] = (module, filename) ->
+ source = require("fs").readFileSync filename, "utf-8"
+ module._compile "module.exports = #{precompile source}", filename
@@ -1,23 +1,23 @@
-eco = require ".."
+{compile} = require "../lib/compiler"
{fixture} = require "./lib/fixtures"
module.exports =
"compiling fixtures/hello.eco": (test) ->
- test.ok eco.compile fixture("hello.eco")
+ test.ok compile fixture("hello.eco")
test.done()
"compiling fixtures/projects.eco": (test) ->
- test.ok eco.compile fixture("projects.eco")
+ test.ok compile fixture("projects.eco")
test.done()
"compiling fixtures/helpers.eco": (test) ->
- test.ok eco.compile fixture("helpers.eco")
+ test.ok compile fixture("helpers.eco")
test.done()
"parse error throws exception": (test) ->
test.expect 1
try
- eco.compile "<% unclosed tag"
+ compile "<% unclosed tag"
catch err
test.ok err.toString().match /^Parse error on line 1/
test.done()
@@ -7,17 +7,48 @@ items = [
]
module.exports =
- "linking hello.eco": (test) ->
+ "eco() caches compiled templates": (test) ->
render = eco fixture("hello.eco")
+ test.same render, eco fixture("hello.eco")
+ test.done()
+
+ "cache can be disabled": (test) ->
+ cache = eco.cache
+ eco.cache = false
+ render = eco fixture("hello.eco")
+ test.ok render isnt eco fixture("hello.eco")
+ eco.cache = cache
+ test.done()
+
+ "eco.preprocess": (test) ->
+ test.same fixture("hello.coffee"), eco.preprocess fixture("hello.eco")
+ test.done()
+
+ "eco.precompile": (test) ->
+ js = eco.precompile "Hello <%= @name %>"
+ test.ok typeof js is "string"
+ fn = eval "(#{js})"
+ test.ok typeof fn is "function"
+ test.same "Hello Sam", fn name: "Sam"
+ test.done()
+
+ "compiling hello.eco": (test) ->
+ render = eco.compile fixture("hello.eco")
test.same fixture("hello.out.1"), render name: "Sam"
test.done()
- "linked templates can be reused": (test) ->
- render = eco "Hello <%= @name %>"
+ "compiled templates can be reused": (test) ->
+ render = eco.compile "Hello <%= @name %>"
test.same "Hello Sam", render name: "Sam"
test.same "Hello Josh", render name: "Josh"
test.done()
+ "eco.compile bypasses cache": (test) ->
+ test.ok eco.cache
+ render = eco.compile fixture("hello.eco")
+ test.ok render isnt eco.compile fixture("hello.eco")
+ test.done()
+
"rendering hello.eco": (test) ->
output = eco.render fixture("hello.eco"), name: "Sam"
test.same fixture("hello.out.1"), output
@@ -92,3 +123,12 @@ module.exports =
"rendering an escaped <%": (test) ->
test.same "<%", eco.render "<%%"
test.done()
+
+ "requiring eco templates as modules": (test) ->
+ require.paths.unshift __dirname + "/fixtures"
+
+ hello = require "hello.eco"
+ test.ok typeof hello is "function"
+ test.same fixture("hello.out.1"), hello name: "Sam"
+
+ test.done()
View
@@ -0,0 +1,30 @@
+eco = require ".."
+express = require "express"
+http = require "http"
+
+module.exports =
+ "registering eco in an express application": (test) ->
+ test.expect 2
+
+ app = express.createServer()
+ app.register ".eco", eco
+ app.set "views", __dirname + "/fixtures"
+ app.get "/hello/:name", (req, res) ->
+ res.render "hello.eco", name: req.params.name, layout: false
+
+ app.listen ->
+ host = "localhost"
+ {port} = app.address()
+ path = "/hello/Sam"
+
+ http.get {host, port, path}, (res) ->
+ test.same 200, res.statusCode
+
+ body = ""
+ res.setEncoding "utf8"
+ res.on "data", (data) -> body += data
+ res.on "end", ->
+ test.same "Hello, Sam.\nI\'M SHOUTING AT YOU, SAM!\n", body
+
+ app.close()
+ test.done()
@@ -2,10 +2,6 @@
{fixture} = require "./lib/fixtures"
module.exports =
- "can be accessed as eco.preprocess": (test) ->
- test.same fixture("hello.coffee"), require("..").preprocess fixture("hello.eco")
- test.done()
-
"preprocessing fixtures/hello.eco": (test) ->
test.same fixture("hello.coffee"), preprocess fixture("hello.eco")
test.done()
@@ -74,4 +70,3 @@ module.exports =
test.same "", lines.pop()
test.ok lines.pop().match(/^\S/)
test.done()
-
View
@@ -1,13 +0,0 @@
-eco = require ".."
-{fixture} = require "./lib/fixtures"
-
-module.exports =
- "requiring eco templates as modules": (test) ->
- require.paths.unshift __dirname + "/fixtures"
-
- hello = require "hello.eco"
- test.ok typeof hello is "function"
- test.same fixture("hello.out.1"), hello name: "Sam"
-
- test.done()
-

2 comments on commit 5d62cdd

gasi replied Jun 3, 2011

Nice job! Could you publish this version on npm?

Owner

sstephenson replied Jun 3, 2011

It's coming as soon as I get a chance to update the documentation. For now, you can npm install https://github.com/sstephenson/eco/tarball/master.

(❤️ npm)

Please sign in to comment.