Skip to content

Commit

Permalink
Merge pete's refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Roberts committed Jul 29, 2014
2 parents 4129888 + b9b491c commit dfb85c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
runtime.bundle.js
bundle.js
52 changes: 24 additions & 28 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,50 +152,46 @@ function parseBlockExpression(string) {

function collectBlocks(node) {
if (!node.children) return node;

var newChildren = [];
var blocks = [];
var targetBlock;
var blockStack = [];

node.children.forEach(function (child) {
node.children.forEach(function (node) {
var block;

if (child.type === 'BlockStart') {
block = AST.BlockStatement(child.blockType, child.blockExpression);
if (node.type === 'BlockStart') {
block = AST.BlockStatement(node.blockType, node.blockExpression);
block.state = 'body';

blocks.push(block);
return;
blockStack.push(block);
}

if (child.type === 'BlockElse') {
blocks[blocks.length - 1].state = 'alternate';
return;
else if (node.type === 'BlockElse') {
last(blockStack).state = 'alternate';
}
else {
if (node.type === 'BlockEnd') {
node = blockStack.pop();
delete node.state;
}

if (child.type === 'BlockEnd') {
block = blocks.pop();
delete block.state;

if (blocks.length > 0) {
targetBlock = blocks[blocks.length - 1];
targetBlock[targetBlock.state].push(block);
} else {
newChildren.push(block);
var currentNodeList;
if (blockStack.length) {
block = last(blockStack);
currentNodeList = block[block.state];
}
else {
currentNodeList = newChildren;
}
return;
}

if (blocks[blocks.length - 1]) {
block = blocks[blocks.length - 1];
block[block.state].push(child);
} else {
newChildren.push(child);
currentNodeList.push(node);
}
});

node.children = newChildren.map(collectBlocks);
return node;
}

function last(arr) {
return arr[arr.length - 1];
}
module.exports = parse;
module.exports.parseHTML = parseHTML;

0 comments on commit dfb85c5

Please sign in to comment.