Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Floby committed Mar 29, 2013
1 parent 7adeda2 commit 4708a1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
1 change: 1 addition & 0 deletions index.js
@@ -1 +1,2 @@
exports.Template = require('./lib/Template'); exports.Template = require('./lib/Template');
exports.registerFilter = require('./lib/compile').registerFilter;
50 changes: 21 additions & 29 deletions lib/Template.js
Expand Up @@ -11,10 +11,10 @@ function Template (filename, options) {


this._filename = path.resolve(process.cwd(), filename); this._filename = path.resolve(process.cwd(), filename);
this._directory = path.dirname(this._filename); this._directory = path.dirname(this._filename);
if(this._directory == '.') this._directory = process.cwd();
this._data = {}; this._data = {};



// set up a stream stream that will hold
// all the output
this._output = new StreamStream(); this._output = new StreamStream();
this._output.on('readable', function() { this._output.on('readable', function() {
this.read(0); this.read(0);
Expand All @@ -26,6 +26,7 @@ function Template (filename, options) {
util.inherits(Template, stream.Readable); util.inherits(Template, stream.Readable);


Template.prototype._read = function _read(size) { Template.prototype._read = function _read(size) {
// read from our output, this is easy!
var data = this._output.read(size); var data = this._output.read(size);
var res; var res;
if(data === null) { if(data === null) {
Expand All @@ -45,27 +46,27 @@ Template.prototype.pipe = function pipe() {
Template.prototype.run = function run() { Template.prototype.run = function run() {
if(this._running) return; if(this._running) return;
this._running = true; this._running = true;
var self = this;
var print = make_print(this); var print = make_print(this);
var include = make_include(this); var include = make_include(this, print);
this.getScript(function(err, script) { this.getScript(function(err, script) {
if(err) return self.emit(err); if(err) return self.emit(err);
try { try {
script.call( script.call(
self.data(), this.data(),
print, print,
include, include,
self.data(), this.data(),
process_template._filters, process_template._filters,
printable printable
); );
this._output.end();
this._endPrintStream();
} }
catch(e) { catch(e) {
self.emit('error', e); this.emit('error', e);
return false; return false;
} }
self._output.end();
self._endPrintStream();
}); });
}; };


Expand All @@ -77,6 +78,10 @@ Template.prototype.addStream = function addStream(s) {
this.read(0); this.read(0);
}; };


/**
* turns anything into a readable stream for printing
* @param toPrint (mixed) what we want to print
*/
function printable (toPrint) { function printable (toPrint) {
var res = new stream.PassThrough(); var res = new stream.PassThrough();
if(toPrint === null || typeof toPrint === 'undefined') { if(toPrint === null || typeof toPrint === 'undefined') {
Expand All @@ -96,9 +101,7 @@ function printable (toPrint) {
function isPrintable (chunk) { function isPrintable (chunk) {
switch(typeof chunk) { switch(typeof chunk) {
case 'undefined': case 'undefined':
case 'null':
return false; return false;
break;
case 'object': case 'object':
return chunk !== null; return chunk !== null;
default: default:
Expand Down Expand Up @@ -127,17 +130,12 @@ function make_print (template) {
} }
} }


function make_include (template) { function make_include (template, print) {
return function include(filename) { return function include(filename) {
if(filename[0] !== '/') if(filename[0] !== '/')
filename = path.normalize(template._directory + '/' + filename); filename = path.normalize(template._directory + '/' + filename);
var tpl = new Template(filename); var tpl = new Template(filename);
var current = template._getPrintStream(); print(tpl);
if(current) {
current.end();
template._setPrintStream(null);
}
template._output.write(tpl);
} }
} }


Expand Down Expand Up @@ -166,22 +164,17 @@ Template.prototype.getScript = function getScript(cb) {
} }
try { try {
var tpl = process_template(data); var tpl = process_template(data);
} tpl = "(function compiled_template(print, include, data, filters, __printable) {"
catch(e) { + tpl
return this.emit('error', e); + "})";
}
tpl = "(function compiled_template(print, include, data, filters, __printable) {"
+ tpl
+ "})";


try {
var script = new Script(tpl, this._filename); var script = new Script(tpl, this._filename);
var f = this._sandbox ? script.runInNewContext(this._sandbox)
: script.runInThisContext();
} catch(e) { } catch(e) {
this.emit('error', e); this.emit('error', e);
return false; return false;
} }
var f = this._sandbox ? script.runInNewContext(this._sandbox)
: script.runInThisContext();


this.emit('script', null, this._script = f); this.emit('script', null, this._script = f);
}.bind(this)); }.bind(this));
Expand All @@ -197,4 +190,3 @@ Template.prototype.data = function(data) {
}; };


module.exports = Template; module.exports = Template;

0 comments on commit 4708a1c

Please sign in to comment.