Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'nested-filters'

  • Loading branch information...
commit 91d01fa4e9f78c981c732f70f8d0184711c94fe5 2 parents b6253e9 + 740061d
@tj tj authored
View
8 examples/nested-filters.jade
@@ -0,0 +1,8 @@
+:stylesheets
+ stylesheet('/path/to/a.css')
+ stylesheet('/path/to/b.css')
+ stylesheet('/path/to/c.css')
+ :javascripts
+ js('/path/to/a.js')
+ js('/path/to/b.js')
+ js('/path/to/c.js')
View
73 examples/nested-filters.js
@@ -0,0 +1,73 @@
+
+/**
+ * Module dependencies.
+ */
+
+var jade = require('./../lib/jade'),
+ nodes = jade.nodes,
+ Compiler = jade.Compiler;
+
+jade.renderFile(__dirname + '/nested-filters.jade', function(err, html){
+ if (err) throw err;
+ 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 StylesheetsVisitor(block, compiler.options).compile();
+};
+
+function StylesheetsVisitor(node, options) {
+ Compiler.call(this, node, options);
+}
+
+// Inherit from Compiler
+
+StylesheetsVisitor.prototype.__proto__ = Compiler.prototype;
+
+// Overwrite visitTag method
+
+StylesheetsVisitor.prototype.visitTag = function(node){
+ var parent = Compiler.prototype.visitTag;
+ switch (node.name) {
+ case 'stylesheet':
+ var style = new nodes.Tag('link');
+ style.setAttribute('rel', "'stylesheet'");
+ style.setAttribute('href', "'" + node.attrs[0].name + "'")
+ parent.call(this, style);
+ break;
+ default:
+ parent.call(this, node);
+ }
+};
View
3  lib/compiler.js
@@ -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
5 lib/jade.js
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.