Added support for transclusion macro

This commit is contained in:
Jeremy Ruston 2011-12-09 08:59:49 +00:00
parent ff73987457
commit a5a0567fe9
8 changed files with 60 additions and 29 deletions

View File

@ -56,10 +56,10 @@ var ArgParser = function(argString,options) {
this.byPos.push({n:"", v:n});
} else {
v = parseToken(match,8);
if(v === null && options.defaultName) {
if(v == null && options.defaultName) {
v = n;
n = options.defaultName;
} else if(v === null && options.defaultValue) {
} else if(v == null && options.defaultValue) {
v = options.defaultValue;
}
this.byPos.push({n:n, v:v});

View File

@ -9,6 +9,25 @@ This file is a bit of a dumping ground; the expectation is that most of these fu
var utils = exports;
utils.deepCopy = function(v) {
var r,t;
if(v instanceof Array) {
r = [];
for(t=0; t<v.length; t++) {
r.push(utils.deepCopy(v[t]));
}
} else if (v === Object(v)) {
r = {};
for(t in v) {
r[t] = utils.deepCopy(v[t]);
}
} else {
r = v;
}
return r;
};
// Pad a string to a certain length with zeros
utils.zeroPad = function(n,d)
{

View File

@ -12,22 +12,22 @@ var ArgParser = require("./ArgParser.js").ArgParser,
var wikiTextMacros = exports;
wikiTextMacros.executeMacros = function(tree,store) {
wikiTextMacros.executeMacros = function(tree,store,tiddler) {
for(var t=0; t<tree.length; t++) {
if(tree[t].type === "macro") {
wikiTextMacros.executeMacro(tree[t],store);
wikiTextMacros.executeMacro(tree[t],store,tiddler);
}
if(tree[t].children) {
wikiTextMacros.executeMacros(tree[t].children,store);
wikiTextMacros.executeMacros(tree[t].children,store,tiddler);
}
}
};
wikiTextMacros.executeMacro = function(macroNode,store) {
wikiTextMacros.executeMacro = function(macroNode,store,tiddler) {
var macroInfo = wikiTextMacros.macros[macroNode.name];
macroNode.output = [];
if(macroInfo) {
macroInfo.handler(macroNode,store);
macroInfo.handler(macroNode,store,tiddler);
} else {
macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name});
}
@ -35,47 +35,59 @@ wikiTextMacros.executeMacro = function(macroNode,store) {
wikiTextMacros.macros = {
allTags: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
br: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
list: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
slider: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
tabs: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
tag: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
tagging: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
tags: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
tiddler: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
var args = new ArgParser(macroNode.params,{defaultName:"name"}),
title = args.getValueByName("name",null),
withTokens = args.getValuesByName("with",[]);
if(withTokens.length > 0) {
} else {
// There are no substitution tokens, so just copy the parse tree of the tiddler
var copy = utils.deepCopy(store.getTiddler(title).getParseTree().tree);
for(var t=0; t<copy.length; t++) {
macroNode.output.push(copy[t]);
}
}
}
},
timeline: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
},
today: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
var now = new Date(),
args = new ArgParser(macroNode.params,{noNames:true}),
value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString();
@ -83,13 +95,12 @@ wikiTextMacros.macros = {
}
},
version: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
macroNode.output.push({type: "text", value: "0.0.0"});
}
},
view: {
handler: function(macroNode,store) {
handler: function(macroNode,store,tiddler) {
}
}
};

View File

@ -101,7 +101,7 @@ WikiTextParser.prototype.renderAsHtml = function(store,title) {
}
}
};
wikiTextMacros.executeMacros(this.tree,this.store);
wikiTextMacros.executeMacros(this.tree,store);
renderSubTree(this.tree);
return output.join("");
};
@ -131,6 +131,7 @@ WikiTextParser.prototype.renderAsText = function(store,title) {
}
}
};
wikiTextMacros.executeMacros(this.tree,store);
renderSubTree(this.tree);
return output.join("");
};

View File

@ -1 +1 @@
An explicit link <a href="Fourth Tiddler">Fourth Tiddler</a> and <a href="Fourth Tiddler">a pretty link</a>
An explicit link <a href="Fourth Tiddler">Fourth Tiddler</a> and <a href="Fourth Tiddler">a pretty link</a> and a transclusion An image <img src="Something.jpg" /> and a couple of &#xc7; &#199; &quot;HTML Entity&quot;

View File

@ -1,3 +1,3 @@
title: ThirdTiddler
An explicit link [[Fourth Tiddler]] and [[a pretty link|Fourth Tiddler]]
An explicit link [[Fourth Tiddler]] and [[a pretty link|Fourth Tiddler]] and a transclusion <<tiddler [[Fourth Tiddler]]>>

View File

@ -1 +1 @@
An explicit link Fourth Tiddler and a pretty link
An explicit link Fourth Tiddler and a pretty link and a transclusion An image and a couple of Ç Ç "HTML Entity"

View File

@ -38,14 +38,14 @@ for(f=0; f<files.length; f++) {
for(t=0; t<titles.length; t++) {
var tree = store.getTiddler(titles[t]).getParseTree(),
htmlRender = tree.render("text/html"),
htmlRender = tree.render("text/html",store,titles[t]),
htmlTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".html"),"utf8"),
plainRender = tree.render("text/plain"),
plainRender = tree.render("text/plain",store,titles[t]),
plainTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".txt"),"utf8");
if(htmlTarget !== htmlRender) {
console.error("Tiddler %s html error\nTarget: %s\nFound: %s",titles[t],htmlTarget,htmlRender);
console.error("Tiddler %s html error\nTarget: %s\nFound: %s\n",titles[t],htmlTarget,htmlRender);
}
if(plainTarget !== plainRender) {
console.error("Tiddler %s plain text error\nTarget: %s\nFound: %s",titles[t],plainTarget,plainRender);
console.error("Tiddler %s plain text error\nTarget: %s\nFound: %s\n",titles[t],plainTarget,plainRender);
}
}