2012-01-24 16:26:37 +00:00
|
|
|
/*\
|
|
|
|
title: js/WikiTextRenderer.js
|
|
|
|
|
|
|
|
An array of JavaScript functions that generate a specified representation of a parse tree
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-02-06 10:57:55 +00:00
|
|
|
var HTML = require("./HTML.js").HTML,
|
|
|
|
utils = require("./Utils.js");
|
2012-01-24 16:26:37 +00:00
|
|
|
|
|
|
|
var WikiTextRenderer = function() {
|
2012-02-07 19:03:59 +00:00
|
|
|
this.renderSteps = []; // Array of {step: n, dependencies: {},handler: function(tiddler,renderer,store,utils) {}}
|
2012-01-24 16:26:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WikiTextRenderer.prototype.addRenderStep = function(renderStep) {
|
|
|
|
this.renderSteps.push(renderStep);
|
|
|
|
return this.renderSteps.length - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) {
|
|
|
|
renderStep = renderStep || 0;
|
2012-01-31 12:00:07 +00:00
|
|
|
var step = this.renderSteps[renderStep];
|
2012-01-24 16:26:37 +00:00
|
|
|
if(renderStep < this.renderSteps.length) {
|
2012-01-31 12:00:07 +00:00
|
|
|
switch(step.type) {
|
|
|
|
case "main":
|
|
|
|
return step.handler(tiddler,this,store,utils);
|
|
|
|
case "macro":
|
|
|
|
return store.renderMacro(step.macro,
|
|
|
|
step.renderType,
|
|
|
|
tiddler,
|
|
|
|
step.params(tiddler,this,store,utils),
|
|
|
|
step.content(tiddler,this,store,utils));
|
|
|
|
}
|
2012-01-24 16:26:37 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-01 12:37:29 +00:00
|
|
|
WikiTextRenderer.prototype.rerender = function(node,changes,tiddler,store,renderStep) {
|
|
|
|
renderStep = renderStep || 0;
|
|
|
|
var step = this.renderSteps[renderStep];
|
|
|
|
if(renderStep < this.renderSteps.length) {
|
|
|
|
switch(step.type) {
|
|
|
|
case "main":
|
|
|
|
node.innerHTML = step.handler(tiddler,this,store,utils);
|
|
|
|
break;
|
|
|
|
case "macro":
|
|
|
|
store.rerenderMacro(node,changes,step.macro,
|
|
|
|
step.renderType,
|
|
|
|
tiddler,
|
|
|
|
step.params(tiddler,this,store,utils),
|
|
|
|
step.content(tiddler,this,store,utils));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-24 16:26:37 +00:00
|
|
|
WikiTextRenderer.prototype.toString = function(type) {
|
2012-02-06 10:57:55 +00:00
|
|
|
var renderNode,
|
|
|
|
renderArray = function(tree) {
|
|
|
|
var children = [];
|
|
|
|
for(var t=0; t<tree.length; t++) {
|
|
|
|
children.push(HTML.elem("li",{
|
|
|
|
"class": ["nodeWikiTextRenderer"]
|
|
|
|
},renderNode(tree[t])));
|
2012-01-24 16:26:37 +00:00
|
|
|
}
|
2012-02-06 10:57:55 +00:00
|
|
|
return HTML.elem("ul",{
|
|
|
|
"class": ["treeWikiTextRenderer"]
|
|
|
|
},children);
|
|
|
|
};
|
|
|
|
renderNode = function(node) {
|
|
|
|
var ret = [];
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"rendererStep",
|
|
|
|
[HTML.text(node.step.toString())],
|
|
|
|
[HTML.text(node.type.toString())]
|
|
|
|
));
|
2012-02-08 12:51:46 +00:00
|
|
|
if(node.macro) {
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"macro",
|
|
|
|
[HTML.text("macro")],
|
|
|
|
[HTML.text(node.macro)]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if(node.params) {
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"params",
|
|
|
|
[HTML.text("params")],
|
|
|
|
[HTML.raw(utils.htmlEncode(node.params.toString()).replace(/\n/g,"<br>"))]
|
|
|
|
));
|
|
|
|
}
|
2012-02-07 19:03:59 +00:00
|
|
|
if(node.dependencies) {
|
|
|
|
var dependencies = [];
|
|
|
|
for(var d in node.dependencies) {
|
|
|
|
if(d === "dependentAll") {
|
|
|
|
dependencies.push(HTML.splitLabel("dependency",[HTML.text(d)],[HTML.text(node.dependencies[d])]));
|
|
|
|
} else {
|
|
|
|
var dependents = [];
|
|
|
|
for(var t in node.dependencies[d]) {
|
|
|
|
dependents.push(t);
|
|
|
|
}
|
|
|
|
dependencies.push(HTML.splitLabel("dependency",[HTML.text(d)],[HTML.text(dependents.join(","))]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"dependencies",
|
|
|
|
[HTML.text("Dependencies")],
|
|
|
|
dependencies
|
|
|
|
));
|
|
|
|
}
|
2012-02-06 10:57:55 +00:00
|
|
|
if(node.content) {
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"content",
|
|
|
|
[HTML.text("content")],
|
|
|
|
[HTML.raw(utils.htmlEncode(node.content.toString()).replace(/\n/g,"<br>"))]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if(node.handler) {
|
|
|
|
ret.push(HTML.splitLabel(
|
|
|
|
"handler",
|
|
|
|
[HTML.text("handler")],
|
|
|
|
[HTML.raw(utils.htmlEncode(node.handler.toString()).replace(/\n/g,"<br>"))]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
return HTML(renderArray(this.renderSteps),type);
|
2012-01-24 16:26:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.WikiTextRenderer = WikiTextRenderer;
|
|
|
|
|
|
|
|
})();
|