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

Adding caching for tiddler parse trees, render functions, and renditions

This commit is contained in:
Jeremy Ruston 2012-01-06 17:40:22 +00:00
parent e190cadcca
commit 14243acec9
4 changed files with 36 additions and 16 deletions

View File

@ -22,6 +22,9 @@ var utils = require("./Utils.js"),
WikiTextParser = require("./WikiTextParser.js").WikiTextParser;
var Tiddler = function(/* tiddler,fields */) {
this.parseTree = null; // Caches the parse tree for the tiddler
this.renderers = {}; // Caches rendering functions for this tiddler (indexed by MIME type)
this.renditions = {}; // Caches the renditions produced by those functions (indexed by MIME type)
this.fields = {};
for(var c=0; c<arguments.length; c++) {
var arg = arguments[c],

View File

@ -280,7 +280,28 @@ WikiStore.prototype.parseText = function(type,text) {
WikiStore.prototype.parseTiddler = function(title) {
var tiddler = this.getTiddler(title);
if(tiddler) {
return this.parseText(tiddler.fields.type,tiddler.fields.text);
// Check the cache
if(!tiddler.parseTree) {
tiddler.parseTree = this.parseText(tiddler.fields.type,tiddler.fields.text);
}
return tiddler.parseTree;
} else {
return null;
}
};
/*
Compiles a JavaScript function that renders a tiddler in a particular MIME type
*/
WikiStore.prototype.compileTiddler = function(title,type) {
/*jslint evil: true */
var tiddler = this.getTiddler(title);
if(tiddler) {
if(!tiddler.renderers[type]) {
var tree = this.parseTiddler(title);
tiddler.renderers[type] = eval(tree.compile(type));
}
return tiddler.renderers[type];
} else {
return null;
}
@ -288,16 +309,20 @@ WikiStore.prototype.parseTiddler = function(title) {
/*
Render a tiddler to a particular MIME type. Optionally render it with a different tiddler
as the context. This option is used to render a tiddler through a template as
as the context. This option is used to render a tiddler through a template eg
store.renderTiddler("text/html",templateTitle,tiddlerTitle)
*/
WikiStore.prototype.renderTiddler = function(type,title,asTitle) {
var parser = this.parseTiddler(title),
asTitleExists = asTitle ? this.tiddlerExists(asTitle) : true;
if(parser && asTitleExists) {
return parser.render(type,parser.children,this,asTitle ? asTitle : title);
var tiddler = this.getTiddler(title),
fn = this.compileTiddler(title,type);
if(asTitle) {
var asTiddler = this.getTiddler(asTitle);
return fn(asTiddler,this,utils);
} else {
return null;
if(!tiddler.renditions[type]) {
tiddler.renditions[type] = fn(tiddler,this,utils);
}
return tiddler.renditions[type];
}
};

View File

@ -20,14 +20,6 @@ var WikiTextParseTree = function(tree,store) {
this.store = store;
};
WikiTextParseTree.prototype.render = function(type,treenode,store,title) {
/*jslint evil: true */
var code = this.compile(type,treenode);
var fn = eval(code);
var tiddler = store.getTiddler(title);
return fn(tiddler,store,utils);
};
// Compile the parse tree into a JavaScript function that returns the required
// representation of the tree
WikiTextParseTree.prototype.compile = function(type,treenode) {

View File

@ -73,7 +73,7 @@ a {
}
a.linkExternal::before {
content: "\27a0";
content: "\27a0\00a0";
}
a:hover {