diff --git a/core/modules/new_widgets/macrodef.js b/core/modules/new_widgets/macrodef.js new file mode 100644 index 000000000..a386fe4ab --- /dev/null +++ b/core/modules/new_widgets/macrodef.js @@ -0,0 +1,55 @@ +/*\ +title: $:/core/modules/new_widgets/macrodef.js +type: application/javascript +module-type: new_widget + +Macro definition widget + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var Widget = require("$:/core/modules/new_widgets/widget.js").widget; + +var MacroDefWidget = function(parseTreeNode,options) { + this.initialise(parseTreeNode,options); +}; + +/* +Inherit from the base widget class +*/ +MacroDefWidget.prototype = new Widget(); + +/* +Render this widget into the DOM +*/ +MacroDefWidget.prototype.render = function(parent,nextSibling) { + this.parentDomNode = parent; + this.computeAttributes(); + this.execute(); + this.renderChildren(parent,nextSibling); +}; + +/* +Compute the internal state of the widget +*/ +MacroDefWidget.prototype.execute = function() { + // Set macro definition + this.setVariable(this.parseTreeNode.name,this.parseTreeNode.text,this.parseTreeNode.params); + // Construct the child widgets + this.makeChildWidgets(); +}; + +/* +Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering +*/ +MacroDefWidget.prototype.refresh = function(changedTiddlers) { + return this.refreshChildren(changedTiddlers); +}; + +exports.macrodef = MacroDefWidget; + +})(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 50583206a..479cfbf12 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -629,19 +629,35 @@ exports.parseTiddler = function(title,options) { // We need to tweak parse trees generated by the existing parser because of the change from {type:"element",tag:"$tiddler",...} to {type:"tiddler",...} var tweakParseTreeNode = function(node) { - if(node.type === "element" && node.tag.charAt(0) === "$") { - node.type = node.tag.substr(1); - } - tweakParseTreeNodes(node.children); - }, - tweakParseTreeNodes = function(nodeList) { - $tw.utils.each(nodeList,tweakParseTreeNode); - }; + if(node.type === "element" && node.tag.charAt(0) === "$") { + node.type = node.tag.substr(1); + } + tweakParseTreeNodes(node.children); +}; + +var tweakParseTreeNodes = function(nodeList) { + $tw.utils.each(nodeList,tweakParseTreeNode); +}; + +var tweakMacroDefinition = function(nodeList) { + if(nodeList && nodeList[0] && nodeList[0].type === "macrodef") { + nodeList[0].children = nodeList.slice(1); + nodeList.splice(1,nodeList.length-1); + tweakMacroDefinition(nodeList.children); + } +}; + +var tweakParser = function(parser) { + // Move any macro definitions to contain the body tree + tweakMacroDefinition(parser.tree); + // Tweak widgets + tweakParseTreeNodes(parser.tree); +}; exports.new_parseText = function(type,text,options) { var parser = this.parseText(type,text,options); if(parser) { - tweakParseTreeNodes(parser.tree) + tweakParser(parser) }; return parser; }; @@ -649,7 +665,7 @@ exports.new_parseText = function(type,text,options) { exports.new_parseTiddler = function(title,options) { var parser = this.parseTiddler(title,options); if(parser) { - tweakParseTreeNodes(parser.tree) + tweakParser(parser) }; return parser; };