Skip to content

Commit

Permalink
Merge branch 'parsetree-examples'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 24, 2010
2 parents 12e96a8 + 8bd40e4 commit dbf7807
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
13 changes: 13 additions & 0 deletions examples/block-expansion.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ul
li
a(href="#") foo
li
a(href="#") bar
li
a(href="#") baz

// is the same as
li> a(href="#") foo
li> a(href="#") bar
li> a(href="#") baz
11 changes: 11 additions & 0 deletions examples/block-expansion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* Module dependencies.
*/

var jade = require('./../lib/jade');

jade.renderFile(__dirname + '/block-expansion.jade', function(err, html){
if (err) throw err;
console.log(html);
});
7 changes: 7 additions & 0 deletions examples/model.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:model
form(user)
p
field(name)
p
field(email)
buttons
84 changes: 84 additions & 0 deletions examples/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@


/**
* Module dependencies.
*/

var jade = require('./../lib/jade'),
Compiler = jade.Compiler,
nodes = jade.nodes;

var options = {
locals: {
user: {
name: 'tj',
email: 'vision-media.ca',
errors: { email: 'Invalid email' },
new: true
}
}
};

jade.renderFile(__dirname + '/model.jade', options, function(err, html){
if (err) throw err;
console.log(html);
});

jade.filters.model = function(block, compiler){
return new Visitor(block).compile();
};

function Visitor(node) {
this.node = node;
}

Visitor.prototype.__proto__ = Compiler.prototype;

Visitor.prototype.visitTag = function(node){
var parent = Compiler.prototype.visitTag;
switch (node.name) {
case 'form':
this.record = node.attrs[0].name;
node = new nodes.Tag('form');
node.addAttribute('id', '"' + this.record + '-model-form"');
node.addAttribute('method', '"post"');
parent.call(this, node);
break;
case 'field':
var name = node.attrs[0].name,
capitalized = name.charAt(0).toUpperCase() + name.slice(1);

var label = new nodes.Tag('label');
label.addAttribute('for', '"' + this.record + '[' + name + ']"');
label.block = new nodes.Block;
label.block.push(new nodes.Text(capitalized + ':'));
parent.call(this, label);

node = new nodes.Tag('input');
node.addAttribute('type', '"text"');
node.addAttribute('name', '"' + this.record + '[' + name + ']"');
node.addAttribute('value', this.record + '.' + name);
parent.call(this, node);

var err = this.record + '.errors.' + name;
node = new nodes.Code('if (' + err + ')', false, false);
node.block = new nodes.Block;
var p = new nodes.Tag('p');
p.block = new nodes.Block;
p.block.push(new nodes.Code(err, true, true));
node.block.push(p);
Visitor.prototype.visitCode.call(this, node);
break;
case 'buttons':
node = new nodes.Tag('input');
node.addAttribute('type', '"submit"');
node.addAttribute('value', this.record + '.new ? "Save" : "Update"');
var p = new nodes.Tag('p');
p.addAttribute('class', '"buttons"');
p.block.push(node);
parent.call(this, p);
break;
default:
parent.call(this, node);
}
};
1 change: 0 additions & 1 deletion examples/parsetree.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ jade.filters.conditionals = function(block, compiler){
}
});
compiler.visit(block);
return '';
};
1 change: 1 addition & 0 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var nodes = require('./nodes'),
*/

var Compiler = module.exports = function Compiler(node, options) {
options = options || {};
this.node = node;
this.pretty = options.pretty;
this.indents = options.indents || 0;
Expand Down

0 comments on commit dbf7807

Please sign in to comment.