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

Add a wiki.makeWidget() method

Encapsulates some logic that has been duplicated elsewhere.
This commit is contained in:
Jeremy Ruston 2013-10-29 14:48:24 +00:00
parent c4d5b803ef
commit 4eebe7388d

View File

@ -707,20 +707,59 @@ exports.new_parseTextReference = function(title,field,index,options) {
} }
}; };
/*
Make a widget tree for a parse tree
parser: parser object
options: see below
Options include:
document: optional document to use
variables: hashmap of variables to set
parentWidget: optional parent widget for the root node
*/
exports.makeWidget = function(parser,options) {
options = options || {};
var widgetNode = {
type: "widget",
children: []
},
currWidgetNode = widgetNode;
// Create setvariable widgets for each variable
$tw.utils.each(options.variables,function(value,name) {
var setVariableWidget = {
type: "setvariable",
attributes: {
name: {type: "string", value: name},
value: {type: "string", value: value}
},
children: []
};
currWidgetNode.children = [setVariableWidget];
currWidgetNode = setVariableWidget;
});
// Add in the supplied parse tree nodes
currWidgetNode.children = parser ? parser.tree : [];
// Create the widget
return new widget.widget(widgetNode,{
wiki: this,
document: options.document || $tw.document,
parentWidget: options.parentWidget
});
};
/* /*
Parse text in a specified format and render it into another format Parse text in a specified format and render it into another format
outputType: content type for the output outputType: content type for the output
textType: content type of the input text textType: content type of the input text
text: input text text: input text
options: see below
Options include:
variables: hashmap of variables to set
parentWidget: optional parent widget for the root node
*/ */
exports.new_renderText = function(outputType,textType,text,parentWidget) { exports.new_renderText = function(outputType,textType,text,options) {
options = options || {};
var parser = $tw.wiki.new_parseText(textType,text), var parser = $tw.wiki.new_parseText(textType,text),
parseTreeNode = parser ? {type: "widget", children: parser.tree} : undefined, widgetNode = this.makeWidget(parser,options);
widgetNode = new widget.widget(parseTreeNode,{
wiki: this,
parentWidget: parentWidget,
document: $tw.document
});
var container = $tw.document.createElement("div"); var container = $tw.document.createElement("div");
widgetNode.render(container,null); widgetNode.render(container,null);
return outputType === "text/html" ? container.innerHTML : container.textContent; return outputType === "text/html" ? container.innerHTML : container.textContent;
@ -730,15 +769,15 @@ exports.new_renderText = function(outputType,textType,text,parentWidget) {
Parse text from a tiddler and render it into another format Parse text from a tiddler and render it into another format
outputType: content type for the output outputType: content type for the output
title: title of the tiddler to be rendered title: title of the tiddler to be rendered
options: see below
Options include:
variables: hashmap of variables to set
parentWidget: optional parent widget for the root node
*/ */
exports.new_renderTiddler = function(outputType,title,parentWidget) { exports.new_renderTiddler = function(outputType,title,options) {
options = options || {};
var parser = this.new_parseTiddler(title), var parser = this.new_parseTiddler(title),
parseTreeNode = parser ? {type: "widget", children: parser.tree} : undefined, widgetNode = this.makeWidget(parser,options);
widgetNode = new widget.widget(parseTreeNode,{
wiki: this,
parentWidget: parentWidget,
document: $tw.document
});
var container = $tw.document.createElement("div"); var container = $tw.document.createElement("div");
widgetNode.render(container,null); widgetNode.render(container,null);
return outputType === "text/html" ? container.innerHTML : container.textContent; return outputType === "text/html" ? container.innerHTML : container.textContent;