1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-06-26 23:22:55 +00:00

Added template support to list macro

This commit is contained in:
Jeremy Ruston 2011-12-16 10:48:36 +00:00
parent d3ede70757
commit 0823beb85d
4 changed files with 58 additions and 57 deletions

View File

@ -44,7 +44,13 @@ WikiStore.prototype.deleteTiddler = function(title) {
}; };
WikiStore.prototype.tiddlerExists = function(title) { WikiStore.prototype.tiddlerExists = function(title) {
return this.tiddlers[title] instanceof Tiddler; var exists = this.tiddlers[title] instanceof Tiddler;
if(exists) {
return true;
} else if (this.shadows) {
return this.shadows.tiddlerExists(title);
}
return ;
}; };
WikiStore.prototype.addTiddler = function(tiddler) { WikiStore.prototype.addTiddler = function(tiddler) {

View File

@ -3,23 +3,6 @@ title: js/WikiTextParser.js
Parses a block of tiddlywiki-format wiki text into a parse tree object. Parses a block of tiddlywiki-format wiki text into a parse tree object.
HTML elements are stored in the tree like this:
{type: "div", attributes: {
attr1: value,
style: {
name: value,
name2: value2
}
}, children: [
{child},
{child},
]}
Text nodes are:
{type: "text", value: "string of text node"}
\*/ \*/
(function(){ (function(){

View File

@ -66,6 +66,9 @@ WikiTextRenderer.prototype.renderAsHtml = function(treenode) {
case "img": case "img":
renderElement(tree[t],true); // Self closing elements renderElement(tree[t],true); // Self closing elements
break; break;
case "context":
renderSubTree(tree[t].children);
break;
case "macro": case "macro":
renderSubTree(tree[t].output); renderSubTree(tree[t].output);
break; break;
@ -75,7 +78,7 @@ WikiTextRenderer.prototype.renderAsHtml = function(treenode) {
} }
} }
}; };
this.executeMacros(treenode); this.executeMacros(treenode,this.title);
renderSubTree(treenode); renderSubTree(treenode);
return output.join(""); return output.join("");
}; };
@ -105,27 +108,28 @@ WikiTextRenderer.prototype.renderAsText = function(treenode) {
} }
} }
}; };
this.executeMacros(treenode); this.executeMacros(treenode,this.title);
renderSubTree(treenode); renderSubTree(treenode);
return output.join(""); return output.join("");
}; };
WikiTextRenderer.prototype.executeMacros = function(tree) { WikiTextRenderer.prototype.executeMacros = function(tree,title) {
for(var t=0; t<tree.length; t++) { for(var t=0; t<tree.length; t++) {
if(tree[t].type === "macro") { if(tree[t].type === "macro") {
this.executeMacro(tree[t]); this.executeMacro(tree[t],title);
} }
if(tree[t].children) { if(tree[t].children) {
this.executeMacros(tree[t].children); var contextForChildren = tree[t].type === "context" ? tree[t].tiddler : title;
this.executeMacros(tree[t].children,contextForChildren);
} }
} }
}; };
WikiTextRenderer.prototype.executeMacro = function(macroNode) { WikiTextRenderer.prototype.executeMacro = function(macroNode,title) {
var macroInfo = WikiTextRenderer.macros[macroNode.name]; var macroInfo = WikiTextRenderer.macros[macroNode.name];
macroNode.output = []; macroNode.output = [];
if(macroInfo) { if(macroInfo) {
macroInfo.handler.call(this,macroNode); macroInfo.handler.call(this,macroNode,title);
} else { } else {
macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name}); macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name});
} }
@ -135,44 +139,54 @@ WikiTextRenderer.versionTiddlyWiki = "2.6.5";
WikiTextRenderer.macros = { WikiTextRenderer.macros = {
allTags: { allTags: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
br: { br: {
handler: function(macroNode) { handler: function(macroNode,title) {
macroNode.output.push({type: "br"}); macroNode.output.push({type: "br"});
} }
}, },
list: { list: {
handler: function(macroNode) { handler: function(macroNode,title) {
var args = new ArgParser(macroNode.params,{defaultName:"type"}), var args = new ArgParser(macroNode.params,{defaultName:"type"}),
type = args.getValueByName("type","all"), type = args.getValueByName("type","all"),
template = args.getValueByName("template",null), template = args.getValueByName("template",null),
templateType = "text/x-tiddlywiki", templateText = "<<view title link>>",
emptyMessage = args.getValueByName("emptyMessage",null); emptyMessage = args.getValueByName("emptyMessage",null);
// Get the template to use (currently it's ignored though) // Get the template to use
template = template ? this.store.getTiddlerText(template,null) : null; template = template ? this.store.getTiddler(template) : null;
template = template || "<<view title link>>"; if(template) {
// Get the tiddlers templateType = template.fields.type;
templateText = template.fields.text;
}
// Get the handler and the tiddlers
var handler = WikiTextRenderer.macros.list.types[type]; var handler = WikiTextRenderer.macros.list.types[type];
handler = handler || WikiTextRenderer.macros.list.types.all; handler = handler || WikiTextRenderer.macros.list.types.all;
var tiddlers = handler.call(this); var tiddlers = handler.call(this);
// Render them as a list // Render them as a list
var ul = {type: "ul", children: []}; var ul = {type: "ul", children: []};
for(var t=0; t<tiddlers.length; t++) { for(var t=0; t<tiddlers.length; t++) {
var title = tiddlers[t], var li = {
li = {
type: "li", type: "li",
children: [ { children: [ {
type: "a", type: "context",
attributes: { tiddler: tiddlers[t],
href: title}, children: []
children: [ { } ]
type: "text", value: title };
}]} var parseTree = this.parser.processor.textProcessors.parse(templateType,templateText);
]}; for(var c=0; c<parseTree.children.length; c++) {
li.children[0].children.push(parseTree.children[c]);
}
ul.children.push(li); ul.children.push(li);
} }
macroNode.output.push(ul); if(ul.children.length > 0) {
macroNode.output.push(ul);
this.executeMacros(macroNode.output,title);
} else if (emptyMessage) {
macroNode.output.push({type: "text", value: emptyMessage});
}
}, },
types: { types: {
all: function() { all: function() {
@ -198,27 +212,27 @@ WikiTextRenderer.macros = {
} }
}, },
slider: { slider: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
tabs: { tabs: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
tag: { tag: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
tagging: { tagging: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
tags: { tags: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
tiddler: { tiddler: {
handler: function(macroNode) { handler: function(macroNode,title) {
var args = new ArgParser(macroNode.params,{defaultName:"name",cascadeDefaults:true}), var args = new ArgParser(macroNode.params,{defaultName:"name",cascadeDefaults:true}),
targetTitle = args.getValueByName("name",null), targetTitle = args.getValueByName("name",null),
withTokens = args.getValuesByName("with",[]), withTokens = args.getValuesByName("with",[]),
@ -234,15 +248,15 @@ WikiTextRenderer.macros = {
macroNode.output.push(parseTree.children[t]); macroNode.output.push(parseTree.children[t]);
} }
// Execute any macros in the copy // Execute any macros in the copy
this.executeMacros(macroNode.output); this.executeMacros(macroNode.output,title);
} }
}, },
timeline: { timeline: {
handler: function(macroNode) { handler: function(macroNode,title) {
} }
}, },
today: { today: {
handler: function(macroNode) { handler: function(macroNode,title) {
var now = new Date(), var now = new Date(),
args = new ArgParser(macroNode.params,{noNames:true}), args = new ArgParser(macroNode.params,{noNames:true}),
value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString(); value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString();
@ -250,16 +264,16 @@ WikiTextRenderer.macros = {
} }
}, },
version: { version: {
handler: function(macroNode) { handler: function(macroNode,title) {
macroNode.output.push({type: "text", value: WikiTextRenderer.versionTiddlyWiki}); macroNode.output.push({type: "text", value: WikiTextRenderer.versionTiddlyWiki});
} }
}, },
view: { view: {
handler: function(macroNode) { handler: function(macroNode,title) {
var args = new ArgParser(macroNode.params,{noNames:true}), var args = new ArgParser(macroNode.params,{noNames:true}),
field = args.byPos[0] ? args.byPos[0].v : null, field = args.byPos[0] ? args.byPos[0].v : null,
format = args.byPos[1] ? args.byPos[1].v : "text", format = args.byPos[1] ? args.byPos[1].v : "text",
tiddler = this.store.getTiddler(this.title), tiddler = this.store.getTiddler(title),
value = tiddler ? tiddler.fields[field] : null; value = tiddler ? tiddler.fields[field] : null;
if(tiddler && field && value) { if(tiddler && field && value) {
switch(format) { switch(format) {
@ -283,7 +297,7 @@ WikiTextRenderer.macros = {
macroNode.output.push(parseTree.children[t]); macroNode.output.push(parseTree.children[t]);
} }
// Execute any macros in the copy // Execute any macros in the copy
this.executeMacros(macroNode.output); this.executeMacros(macroNode.output,title);
break; break;
case "date": case "date":
var template = args.byPos[2] ? args.byPos[2].v : "DD MMM YYYY"; var template = args.byPos[2] ? args.byPos[2].v : "DD MMM YYYY";

View File

@ -8,5 +8,3 @@ TiddlyWiki is a unique [[wiki|WikiWikiWeb]] that people [[love using|Raves]] to
TiddlyWiki is written in [[HTML]], [[CSS]] and JavaScript to run on any reasonably modern [[browser|Browsers]] without needing any ServerSide logic. It allows anyone to create personal SelfContained hypertext documents that can be published to a WebServer, sent by email, stored in a DropBox or kept on a USB thumb drive to make a WikiOnAStick. Because it doesn't need to be installed and configured it makes a great GuerillaWiki. This is revision <<version>> of TiddlyWiki, and is published under an OpenSourceLicense. TiddlyWiki is written in [[HTML]], [[CSS]] and JavaScript to run on any reasonably modern [[browser|Browsers]] without needing any ServerSide logic. It allows anyone to create personal SelfContained hypertext documents that can be published to a WebServer, sent by email, stored in a DropBox or kept on a USB thumb drive to make a WikiOnAStick. Because it doesn't need to be installed and configured it makes a great GuerillaWiki. This is revision <<version>> of TiddlyWiki, and is published under an OpenSourceLicense.
Unlike most wikis, TiddlyWiki doesn't directly support group collaboration; it is a wiki in the sense of elevating linking be a part of the punctuation of writing. You can easily publish a TiddlyWiki you have created by placing the single file on a web server (for instance the homepage hosting provided by many ISPs). If you need full group collaboration features, there are several HostedOptions to choose from. Unlike most wikis, TiddlyWiki doesn't directly support group collaboration; it is a wiki in the sense of elevating linking be a part of the punctuation of writing. You can easily publish a TiddlyWiki you have created by placing the single file on a web server (for instance the homepage hosting provided by many ISPs). If you need full group collaboration features, there are several HostedOptions to choose from.
<<list all>>