mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-09-16 14:51:24 +00:00
Introduce refactored wiki parser and renderer
This is a half-way through a big refactoring of the parsing and rendering infrastructure. The main change is to separate the parse and render trees, which makes the code a lot cleaner. The new parser isn't yet functional enough to replace the existing parser so for the moment you have to manually invoke it with `$tw.testNewParser()` in your browser console. I really ought to use branches for this kind of thing...
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*\
|
||||
title: $:/core/modules/rendertree/wikirendertree.js
|
||||
type: application/javascript
|
||||
module-type: global
|
||||
|
||||
Wiki text render tree
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Create a render tree object for a parse tree
|
||||
*/
|
||||
var WikiRenderTree = function(parser,options) {
|
||||
this.parser = parser;
|
||||
this.wiki = options.wiki;
|
||||
};
|
||||
|
||||
/*
|
||||
Generate the full render tree for this parse tree
|
||||
renderContext: see below
|
||||
An renderContext consists of these fields:
|
||||
tiddlerTitle: title of the tiddler providing the context
|
||||
parentContext: reference back to previous context in the stack
|
||||
*/
|
||||
WikiRenderTree.prototype.execute = function(renderContext) {
|
||||
this.rendererTree = this.createRenderers(renderContext,this.parser.tree);
|
||||
};
|
||||
|
||||
/*
|
||||
Create an array of renderers for an array of parse tree nodes
|
||||
*/
|
||||
WikiRenderTree.prototype.createRenderers = function(renderContext,parseTreeNodes) {
|
||||
var rendererNodes = [];
|
||||
for(var t=0; t<parseTreeNodes.length; t++) {
|
||||
rendererNodes.push(this.createRenderer(renderContext,parseTreeNodes[t]));
|
||||
}
|
||||
return rendererNodes;
|
||||
};
|
||||
|
||||
/*
|
||||
Create a renderer node for a parse tree node
|
||||
*/
|
||||
WikiRenderTree.prototype.createRenderer = function(renderContext,parseTreeNode) {
|
||||
var RenderNodeClass = this.parser.vocabulary.rendererClasses[parseTreeNode.type];
|
||||
return new RenderNodeClass(this,renderContext,parseTreeNode);
|
||||
};
|
||||
|
||||
/*
|
||||
Render as a string
|
||||
*/
|
||||
WikiRenderTree.prototype.render = function(type) {
|
||||
var output = [];
|
||||
$tw.utils.each(this.rendererTree,function(node) {
|
||||
if(node.render) {
|
||||
output.push(node.render(type));
|
||||
}
|
||||
});
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
/*
|
||||
Render to the DOM
|
||||
*/
|
||||
WikiRenderTree.prototype.renderInDom = function(container) {
|
||||
this.container = container;
|
||||
$tw.utils.each(this.rendererTree,function(node) {
|
||||
if(node.renderInDom) {
|
||||
container.appendChild(node.renderInDom());
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
Update the DOM rendering in the light of a set of changes
|
||||
*/
|
||||
WikiRenderTree.prototype.refreshInDom = function(changes) {
|
||||
$tw.utils.each(this.rendererTree,function(node) {
|
||||
if(node.refreshInDom) {
|
||||
node.refreshInDom(changes);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.WikiRenderTree = WikiRenderTree;
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user