Skip to content

Commit

Permalink
Refs #103 - Support Teacup
Browse files Browse the repository at this point in the history
  • Loading branch information
hurrymaplelad committed Oct 28, 2016
1 parent 1ac7052 commit b235ec1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
49 changes: 43 additions & 6 deletions lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
require('./shared/partials').test('arc-templates');
require('./shared').test('teacup');
4 changes: 4 additions & 0 deletions test/fixtures/teacup/user.teacup
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{renderable, p} = require 'teacup'

module.exports = renderable ({user}) ->
p user.name

0 comments on commit b235ec1

Please sign in to comment.