Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve/inherit indentation levels. #237

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/*jshint eqnull:true*/
var Handlebars = {};

Handlebars.VERSION = "1.0.beta.5";
Handlebars.VERSION = "1.0.beta.5";

Handlebars.indent = false;
Handlebars.helpers = {};
Handlebars.partials = {};

Expand Down Expand Up @@ -97,5 +98,4 @@ Handlebars.registerHelper('log', function(context) {

// END(BROWSER)

module.exports = Handlebars;

module.exports = Handlebars;
41 changes: 28 additions & 13 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Handlebars.JavaScriptCompiler = function() {};
invokePartial: 12,
push: 13,
assignToHash: 15,
pushStringParam: 16
pushStringParam: 16,
indent: 17
};

Compiler.MULTI_PARAM_OPCODES = {
Expand All @@ -38,7 +39,8 @@ Handlebars.JavaScriptCompiler = function() {};
invokePartial: 1,
push: 1,
assignToHash: 1,
pushStringParam: 1
pushStringParam: 1,
indent: 1
};

Compiler.DISASSEMBLE_MAP = {};
Expand Down Expand Up @@ -114,7 +116,6 @@ Handlebars.JavaScriptCompiler = function() {};
this.options.knownHelpers[name] = knownHelpers[name];
}
}

return this.program(program);
},

Expand All @@ -128,6 +129,18 @@ Handlebars.JavaScriptCompiler = function() {};

for(var i=0, l=statements.length; i<l; i++) {
statement = statements[i];
if((statement.type==='partial' || statement.type==='mustache') && i>0 && statements[i-1].type==='content') {
var str = statements[i-1]['string'];
var indent = [];
for(var j=str.lastIndexOf('\n')+1, n=str.length; j<n; j++) {
if(str[j]==='\t' || str[j]===' ') {
indent.push(str[j]);
continue;
}
break;
}
statement.indent = indent.join('');
}
this[statement.type](statement);
}
this.isSimple = l === 1;
Expand Down Expand Up @@ -210,8 +223,9 @@ Handlebars.JavaScriptCompiler = function() {};
} else {
this.opcode('push', 'depth0');
}

this.opcode('invokePartial', id.original);
this.opcode('indent', partial.indent || '');
this.opcode('append');
},

Expand All @@ -223,6 +237,7 @@ Handlebars.JavaScriptCompiler = function() {};
var params = this.setupStackForMustache(mustache);

this.opcode('invokeMustache', params.length, mustache.id.original, !!mustache.hash);
this.opcode('indent', mustache.indent || '');

if(mustache.escaped && !this.options.noEscape) {
this.opcode('appendEscaped');
Expand Down Expand Up @@ -508,7 +523,13 @@ Handlebars.JavaScriptCompiler = function() {};

this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
},


indent: function(indent) {
if (typeof indent === 'string' && indent.length) {
this.pushStack("Handlebars.Utils.indent(" + this.popStack() + ",'" + indent + "')");
}
},

getContext: function(depth) {
if(this.lastContext !== depth) {
this.lastContext = depth;
Expand Down Expand Up @@ -645,12 +666,7 @@ Handlebars.JavaScriptCompiler = function() {};
},

invokePartial: function(context) {
var params = [this.nameLookup('partials', context, 'partial'), "'" + context + "'", this.popStack(), "helpers", "partials"];

if (this.options.data) {
params.push("data");
}

var params = [this.nameLookup('partials', context, 'partial'), "'" + context + "'", this.popStack(), "helpers", "partials", this.options.data ? "data" : "null"];
this.pushStack("self.invokePartial(" + params.join(", ") + ");");
},

Expand Down Expand Up @@ -804,5 +820,4 @@ Handlebars.compile = function(string, options) {
};
};

// END(BROWSER)

// END(BROWSER)
3 changes: 1 addition & 2 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,4 @@ Handlebars.VM = {

Handlebars.template = Handlebars.VM.template;

// END(BROWSER)

// END(BROWSER)
11 changes: 10 additions & 1 deletion lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ Handlebars.SafeString.prototype.toString = function() {
if(!possible.test(string)) { return string; }
return string.replace(badChars, escapeChar);
},

indent: function(src, indent) {
if(Handlebars.indent && typeof indent === 'string' && typeof src === 'string' && indent.length) {
var lines = src.split('\n');
for(var i=1; i<lines.length; i++) {
lines[i] = indent + lines[i];
}
return lines.join('\n');
}
return src;
},
isEmpty: function(value) {
if (typeof value === "undefined") {
return true;
Expand Down