1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Refactored wiki text rendering

This commit is contained in:
Jeremy Ruston 2011-12-09 18:29:29 +00:00
parent 12cc309f72
commit 32514df9f3
5 changed files with 228 additions and 219 deletions

View File

@ -50,6 +50,7 @@ At this point tiddlers are placed in the store so that they can be referenced by
"use strict";
var Tiddler = require("./Tiddler.js").Tiddler,
WikiTextRenderer = require("./WikiTextRenderer").WikiTextRenderer,
tiddlerInput = require("./TiddlerInput.js"),
tiddlerOutput = require("./TiddlerOutput.js"),
utils = require("./Utils.js"),
@ -282,13 +283,17 @@ Recipe.prototype.cookRss = function()
numRssItems = 20,
s = [],
d = new Date(),
u = this.store.getTiddler("SiteUrl").getParseTree().render("text/plain",me.store,"SiteUrl"),
renderTiddler = function(title,type) {
var r = new WikiTextRenderer(me.store.getTiddler(title).getParseTree(),me.store,title);
return r.render(type);
},
u = renderTiddler("SiteUrl","text/plain"),
encodeTiddlyLink = function(title) {
return title.indexOf(" ") == -1 ? title : "[[" + title + "]]";
},
tiddlerToRssItem = function(tiddler,uri) {
var s = "<title" + ">" + utils.htmlEncode(tiddler.fields.title) + "</title" + ">\n";
s += "<description>" + utils.htmlEncode(tiddler.getParseTree().render("text/html",me.store,tiddler.fields.title)) + "</description>\n";
s += "<description>" + utils.htmlEncode(renderTiddler(tiddler.fields.title,"text/plain")) + "</description>\n";
var i;
if(tiddler.fields.tags) {
for(i=0; i<tiddler.fields.tags.length; i++) {
@ -327,10 +332,10 @@ Recipe.prototype.cookRss = function()
s.push("<" + "?xml version=\"1.0\"?" + ">");
s.push("<rss version=\"2.0\">");
s.push("<channel>");
s.push("<title" + ">" + utils.htmlEncode(this.store.getTiddler("SiteTitle").getParseTree().render("text/plain",me.store,"SiteTitle")) + "</title" + ">");
s.push("<title" + ">" + utils.htmlEncode(renderTiddler("SiteTitle","text/plain")) + "</title" + ">");
if(u)
s.push("<link>" + utils.htmlEncode(u) + "</link>");
s.push("<description>" + utils.htmlEncode(this.store.getTiddler("SiteSubtitle").getParseTree().render("text/plain",me.store,"SiteSubtitle")) + "</description>");
s.push("<description>" + utils.htmlEncode(renderTiddler("SiteSubtitle","text/plain")) + "</description>");
//s.push("<language>" + config.locale + "</language>");
s.push("<pubDate>" + d.toUTCString() + "</pubDate>");
s.push("<lastBuildDate>" + d.toUTCString() + "</lastBuildDate>");

View File

@ -1,111 +0,0 @@
/*
Wiki text macro implementation
*/
/*jslint node: true */
"use strict";
var ArgParser = require("./ArgParser.js").ArgParser,
WikiTextParserModule = require("./WikiTextParser.js"),
utils = require("./Utils.js"),
util = require("util");
var wikiTextMacros = exports;
wikiTextMacros.versionTiddlyWiki = "2.6.5";
wikiTextMacros.executeMacros = function(tree,store,title) {
for(var t=0; t<tree.length; t++) {
if(tree[t].type === "macro") {
wikiTextMacros.executeMacro(tree[t],store,title);
}
if(tree[t].children) {
wikiTextMacros.executeMacros(tree[t].children,store,title);
}
}
};
wikiTextMacros.executeMacro = function(macroNode,store,title) {
var macroInfo = wikiTextMacros.macros[macroNode.name];
macroNode.output = [];
if(macroInfo) {
macroInfo.handler(macroNode,store,title);
} else {
macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name});
}
};
wikiTextMacros.macros = {
allTags: {
handler: function(macroNode,store,title) {
}
},
br: {
handler: function(macroNode,store,title) {
}
},
list: {
handler: function(macroNode,store,title) {
}
},
slider: {
handler: function(macroNode,store,title) {
}
},
tabs: {
handler: function(macroNode,store,title) {
}
},
tag: {
handler: function(macroNode,store,title) {
}
},
tagging: {
handler: function(macroNode,store,title) {
}
},
tags: {
handler: function(macroNode,store,title) {
}
},
tiddler: {
handler: function(macroNode,store,title) {
var args = new ArgParser(macroNode.params,{defaultName:"name"}),
targetTitle = args.getValueByName("name",null),
withTokens = args.getValuesByName("with",[]),
text = store.getTiddlerText(targetTitle,""),
t;
for(t=0; t<withTokens.length; t++) {
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
text = text.replace(placeholderRegExp,withTokens[t]);
}
var parseTree = new WikiTextParserModule.WikiTextParser(text);
for(t=0; t<parseTree.tree.length; t++) {
macroNode.output.push(parseTree.tree[t]);
}
// Execute any macros in the copy
wikiTextMacros.executeMacros(macroNode.output,store,title);
}
},
timeline: {
handler: function(macroNode,store,title) {
}
},
today: {
handler: function(macroNode,store,title) {
var now = new Date(),
args = new ArgParser(macroNode.params,{noNames:true,cascadeDefaults:true}),
value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString();
macroNode.output.push({type: "text", value: value});
}
},
version: {
handler: function(macroNode,store,title) {
macroNode.output.push({type: "text", value: wikiTextMacros.versionTiddlyWiki});
}
},
view: {
handler: function(macroNode,store,title) {
}
}
};

View File

@ -26,8 +26,7 @@ Text nodes are:
/*jslint node: true */
"use strict";
var wikiTextMacros = require("./WikiTextMacros.js"),
wikiTextRules = require("./WikiTextRules.js").wikiTextRules,
var wikiTextRules = require("./WikiTextRules.js").wikiTextRules,
utils = require("./Utils.js"),
util = require("util");
@ -40,103 +39,6 @@ var WikiTextParser = function(text) {
this.subWikify(this.tree);
};
// Render the tiddler as HTML from its parse tree
// type - MIME type of required output
// store - store object providing context for inter-tiddler operations
// title - render the tree as if it came from a tiddler of this title
WikiTextParser.prototype.render = function(type,store,title) {
if(type === "text/html") {
return this.renderAsHtml(store,title);
} else if (type === "text/plain") {
return this.renderAsText(store,title);
} else {
return null;
}
};
WikiTextParser.prototype.renderAsHtml = function(store,title) {
var output = [],
renderSubTree;
var renderElement = function(element, selfClosing) {
var tagBits = [element.type];
if(element.attributes) {
for(var a in element.attributes) {
var r = element.attributes[a];
if(a === "style") {
var s = [];
for(var t in r) {
s.push(t + ":" + r[t] + ";");
}
r = s.join("");
}
tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\"");
}
}
output.push("<" + tagBits.join(" ") + (selfClosing ? " /" : "") + ">");
if(!selfClosing) {
if(element.children) {
renderSubTree(element.children);
}
output.push("</" + element.type + ">");
}
};
renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) {
switch(tree[t].type) {
case "text":
output.push(utils.htmlEncode(tree[t].value));
break;
case "entity":
output.push(tree[t].value);
break;
case "br":
case "img":
renderElement(tree[t],true); // Self closing elements
break;
case "macro":
renderSubTree(tree[t].output);
break;
default:
renderElement(tree[t]);
break;
}
}
};
wikiTextMacros.executeMacros(this.tree,store,title);
renderSubTree(this.tree);
return output.join("");
};
WikiTextParser.prototype.renderAsText = function(store,title) {
var output = [];
var renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) {
switch(tree[t].type) {
case "text":
output.push(tree[t].value);
break;
case "entity":
var c = utils.entityDecode(tree[t].value);
if(c) {
output.push(c);
} else {
output.push(tree[t].value);
}
break;
case "macro":
renderSubTree(tree[t].output);
break;
}
if(tree[t].children) {
renderSubTree(tree[t].children);
}
}
};
wikiTextMacros.executeMacros(this.tree,store,title);
renderSubTree(this.tree);
return output.join("");
};
WikiTextParser.prototype.outputText = function(place,startPos,endPos) {
if(startPos < endPos) {
place.push({type: "text", value: this.source.substring(startPos,endPos)});

211
js/WikiTextRenderer.js Normal file
View File

@ -0,0 +1,211 @@
/*
Wiki text macro implementation
*/
/*jslint node: true */
"use strict";
var ArgParser = require("./ArgParser.js").ArgParser,
WikiTextParser = require("./WikiTextParser.js").WikiTextParser,
utils = require("./Utils.js"),
util = require("util");
var WikiTextRenderer = function(parser,store,title) {
this.parser = parser;
this.store = store;
this.title = title;
};
WikiTextRenderer.prototype.render = function(type) {
if(type === "text/html") {
return this.renderAsHtml();
} else if (type === "text/plain") {
return this.renderAsText();
} else {
return null;
}
};
WikiTextRenderer.prototype.renderAsHtml = function() {
var output = [],
renderSubTree;
var renderElement = function(element, selfClosing) {
var tagBits = [element.type];
if(element.attributes) {
for(var a in element.attributes) {
var r = element.attributes[a];
if(a === "style") {
var s = [];
for(var t in r) {
s.push(t + ":" + r[t] + ";");
}
r = s.join("");
}
tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\"");
}
}
output.push("<" + tagBits.join(" ") + (selfClosing ? " /" : "") + ">");
if(!selfClosing) {
if(element.children) {
renderSubTree(element.children);
}
output.push("</" + element.type + ">");
}
};
renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) {
switch(tree[t].type) {
case "text":
output.push(utils.htmlEncode(tree[t].value));
break;
case "entity":
output.push(tree[t].value);
break;
case "br":
case "img":
renderElement(tree[t],true); // Self closing elements
break;
case "macro":
renderSubTree(tree[t].output);
break;
default:
renderElement(tree[t]);
break;
}
}
};
this.executeMacros(this.parser.tree);
renderSubTree(this.parser.tree);
return output.join("");
};
WikiTextRenderer.prototype.renderAsText = function() {
var output = [];
var renderSubTree = function(tree) {
for(var t=0; t<tree.length; t++) {
switch(tree[t].type) {
case "text":
output.push(tree[t].value);
break;
case "entity":
var c = utils.entityDecode(tree[t].value);
if(c) {
output.push(c);
} else {
output.push(tree[t].value);
}
break;
case "macro":
renderSubTree(tree[t].output);
break;
}
if(tree[t].children) {
renderSubTree(tree[t].children);
}
}
};
this.executeMacros(this.parser.tree);
renderSubTree(this.parser.tree);
return output.join("");
};
WikiTextRenderer.prototype.executeMacros = function(tree) {
for(var t=0; t<tree.length; t++) {
if(tree[t].type === "macro") {
this.executeMacro(tree[t]);
}
if(tree[t].children) {
this.executeMacros(tree[t].children);
}
}
};
WikiTextRenderer.prototype.executeMacro = function(macroNode) {
var macroInfo = WikiTextRenderer.macros[macroNode.name];
macroNode.output = [];
if(macroInfo) {
macroInfo.handler.call(this,macroNode);
} else {
macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name});
}
};
WikiTextRenderer.versionTiddlyWiki = "2.6.5";
WikiTextRenderer.macros = {
allTags: {
handler: function(macroNode) {
}
},
br: {
handler: function(macroNode) {
}
},
list: {
handler: function(macroNode) {
}
},
slider: {
handler: function(macroNode) {
}
},
tabs: {
handler: function(macroNode) {
}
},
tag: {
handler: function(macroNode) {
}
},
tagging: {
handler: function(macroNode) {
}
},
tags: {
handler: function(macroNode) {
}
},
tiddler: {
handler: function(macroNode) {
var args = new ArgParser(macroNode.params,{defaultName:"name"}),
targetTitle = args.getValueByName("name",null),
withTokens = args.getValuesByName("with",[]),
text = this.store.getTiddlerText(targetTitle,""),
t;
for(t=0; t<withTokens.length; t++) {
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
text = text.replace(placeholderRegExp,withTokens[t]);
}
var parseTree = new WikiTextParser(text);
for(t=0; t<parseTree.tree.length; t++) {
macroNode.output.push(parseTree.tree[t]);
}
// Execute any macros in the copy
this.executeMacros(macroNode.output);
}
},
timeline: {
handler: function(macroNode) {
}
},
today: {
handler: function(macroNode) {
var now = new Date(),
args = new ArgParser(macroNode.params,{noNames:true,cascadeDefaults:true}),
value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString();
macroNode.output.push({type: "text", value: value});
}
},
version: {
handler: function(macroNode) {
macroNode.output.push({type: "text", value: WikiTextRenderer.versionTiddlyWiki});
}
},
view: {
handler: function(macroNode) {
}
}
};
exports.WikiTextRenderer = WikiTextRenderer;

View File

@ -14,6 +14,7 @@ verifying that the output matches `<tiddlername>.html` and `<tiddlername>.txt`.
var Tiddler = require("./js/Tiddler.js").Tiddler,
WikiStore = require("./js/WikiStore.js").WikiStore,
WikiTextRenderer = require("./js/WikiTextRenderer.js").WikiTextRenderer,
tiddlerInput = require("./js/TiddlerInput"),
utils = require("./js/Utils.js"),
util = require("util"),
@ -39,11 +40,12 @@ 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",store,titles[t]),
htmlTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".html"),"utf8"),
plainRender = tree.render("text/plain",store,titles[t]),
plainTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".txt"),"utf8");
var htmlTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".html"),"utf8"),
plainTarget = fs.readFileSync(path.resolve(testdirectory,titles[t] + ".txt"),"utf8"),
parser = store.getTiddler(titles[t]).getParseTree(),
renderer = new WikiTextRenderer(parser,store,titles[t]),
htmlRender = renderer.render("text/html"),
plainRender = renderer.render("text/plain");
if(htmlTarget !== htmlRender) {
console.error("Tiddler %s html error\nTarget: %s\nFound: %s\n",titles[t],htmlTarget,htmlRender);
}