From 4eebe7388dee877776112b782f543dc383c964e2 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 29 Oct 2013 14:48:24 +0000 Subject: [PATCH] Add a wiki.makeWidget() method Encapsulates some logic that has been duplicated elsewhere. --- core/modules/wiki.js | 67 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 04795756b..b0f9a11a3 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -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 outputType: content type for the output textType: content type of the 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), - parseTreeNode = parser ? {type: "widget", children: parser.tree} : undefined, - widgetNode = new widget.widget(parseTreeNode,{ - wiki: this, - parentWidget: parentWidget, - document: $tw.document - }); + widgetNode = this.makeWidget(parser,options); var container = $tw.document.createElement("div"); widgetNode.render(container,null); 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 outputType: content type for the output 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), - parseTreeNode = parser ? {type: "widget", children: parser.tree} : undefined, - widgetNode = new widget.widget(parseTreeNode,{ - wiki: this, - parentWidget: parentWidget, - document: $tw.document - }); + widgetNode = this.makeWidget(parser,options); var container = $tw.document.createElement("div"); widgetNode.render(container,null); return outputType === "text/html" ? container.innerHTML : container.textContent;