mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-25 09:30:28 +00:00
Add support for macro definitions
This commit is contained in:
parent
79dcc9a557
commit
9278c43a91
55
core/modules/new_widgets/macrodef.js
Normal file
55
core/modules/new_widgets/macrodef.js
Normal 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;
|
||||
|
||||
})();
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user