Permalink
Browse files

cleanup

  • Loading branch information...
Floby committed Mar 29, 2013
1 parent 7adeda2 commit 4708a1c32e8a307de43ffb6360ae895958ec2fc1
Showing with 22 additions and 29 deletions.
  1. +1 −0 index.js
  2. +21 −29 lib/Template.js
View
@@ -1 +1,2 @@
exports.Template = require('./lib/Template');
+exports.registerFilter = require('./lib/compile').registerFilter;
View
@@ -11,10 +11,10 @@ function Template (filename, options) {
this._filename = path.resolve(process.cwd(), filename);
this._directory = path.dirname(this._filename);
- if(this._directory == '.') this._directory = process.cwd();
this._data = {};
-
+ // set up a stream stream that will hold
+ // all the output
this._output = new StreamStream();
this._output.on('readable', function() {
this.read(0);
@@ -26,6 +26,7 @@ function Template (filename, options) {
util.inherits(Template, stream.Readable);
Template.prototype._read = function _read(size) {
+ // read from our output, this is easy!
var data = this._output.read(size);
var res;
if(data === null) {
@@ -45,27 +46,27 @@ Template.prototype.pipe = function pipe() {
Template.prototype.run = function run() {
if(this._running) return;
this._running = true;
- var self = this;
+
var print = make_print(this);
- var include = make_include(this);
+ var include = make_include(this, print);
this.getScript(function(err, script) {
if(err) return self.emit(err);
try {
script.call(
- self.data(),
+ this.data(),
print,
include,
- self.data(),
+ this.data(),
process_template._filters,
printable
);
+ this._output.end();
+ this._endPrintStream();
}
catch(e) {
- self.emit('error', e);
+ this.emit('error', e);
return false;
}
- self._output.end();
- self._endPrintStream();
});
};
@@ -77,6 +78,10 @@ Template.prototype.addStream = function addStream(s) {
this.read(0);
};
+/**
+ * turns anything into a readable stream for printing
+ * @param toPrint (mixed) what we want to print
+ */
function printable (toPrint) {
var res = new stream.PassThrough();
if(toPrint === null || typeof toPrint === 'undefined') {
@@ -96,9 +101,7 @@ function printable (toPrint) {
function isPrintable (chunk) {
switch(typeof chunk) {
case 'undefined':
- case 'null':
return false;
- break;
case 'object':
return chunk !== null;
default:
@@ -127,17 +130,12 @@ function make_print (template) {
}
}
-function make_include (template) {
+function make_include (template, print) {
return function include(filename) {
if(filename[0] !== '/')
filename = path.normalize(template._directory + '/' + filename);
var tpl = new Template(filename);
- var current = template._getPrintStream();
- if(current) {
- current.end();
- template._setPrintStream(null);
- }
- template._output.write(tpl);
+ print(tpl);
}
}
@@ -166,22 +164,17 @@ Template.prototype.getScript = function getScript(cb) {
}
try {
var tpl = process_template(data);
- }
- catch(e) {
- return this.emit('error', e);
- }
- tpl = "(function compiled_template(print, include, data, filters, __printable) {"
- + tpl
- + "})";
+ tpl = "(function compiled_template(print, include, data, filters, __printable) {"
+ + tpl
+ + "})";
- try {
var script = new Script(tpl, this._filename);
+ var f = this._sandbox ? script.runInNewContext(this._sandbox)
+ : script.runInThisContext();
} catch(e) {
this.emit('error', e);
return false;
}
- var f = this._sandbox ? script.runInNewContext(this._sandbox)
- : script.runInThisContext();
this.emit('script', null, this._script = f);
}.bind(this));
@@ -197,4 +190,3 @@ Template.prototype.data = function(data) {
};
module.exports = Template;
-

0 comments on commit 4708a1c

Please sign in to comment.