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

print-window-tiddler
Jeremy Ruston 2012-01-06 17:40:22 +00:00
rodzic e190cadcca
commit 14243acec9
4 zmienionych plików z 36 dodań i 16 usunięć

Wyświetl plik

@ -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],

Wyświetl plik

@ -280,24 +280,49 @@ 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;
}
};
/*
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];
}
};

Wyświetl plik

@ -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) {

Wyświetl plik

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