vendor/assets/javascripts/haml.js in ruby-haml-js-0.0.3 vs vendor/assets/javascripts/haml.js in ruby-haml-js-0.0.4

- old
+ new

@@ -309,17 +309,59 @@ { name: "if", regexp: /^(\s*):if\s+(.*)\s*$/i, process: function () { var condition = this.matches[2]; + this.pushIfCondition([condition]); return '(function () { ' + 'if (' + condition + ') { ' + 'return (\n' + (this.render_contents() || '') + '\n);' + '} else { return ""; } }).call(this)'; } }, + // else if statements + { + name: "else if", + regexp: /^(\s*):else if\s+(.*)\s*$/i, + process: function () { + var condition = this.matches[2], + conditionsArray = this.getIfConditions()[this.getIfConditions().length - 1], + ifArray = [], + ifStatement; + for (var i=0, l=conditionsArray.length; i<l; i++) { + ifArray.push('! (' + conditionsArray[i]+')'); + } + conditionsArray.push(condition); + ifArray.push(condition); + ifStatement = 'if (' + ifArray.join(' && ') + ') { '; + return '(function () { ' + + ifStatement + + 'return (\n' + (this.render_contents() || '') + '\n);' + + '} else { return ""; } }).call(this)'; + } + }, + + // else statements + { + name: "else", + regexp: /^(\s*):else\s*$/i, + process: function () { + var conditionsArray = this.popIfCondition(), + ifArray = [], + ifStatement; + for (var i=0, l=conditionsArray.length; i<l; i++) { + ifArray.push('! (' + conditionsArray[i]+')'); + } + ifStatement = 'if (' + ifArray.join(' && ') + ') { '; + return '(function () { ' + + ifStatement + + 'return (\n' + (this.render_contents() || '') + '\n);' + + '} else { return ""; } }).call(this)'; + } + }, + // silent-comments { name: "silent-comments", regexp: /^(\s*)-#\s*(.*)\s*$/i, process: function () { @@ -441,11 +483,12 @@ ]; function compile(lines) { var block = false, - output = []; + output = [], + ifConditions = []; // If lines is a string, turn it into an array if (typeof lines === 'string') { lines = lines.trim().replace(/\n\r|\r/g, '\n').split('\n'); } @@ -473,19 +516,28 @@ contents: [], indent_level: (match[1]), matches: match, check_indent: new RegExp("^(?:\\s*|" + match[1] + " (.*))$"), process: matcher.process, + getIfConditions: function() { + return ifConditions; + }, + pushIfCondition: function(condition) { + ifConditions.push(condition); + }, + popIfCondition: function() { + return ifConditions.pop(); + }, render_contents: function () { return compile(this.contents); } }; found = true; } } }); - + // Match plain text if (!found) { output.push(function () { // Escaped plain text if (line[0] === '\\') { @@ -647,5 +699,6 @@ // Hook into module system if (typeof module !== 'undefined') { module.exports = Haml; } +