1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-12 18:30:27 +00:00

Refactored renderer structure

This is to enable macros to be re-rendered as part of the refresh
process
This commit is contained in:
Jeremy Ruston 2012-01-31 12:00:07 +00:00
parent 6f027c3b49
commit 6996f255ca
2 changed files with 93 additions and 81 deletions

View File

@ -66,6 +66,7 @@ WikiTextParseTree.prototype.compile = function(type,treenode) {
] ]
} }
]); ]);
renderStep.type = "main";
renderStep.step = renderStepIndex; renderStep.step = renderStepIndex;
renderStep.dependencies = []; renderStep.dependencies = [];
renderStep.handler = eval(parseTree.render()); renderStep.handler = eval(parseTree.render());
@ -102,80 +103,73 @@ WikiTextParseTree.prototype.compileMacroCall = function(output,renderer,type,nod
pushString(output,"{{** Macro '" + name + "' cannot render to MIME type '" + type + "'**}}"); pushString(output,"{{** Macro '" + name + "' cannot render to MIME type '" + type + "'**}}");
return; return;
} }
// Compose the macro call as a render function renderStep.type = "macro";
var macroCall = { renderStep.macro = name;
type: "Function", renderStep.renderType = type;
name: null, renderStep.step = renderStepIndex;
params: ["tiddler","renderer","store","utils"], // These are the parameters passed to the tiddler function; must match the invocation in WikiStore.renderTiddler() renderStep.dependencies = node.dependencies;
elements: [ {
type: "ReturnStatement",
value: {
type: "FunctionCall",
name: {
base: {
base: {
base: {
name: "store",
type: "Variable"},
name: "macros",
type: "PropertyAccess"},
name: {
type: "StringLiteral",
value: name},
type: "PropertyAccess"},
name: "render",
type: "PropertyAccess"},
"arguments": [ {
type: "StringLiteral",
value: type
},{
type: "Variable",
name: "tiddler"
},{
type: "Variable",
name: "store"
},{
type: "ObjectLiteral",
properties: []
}]
}}]
};
// Slot the parameters into the macro call // Slot the parameters into the macro call
var properties = [];
for(p in params) { for(p in params) {
if(params[p].type === "string") { if(params[p].type === "string") {
n = {type: "StringLiteral", value: params[p].value}; n = {type: "StringLiteral", value: params[p].value};
} else { } else {
n = this.store.jsParser.parse(params[p].value).tree.elements[0]; n = this.store.jsParser.parse(params[p].value).tree.elements[0];
} }
macroCall.elements[0].value["arguments"][3].properties.push({ properties.push({
type: "PropertyAssignment", type: "PropertyAssignment",
name: p, name: p,
value: n value: n
}); });
} }
renderStep.params = eval(this.store.jsParser.createTree([
{
type: "Function",
name: null,
params: ["tiddler","renderer","store","utils"], // These are the parameters passed to the tiddler function; must match the invocation in WikiStore.renderTiddler()
elements: [ {
type: "ReturnStatement",
value: {
type: "ObjectLiteral",
properties: properties
}
} ]
}
]).render());
// Compile any child nodes // Compile any child nodes
var subOutput = [];
if(node.children) { if(node.children) {
var subOutput = [];
this.compileSubTreeHtml(subOutput,renderer,node.children); this.compileSubTreeHtml(subOutput,renderer,node.children);
macroCall.elements[0].value["arguments"].push({
type: "FunctionCall",
name: {
type: "PropertyAccess",
base: {
type: "ArrayLiteral",
elements: subOutput
},
name: "join"
},
"arguments": [ {
type: "StringLiteral",
value: ""
}]
});
} }
renderStep.step = renderStepIndex; renderStep.content = eval(this.store.jsParser.createTree([
renderStep.dependencies = node.dependencies; {
renderStep.handler = eval(this.store.jsParser.createTree(macroCall).render()); type: "Function",
name: null,
params: ["tiddler","renderer","store","utils"], // These are the parameters passed to the tiddler function; must match the invocation in WikiStore.renderTiddler()
elements: [
{
type: "ReturnStatement",
value: {
type: "FunctionCall",
name: {
type: "PropertyAccess",
base: {
type: "ArrayLiteral",
elements: subOutput
},
name: "join"
},
"arguments": [ {
type: "StringLiteral",
value: ""
}
]
}
}
]
}
]).render());
// Add the wrapper node
var wrapperTag = macro.wrapperTag || "div"; var wrapperTag = macro.wrapperTag || "div";
if(type === "text/html" && !this.store.disableHtmlWrapperNodes) { if(type === "text/html" && !this.store.disableHtmlWrapperNodes) {
pushString(output,utils.stitchElement(wrapperTag,{ pushString(output,utils.stitchElement(wrapperTag,{
@ -183,6 +177,7 @@ WikiTextParseTree.prototype.compileMacroCall = function(output,renderer,type,nod
"data-tw-render-step": renderStepIndex "data-tw-render-step": renderStepIndex
})); }));
} }
// Output the macro call
output.push({ output.push({
type: "FunctionCall", type: "FunctionCall",
name: { name: {

View File

@ -22,8 +22,18 @@ WikiTextRenderer.prototype.addRenderStep = function(renderStep) {
WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) { WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) {
renderStep = renderStep || 0; renderStep = renderStep || 0;
var step = this.renderSteps[renderStep];
if(renderStep < this.renderSteps.length) { if(renderStep < this.renderSteps.length) {
return this.renderSteps[renderStep].handler(tiddler,this,store,utils); 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));
}
} else { } else {
return null; return null;
} }
@ -31,36 +41,43 @@ WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) {
WikiTextRenderer.prototype.toString = function(type) { WikiTextRenderer.prototype.toString = function(type) {
var output = [], var output = [],
stitchSplitLabel = function(name,value) {
output.push(utils.stitchElement("span",null,
{classes: ["treeNode","splitLabel"]}));
output.push(utils.stitchElement("span",null,{
content: name,
classes: ["splitLabelLeft"]
}));
output.push(utils.stitchElement("code",null,{
content: value,
classes: ["splitLabelRight"]
}));
output.push("</span>");
},
customTemplates = [ customTemplates = [
function(output,type,node) { // Rendering step function(output,type,node) { // Rendering step
if(node.step !== undefined) { if(node.step !== undefined) {
output.push(utils.stitchElement("span", output.push(utils.stitchElement("span",null,{
{"data-tw-treenode-type": "renderStep"},{
content: node.step.toString(), content: node.step.toString(),
classes: ["treeNode","label"] classes: ["treeNode","label"]
})); }));
output.push(utils.stitchElement("span",null,
{classes: ["treeNode","splitLabel"]}));
output.push(utils.stitchElement("span",{"data-tw-treenode-type": "renderStepDependencies"},{
content: "dependencies",
classes: ["splitLabelLeft"]
}));
output.push(utils.stitchElement("span",null,{ output.push(utils.stitchElement("span",null,{
content: utils.htmlEncode(node.dependencies === null ? "*" : node.dependencies.join(", ")), content: node.type.toString(),
classes: ["splitLabelRight"] classes: ["treeNode","label"]
})); }));
output.push("</span>"); stitchSplitLabel("dependencies",node.dependencies === null ? "*" : node.dependencies.join(", "));
output.push(utils.stitchElement("span",null, if(node.macro) {
{classes: ["treeNode","splitLabel"]})); stitchSplitLabel("macro",utils.htmlEncode(node.macro.toString()));
output.push(utils.stitchElement("span",{"data-tw-treenode-type": "renderStepHandler"},{ }
content: "handler", if(node.params) {
classes: ["splitLabelLeft"] stitchSplitLabel("params",utils.htmlEncode(node.params.toString()).replace(/\n/g,"<br>"));
})); }
output.push(utils.stitchElement("code",null,{ if(node.content) {
content: utils.htmlEncode(node.handler.toString()).replace(/\n/g,"<br>"), stitchSplitLabel("content",utils.htmlEncode(node.content.toString()).replace(/\n/g,"<br>"));
classes: ["splitLabelRight"] }
})); if(node.handler) {
output.push("</span>"); stitchSplitLabel("handler",utils.htmlEncode(node.handler.toString()).replace(/\n/g,"<br>"));
}
return true; return true;
} }
return false; return false;