From b235ec1f53b1831f8de4f571e85ac8efea57f28f Mon Sep 17 00:00:00 2001 From: Adam Hull Date: Sun, 7 Apr 2013 23:38:12 -0700 Subject: [PATCH] Refs #103 - Support Teacup --- lib/consolidate.js | 49 ++++++++++++++++++++++++++++---- package.json | 2 ++ test/consolidate.js | 3 +- test/fixtures/teacup/user.teacup | 4 +++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/teacup/user.teacup diff --git a/lib/consolidate.js b/lib/consolidate.js index 795c2b2..4f57665 100644 --- a/lib/consolidate.js +++ b/lib/consolidate.js @@ -1191,20 +1191,20 @@ function reactBaseTmpl(data, options){ * The main render parser for React bsaed templates */ function reactRenderer(type){ - + if (require.extensions) { - + // Ensure JSX is transformed on require if (!require.extensions['.jsx']) { require.extensions['.jsx'] = requireReact; } - + // Supporting .react extension as well as test cases // Using .react extension is not recommended. if (!require.extensions['.react']) { require.extensions['.react'] = requireReact; } - + } // Return rendering fx @@ -1367,7 +1367,44 @@ exports.slm.render = function(str, options, fn) { }; /** - * expose the instance of the engine + * Teacup support. */ +exports.teacup = function(path, options, fn) { + return promisify(fn, function(fn) { + var engine = requires.teacup || (requires.teacup = require('teacup/lib/express')); + require.extensions['.teacup'] = require.extensions['.coffee']; + if (path[0] != '/') { + path = join(process.cwd(), path); + } + if (!options.cache) { + var originalFn = fn; + fn = function() { + delete require.cache[path]; + originalFn.apply(this, arguments); + }; + } + engine.renderFile(path, options, fn); + }); +}; -exports.requires = requires; +/** + * Teacup string support. + */ +exports.teacup.render = function(str, options, fn){ + var coffee = require('coffee-script'); + var vm = require('vm'); + var sandbox = { + module: {exports: {}}, + require: require + }; + return promisify(fn, function(fn) { + vm.runInNewContext(coffee.compile(str), sandbox); + var tmpl = sandbox.module.exports; + fn(null, tmpl(options)); + }); +} + +/** + * expose the instance of the engine + */ + exports.requires = requires; diff --git a/package.json b/package.json index eac8362..007f38e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "devDependencies": { "arc-templates": "^0.5.1", "atpl": ">=0.7.6", + "coffee-script": "^1.11.1", "dot": "^1.0.1", "dust": "^0.3.0", "dustjs-helpers": "^1.1.1", @@ -42,6 +43,7 @@ "should": "*", "slm": "^0.5.0", "swig": "^1.4.1", + "teacup": "^2.0.0", "templayed": ">=0.2.3", "tinyliquid": "^0.2.22", "toffee": "^0.1.12", diff --git a/test/consolidate.js b/test/consolidate.js index 340bb5c..0b594a7 100644 --- a/test/consolidate.js +++ b/test/consolidate.js @@ -49,4 +49,5 @@ require('./shared').test('slm'); require('./shared').test('arc-templates'); require('./shared/filters').test('arc-templates'); require('./shared/includes').test('arc-templates'); -require('./shared/partials').test('arc-templates'); \ No newline at end of file +require('./shared/partials').test('arc-templates'); +require('./shared').test('teacup'); diff --git a/test/fixtures/teacup/user.teacup b/test/fixtures/teacup/user.teacup new file mode 100644 index 0000000..4328466 --- /dev/null +++ b/test/fixtures/teacup/user.teacup @@ -0,0 +1,4 @@ +{renderable, p} = require 'teacup' + +module.exports = renderable ({user}) -> + p user.name