131 lines
3.0 KiB
JavaScript
131 lines
3.0 KiB
JavaScript
var Handlebars = require("handlebars");
|
|
require("handlebars/visitor");
|
|
|
|
// BEGIN(BROWSER)
|
|
Handlebars.PrintVisitor = function() { this.padding = 0; };
|
|
Handlebars.PrintVisitor.prototype = new Handlebars.Visitor();
|
|
|
|
Handlebars.PrintVisitor.prototype.pad = function(string, newline) {
|
|
var out = "";
|
|
|
|
for(var i=0,l=this.padding; i<l; i++) {
|
|
out = out + " ";
|
|
}
|
|
|
|
out = out + string;
|
|
|
|
if(newline !== false) { out = out + "\n"; }
|
|
return out;
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.program = function(program) {
|
|
var out = this.pad("PROGRAM:"),
|
|
statements = program.statements,
|
|
inverse = program.inverse,
|
|
i, l;
|
|
|
|
this.padding++;
|
|
|
|
for(i=0, l=statements.length; i<l; i++) {
|
|
out = out + this.accept(statements[i]);
|
|
}
|
|
|
|
this.padding--;
|
|
|
|
if(inverse) {
|
|
out = out + this.pad("{{^}}");
|
|
|
|
this.padding++;
|
|
|
|
for(i=0, l=inverse.statements.length; i<l; i++) {
|
|
out = out + this.accept(inverse.statements[i]);
|
|
}
|
|
}
|
|
|
|
this.padding--;
|
|
|
|
return out;
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.block = function(block) {
|
|
var out = "";
|
|
|
|
out = out + this.pad("BLOCK:");
|
|
this.padding++;
|
|
out = out + this.accept(block.mustache);
|
|
out = out + this.accept(block.program);
|
|
this.padding--;
|
|
|
|
return out;
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.inverse = function(block) {
|
|
var out = "";
|
|
|
|
out = out + this.pad("INVERSE:");
|
|
this.padding++;
|
|
out = out + this.accept(block.mustache);
|
|
out = out + this.accept(block.program);
|
|
this.padding--;
|
|
|
|
return out;
|
|
};
|
|
|
|
|
|
Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
|
|
var params = mustache.params, paramStrings = [], hash;
|
|
|
|
for(var i=0, l=params.length; i<l; i++) {
|
|
paramStrings.push(this.accept(params[i]));
|
|
}
|
|
|
|
params = "[" + paramStrings.join(", ") + "]";
|
|
|
|
hash = mustache.hash ? " " + this.accept(mustache.hash) : "";
|
|
|
|
return this.pad("{{ " + this.accept(mustache.id) + " " + params + hash + " }}");
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.partial = function(partial) {
|
|
var content = this.accept(partial.id);
|
|
if(partial.context) { content = content + " " + this.accept(partial.context); }
|
|
return this.pad("{{> " + content + " }}");
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.hash = function(hash) {
|
|
var pairs = hash.pairs;
|
|
var joinedPairs = [], left, right;
|
|
|
|
for(var i=0, l=pairs.length; i<l; i++) {
|
|
left = pairs[i][0];
|
|
right = this.accept(pairs[i][1]);
|
|
joinedPairs.push( left + "=" + right );
|
|
}
|
|
|
|
return "HASH{" + joinedPairs.join(", ") + "}";
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.STRING = function(string) {
|
|
return '"' + string.string + '"';
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.ID = function(id) {
|
|
var path = id.parts.join("/");
|
|
if(id.parts.length > 1) {
|
|
return "PATH:" + path;
|
|
} else {
|
|
return "ID:" + path;
|
|
}
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.content = function(content) {
|
|
return this.pad("CONTENT[ '" + content.string + "' ]");
|
|
};
|
|
|
|
Handlebars.PrintVisitor.prototype.comment = function(comment) {
|
|
return this.pad("{{! '" + comment.comment + "' }}");
|
|
};
|
|
// END(BROWSER)
|
|
|
|
exports.PrintVisitor = Handlebars.PrintVisitor;
|