Permalink
Browse files

Fixed nested filter support

  • Loading branch information...
tj committed Oct 11, 2010
1 parent 2a93be3 commit 740061d00aa81157daa5fd3257c565510dfd48a0
Showing with 46 additions and 8 deletions.
  1. +5 −1 examples/nested-filters.jade
  2. +36 −4 examples/nested-filters.js
  3. +1 −2 lib/compiler.js
  4. +4 −1 lib/jade.js
@@ -1,4 +1,8 @@
:stylesheets
stylesheet('/path/to/a.css')
stylesheet('/path/to/b.css')
- stylesheet('/path/to/c.css')
+ stylesheet('/path/to/c.css')
+ :javascripts
+ js('/path/to/a.js')
+ js('/path/to/b.js')
+ js('/path/to/c.js')
View
@@ -12,21 +12,53 @@ jade.renderFile(__dirname + '/nested-filters.jade', function(err, html){
console.log(html);
});
+// :javascripts
+
+jade.filters.javascripts = function(block, compiler){
+ return new JavascriptsVisitor(block, compiler.options).compile();
+};
+
+function JavascriptsVisitor(node, options) {
+ Compiler.call(this, node, options);
+}
+
+// Inherit from Compiler
+
+JavascriptsVisitor.prototype.__proto__ = Compiler.prototype;
+
+// Overwrite visitTag method
+
+JavascriptsVisitor.prototype.visitTag = function(node){
+ var parent = Compiler.prototype.visitTag;
+ switch (node.name) {
+ case 'js':
+ var script = new nodes.Tag('script');
+ script.setAttribute('type', "'text/javascript'");
+ script.setAttribute('src', "'" + node.attrs[0].name + "'")
+ parent.call(this, script);
+ break;
+ default:
+ parent.call(this, node);
+ }
+};
+
+// :stylesheets
+
jade.filters.stylesheets = function(block, compiler){
- return new Visitor(block, compiler.options).compile();
+ return new StylesheetsVisitor(block, compiler.options).compile();
};
-function Visitor(node, options) {
+function StylesheetsVisitor(node, options) {
Compiler.call(this, node, options);
}
// Inherit from Compiler
-Visitor.prototype.__proto__ = Compiler.prototype;
+StylesheetsVisitor.prototype.__proto__ = Compiler.prototype;
// Overwrite visitTag method
-Visitor.prototype.visitTag = function(node){
+StylesheetsVisitor.prototype.visitTag = function(node){
var parent = Compiler.prototype.visitTag;
switch (node.name) {
case 'stylesheet':
View
@@ -41,9 +41,8 @@ Compiler.prototype = {
*/
compile: function(){
- this.buf = ['var buf = [];'];
+ this.buf = [];
this.visit(this.node);
- this.buf.push("return buf.join('');");
return this.buf.join('\n');
},
View
@@ -178,7 +178,10 @@ function parse(str, options){
}
try {
- return new Function('locals', 'with (locals) {' + js + '}');
+ return new Function('locals', ''
+ + 'var buf = [];\n'
+ + 'with (locals) {' + js + '}'
+ + 'return buf.join("");');
} catch (err) {
process.compile(js, filename || 'Jade');
return;

0 comments on commit 740061d

Please sign in to comment.