-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
:model | ||
form(user) | ||
p | ||
field(name) | ||
p | ||
field(email) | ||
buttons |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,5 +42,4 @@ jade.filters.conditionals = function(block, compiler){ | |
} | ||
}); | ||
compiler.visit(block); | ||
return ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters