Skip to content

Commit

Permalink
Merge branch 'bugs/newlines'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 4, 2010
2 parents 95c0a73 + 25d6dae commit 18d2713
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

jade.renderFile(__dirname + '/layout.jade', {debug:true},function(err, html){
jade.renderFile(__dirname + '/layout.jade', function(err, html){
if (err) throw err;
console.log(html);
});
11 changes: 11 additions & 0 deletions examples/whitespace.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- var js = '<script></script>'
!!! 5
html

head
title= "Some " + "JavaScript"
!= js



body
11 changes: 11 additions & 0 deletions examples/whitespace.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 + '/whitespace.jade', {debug:true},function(err, html){
if (err) throw err;
console.log(html);
});
2 changes: 1 addition & 1 deletion lib/jade.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Parser = require('./parser'),
* Library version.
*/

exports.version = '0.5.0';
exports.version = '0.5.1';

/**
* Intermediate JavaScript cache.
Expand Down
5 changes: 2 additions & 3 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,14 @@ Lexer.prototype = {
var tok = this.tok('indent', captures[1]),
indents = tok.val.length / 2;
if (this.input[0] === '\n') {
tok.type = 'newline';
return tok;
return this.next;
} else if (indents % 1 !== 0) {
throw new Error('Invalid indentation, got '
+ tok.val.length + ' space'
+ (tok.val.length > 1 ? 's' : '')
+ ', must be a multiple of two.');
} else if (indents === this.lastIndents) {
tok.type = 'newline';
return this.next;
} else if (indents > this.lastIndents + 1) {
throw new Error('Invalid indentation, got '
+ indents + ' expected '
Expand Down
19 changes: 4 additions & 15 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ Parser.prototype = {
* | text
* | each
* | code
* | newline
* | id
* | class
*/
Expand All @@ -164,9 +163,6 @@ Parser.prototype = {
return this.parseEach();
case 'code':
return this.parseCode();
case 'newline':
this.advance;
return this.parseExpr();
case 'id':
case 'class':
var tok = this.advance;
Expand Down Expand Up @@ -253,19 +249,15 @@ Parser.prototype = {
},

/**
* indent (text | newline)* outdent
* indent text* outdent
*/

parseTextBlock: function(){
var text = new nodes.Text;
text.line = this.line;
this.expect('indent');
while (this.peek.type === 'text' || this.peek.type === 'newline') {
if (this.peek.type === 'newline') {
this.advance;
} else {
text.push(this.advance.val);
}
while (this.peek.type === 'text') {
text.push(this.advance.val);
}
this.expect('outdent');
return text;
Expand All @@ -287,7 +279,7 @@ Parser.prototype = {
},

/**
* tag (attrs | class | id)* (text | code)? newline* block?
* tag (attrs | class | id)* (text | code)? block?
*/

parseTag: function(){
Expand Down Expand Up @@ -328,9 +320,6 @@ Parser.prototype = {
break;
}

// newline*
while (this.peek.type === 'newline') this.advance;

// block?
if (this.peek.type === 'indent') {
var block = this.parseBlock();
Expand Down
1 change: 0 additions & 1 deletion test/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var jade = require('jade'),
render = jade.render,
nodes = jade.nodes;


jade.filters.conditionals = function(block, compiler){
return new Visitor(block).compile();
};
Expand Down
22 changes: 22 additions & 0 deletions test/jade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,28 @@ module.exports = {
].join('');

assert.equal(html, render(str));

var str = [
'html',
' ',
' head',
' != "test"',
' ',
' ',
' ',
' body'
].join('\n');

var html = [
'<html>',
'<head>',
'test',
'</head>',
'<body></body>',
'</html>'
].join('');

assert.equal(html, render(str));
},

'test cache': function(assert){
Expand Down

0 comments on commit 18d2713

Please sign in to comment.