mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-25 01:20:30 +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:
parent
6f027c3b49
commit
6996f255ca
@ -66,6 +66,7 @@ WikiTextParseTree.prototype.compile = function(type,treenode) {
|
||||
]
|
||||
}
|
||||
]);
|
||||
renderStep.type = "main";
|
||||
renderStep.step = renderStepIndex;
|
||||
renderStep.dependencies = [];
|
||||
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 + "'**}}");
|
||||
return;
|
||||
}
|
||||
// Compose the macro call as a render function
|
||||
var macroCall = {
|
||||
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: {
|
||||
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: []
|
||||
}]
|
||||
}}]
|
||||
};
|
||||
renderStep.type = "macro";
|
||||
renderStep.macro = name;
|
||||
renderStep.renderType = type;
|
||||
renderStep.step = renderStepIndex;
|
||||
renderStep.dependencies = node.dependencies;
|
||||
// Slot the parameters into the macro call
|
||||
var properties = [];
|
||||
for(p in params) {
|
||||
if(params[p].type === "string") {
|
||||
n = {type: "StringLiteral", value: params[p].value};
|
||||
} else {
|
||||
n = this.store.jsParser.parse(params[p].value).tree.elements[0];
|
||||
}
|
||||
macroCall.elements[0].value["arguments"][3].properties.push({
|
||||
properties.push({
|
||||
type: "PropertyAssignment",
|
||||
name: p,
|
||||
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
|
||||
var subOutput = [];
|
||||
if(node.children) {
|
||||
var subOutput = [];
|
||||
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.dependencies = node.dependencies;
|
||||
renderStep.handler = eval(this.store.jsParser.createTree(macroCall).render());
|
||||
renderStep.content = 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: "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";
|
||||
if(type === "text/html" && !this.store.disableHtmlWrapperNodes) {
|
||||
pushString(output,utils.stitchElement(wrapperTag,{
|
||||
@ -183,6 +177,7 @@ WikiTextParseTree.prototype.compileMacroCall = function(output,renderer,type,nod
|
||||
"data-tw-render-step": renderStepIndex
|
||||
}));
|
||||
}
|
||||
// Output the macro call
|
||||
output.push({
|
||||
type: "FunctionCall",
|
||||
name: {
|
||||
|
@ -22,8 +22,18 @@ WikiTextRenderer.prototype.addRenderStep = function(renderStep) {
|
||||
|
||||
WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) {
|
||||
renderStep = renderStep || 0;
|
||||
var step = this.renderSteps[renderStep];
|
||||
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 {
|
||||
return null;
|
||||
}
|
||||
@ -31,36 +41,43 @@ WikiTextRenderer.prototype.render = function(tiddler,store,renderStep) {
|
||||
|
||||
WikiTextRenderer.prototype.toString = function(type) {
|
||||
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 = [
|
||||
function(output,type,node) { // Rendering step
|
||||
if(node.step !== undefined) {
|
||||
output.push(utils.stitchElement("span",
|
||||
{"data-tw-treenode-type": "renderStep"},{
|
||||
output.push(utils.stitchElement("span",null,{
|
||||
content: node.step.toString(),
|
||||
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,{
|
||||
content: utils.htmlEncode(node.dependencies === null ? "*" : node.dependencies.join(", ")),
|
||||
classes: ["splitLabelRight"]
|
||||
content: node.type.toString(),
|
||||
classes: ["treeNode","label"]
|
||||
}));
|
||||
output.push("</span>");
|
||||
output.push(utils.stitchElement("span",null,
|
||||
{classes: ["treeNode","splitLabel"]}));
|
||||
output.push(utils.stitchElement("span",{"data-tw-treenode-type": "renderStepHandler"},{
|
||||
content: "handler",
|
||||
classes: ["splitLabelLeft"]
|
||||
}));
|
||||
output.push(utils.stitchElement("code",null,{
|
||||
content: utils.htmlEncode(node.handler.toString()).replace(/\n/g,"<br>"),
|
||||
classes: ["splitLabelRight"]
|
||||
}));
|
||||
output.push("</span>");
|
||||
stitchSplitLabel("dependencies",node.dependencies === null ? "*" : node.dependencies.join(", "));
|
||||
if(node.macro) {
|
||||
stitchSplitLabel("macro",utils.htmlEncode(node.macro.toString()));
|
||||
}
|
||||
if(node.params) {
|
||||
stitchSplitLabel("params",utils.htmlEncode(node.params.toString()).replace(/\n/g,"<br>"));
|
||||
}
|
||||
if(node.content) {
|
||||
stitchSplitLabel("content",utils.htmlEncode(node.content.toString()).replace(/\n/g,"<br>"));
|
||||
}
|
||||
if(node.handler) {
|
||||
stitchSplitLabel("handler",utils.htmlEncode(node.handler.toString()).replace(/\n/g,"<br>"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user