1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-29 08:43:14 +00:00

Add support for macro definitions

This commit is contained in:
Jeremy Ruston 2013-10-13 22:39:05 +01:00
parent 79dcc9a557
commit 9278c43a91
2 changed files with 81 additions and 10 deletions

View File

@ -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;
})();

View File

@ -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;
};