Further refinement of readable pretty-printing of tokens.

Change-Id: I8343e5c0e2a17116920b8585ce8e5d9bc8826286
This commit is contained in:
Subramanya Sastry 2012-07-13 14:08:10 -05:00
parent 6999246fec
commit c148470204
2 changed files with 53 additions and 26 deletions

View file

@ -163,7 +163,7 @@ ListHandler.prototype.doListItem = function ( bs, bn, token ) {
} }
res = tokens; res = tokens;
} }
this.manager.env.tracer.output("Returning: " + TagTk.toStringTokens(res, ",")); this.manager.env.tracer.output("Returning: " + TagTk.toStringTokens(res).join(","));
this.manager.env.tracer.endPass("doListItem"); this.manager.env.tracer.endPass("doListItem");
return res; return res;
}; };

View file

@ -14,18 +14,18 @@ function TagTk( name, attribs, dataAttribs ) {
} }
// SSS: Hacky! // SSS: Hacky!
TagTk.toStringTokens = function(tokens, separator) { TagTk.toStringTokens = function(tokens, indent) {
if (!indent) indent = "";
if (tokens.constructor !== Array) { if (tokens.constructor !== Array) {
return tokens.toString(); return [tokens.toString(false, indent)];
} else if (tokens.length === 0) { } else if (tokens.length === 0) {
return null; return [null];
} else { } else {
var buf = []; var buf = [];
for (var i = 0, n = tokens.length; i < n; i++) { for (var i = 0, n = tokens.length; i < n; i++) {
buf.push(tokens[i].toString()); buf.push(tokens[i].toString(false, indent));
} }
if (!separator) separator = "\n"; return buf;
return buf.join(separator);
} }
} }
@ -56,8 +56,10 @@ TagTk.prototype = {
}, },
toString: function() { toString: function() {
var buf = [];
if (this.dataAttribs.stx && this.dataAttribs.stx === "html") { if (this.dataAttribs.stx && this.dataAttribs.stx === "html") {
return "<HTML:" + this.name + ">"; return "<HTML:" + this.name + ">";
return buf.join();
} else { } else {
var f = this.tagToStringFns[this.name]; var f = this.tagToStringFns[this.name];
return f ? f.bind(this)() : this.defaultToString(); return f ? f.bind(this)() : this.defaultToString();
@ -100,35 +102,60 @@ SelfclosingTagTk.prototype = {
return $.extend( { type: 'SelfclosingTagTk' }, this ); return $.extend( { type: 'SelfclosingTagTk' }, this );
}, },
defaultToString: function(compact) { defaultToString: function(compact, indent) {
if (compact) { function multiTokenArgToString(key, arg, indent, newIndent) {
var buf = "<" + this.name + ">:"; var present = true;
return (this.attribs[0]) ? buf + TagTk.toStringTokens(this.attribs[0].k) : buf; var toks = TagTk.toStringTokens(arg, newIndent);
} else { var str = toks.join("\n" + newIndent);
var buf = ["<" + this.name + ">:{"];
for (var i = 0, n = this.attribs.length; i < n; i++) { if (toks.length > 1 || str[0] === '<') {
var a = this.attribs[i]; str = [key, ":{\n", newIndent, str, "\n", indent, "}"].join('');
var kStr = TagTk.toStringTokens(a.k); } else {
if (kStr) buf.push("k={" + kStr + "}"); present = (str !== '');
else buf.push("k=");
var vStr = TagTk.toStringTokens(a.v);
if (vStr) buf.push("v={" + vStr + "}");
else buf.push("v=");
} }
buf.push("}"); return {present: present, str: str};
return buf.join("\n"); }
if (compact) {
var buf = "<" + this.name + ">:";
return (this.attribs[0]) ? buf + TagTk.toStringTokens(this.attribs[0].k, "\n") : buf;
} else {
if (!indent) indent = "";
var indentIncrement = " ";
var origIndent = indent;
var buf = [];
indent = indent + indentIncrement;
for (var i = 0, n = this.attribs.length; i < n; i++) {
var a = this.attribs[i];
var newIndent = indent + indentIncrement;
var kVal = multiTokenArgToString("k", a.k, indent, newIndent);
var vVal = multiTokenArgToString("v", a.v, indent, newIndent);
if (kVal.present && vVal.present) {
buf.push([kVal.str, "=", vVal.str].join(''));
} else {
if (kVal.present) {
buf.push(kVal.str);
}
if (vVal.present) {
buf.push(vVal.str);
}
}
}
return ["<", this.name, ">(\n", indent, buf.join("\n" + indent + "|"), "\n", origIndent, ")"].join('');
} }
}, },
tagToStringFns: { }, tagToStringFns: { },
toString: function(compact) { toString: function(compact, indent) {
if (this.dataAttribs.stx && this.dataAttribs.stx === "html") { if (this.dataAttribs.stx && this.dataAttribs.stx === "html") {
return "<HTML:" + this.name + " />"; return "<HTML:" + this.name + " />";
} else { } else {
var f = this.tagToStringFns[this.name]; var f = this.tagToStringFns[this.name];
return f ? f.bind(this)(compact) : this.defaultToString(compact); return f ? f.bind(this)(compact, indent) : this.defaultToString(compact, indent);
} }
} }
}; };