mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-13 10:50:27 +00:00
Add support for positional parameters
This commit is contained in:
parent
31c3abb7ab
commit
9713da5071
@ -48,14 +48,14 @@ exports.parse = function() {
|
||||
tiddlerNode = {
|
||||
type: "tiddler",
|
||||
attributes: {
|
||||
tiddler: {type: "string", value: targetTitle}
|
||||
tiddler: {name: "tiddler", type: "string", value: targetTitle}
|
||||
},
|
||||
isBlock: true,
|
||||
children: [transcludeNode]
|
||||
};
|
||||
}
|
||||
if(template) {
|
||||
transcludeNode.attributes["$tiddler"] = {type: "string", value: template};
|
||||
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: template};
|
||||
if(textRef) {
|
||||
return [tiddlerNode];
|
||||
} else {
|
||||
@ -63,12 +63,12 @@ exports.parse = function() {
|
||||
}
|
||||
} else {
|
||||
if(textRef) {
|
||||
transcludeNode.attributes["$tiddler"] = {type: "string", value: targetTitle};
|
||||
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: targetTitle};
|
||||
if(targetField) {
|
||||
transcludeNode.attributes["$field"] = {type: "string", value: targetField};
|
||||
transcludeNode.attributes["$field"] = {name: "$field", type: "string", value: targetField};
|
||||
}
|
||||
if(targetIndex) {
|
||||
transcludeNode.attributes["$index"] = {type: "string", value: targetIndex};
|
||||
transcludeNode.attributes["$index"] = {name: "$index", type: "string", value: targetIndex};
|
||||
}
|
||||
return [tiddlerNode];
|
||||
} else {
|
||||
|
@ -47,13 +47,13 @@ exports.parse = function() {
|
||||
tiddlerNode = {
|
||||
type: "tiddler",
|
||||
attributes: {
|
||||
tiddler: {type: "string", value: targetTitle}
|
||||
tiddler: {name: "tiddler", type: "string", value: targetTitle}
|
||||
},
|
||||
children: [transcludeNode]
|
||||
};
|
||||
}
|
||||
if(template) {
|
||||
transcludeNode.attributes["$tiddler"] = {type: "string", value: template};
|
||||
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: template};
|
||||
if(textRef) {
|
||||
return [tiddlerNode];
|
||||
} else {
|
||||
@ -61,12 +61,12 @@ exports.parse = function() {
|
||||
}
|
||||
} else {
|
||||
if(textRef) {
|
||||
transcludeNode.attributes["$tiddler"] = {type: "string", value: targetTitle};
|
||||
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: targetTitle};
|
||||
if(targetField) {
|
||||
transcludeNode.attributes["$field"] = {type: "string", value: targetField};
|
||||
transcludeNode.attributes["$field"] = {name: "$field", type: "string", value: targetField};
|
||||
}
|
||||
if(targetIndex) {
|
||||
transcludeNode.attributes["$index"] = {type: "string", value: targetIndex};
|
||||
transcludeNode.attributes["$index"] = {name: "$index", type: "string", value: targetIndex};
|
||||
}
|
||||
return [tiddlerNode];
|
||||
} else {
|
||||
|
@ -49,8 +49,9 @@ ParametersWidget.prototype.execute = function() {
|
||||
}
|
||||
// Process each parameter
|
||||
if(transclusionWidget) {
|
||||
$tw.utils.each(this.attributes,function(value,name) {
|
||||
self.setVariable(name,transclusionWidget.getTransclusionParameter(name,value));
|
||||
$tw.utils.each($tw.utils.getOrderedAttributesFromParseTreeNode(self.parseTreeNode),function(attr,index) {
|
||||
var name = attr.name;
|
||||
self.setVariable(name,transclusionWidget.getTransclusionParameter(name,transclusionWidget.getTransclusionParameterByPosition(index,self.getAttribute(name))));
|
||||
});
|
||||
}
|
||||
// Construct the child widgets
|
||||
|
@ -47,7 +47,25 @@ UberTranscludeWidget.prototype.execute = function() {
|
||||
this.transcludeIndex = this.getAttribute("$index");
|
||||
this.transcludeMode = this.getAttribute("$mode");
|
||||
this.recursionMarker = this.getAttribute("$recursionMarker","yes");
|
||||
// Find the value widgets in our child parse tree
|
||||
// Collect the string parameters
|
||||
this.stringParametersByName = Object.create(null);
|
||||
this.stringParametersByPosition = [];
|
||||
var stringParameterIndex = 0;
|
||||
$tw.utils.each($tw.utils.getOrderedAttributesFromParseTreeNode(this.parseTreeNode),function(attr) {
|
||||
var name = attr.name, value = self.getAttribute(name);
|
||||
if(name.charAt(0) === "$") {
|
||||
if(name.charAt(1) === "$") {
|
||||
// Attributes starting $$ represent parameters starting with a single $
|
||||
name = name.slice(1);
|
||||
} else {
|
||||
// Attributes starting with a single $ are reserved for the widget
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.stringParametersByName[name] = value;
|
||||
self.stringParametersByPosition[stringParameterIndex++] = value;
|
||||
});
|
||||
// Collect the value widgets in our child parse tree
|
||||
this.slotValueParseTrees = Object.create(null);
|
||||
var noValueWidgetsFound = true,
|
||||
searchParseTreeNodes = function(nodes) {
|
||||
@ -120,10 +138,23 @@ UberTranscludeWidget.prototype.execute = function() {
|
||||
Fetch the value of a parameter
|
||||
*/
|
||||
UberTranscludeWidget.prototype.getTransclusionParameter = function(name,defaultValue) {
|
||||
if(name.charAt(0) === "$") {
|
||||
name = "$" + name;
|
||||
if(name in this.stringParametersByName) {
|
||||
return this.stringParametersByName[name];
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
Fetch the value of a parameter identified by its position
|
||||
*/
|
||||
UberTranscludeWidget.prototype.getTransclusionParameterByPosition = function(index,defaultValue) {
|
||||
if(index in this.stringParametersByPosition) {
|
||||
return this.stringParametersByPosition[index];
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
return this.getAttribute(name,defaultValue);
|
||||
};
|
||||
|
||||
/*
|
||||
|
@ -0,0 +1,26 @@
|
||||
title: Ubertransclude/Parameterised/Positional
|
||||
description: Positional parameterised transclusion
|
||||
type: text/vnd.tiddlywiki-multiple
|
||||
tags: [[$:/tags/wiki-test-spec]]
|
||||
|
||||
title: Output
|
||||
|
||||
\whitespace trim
|
||||
<$ubertransclude $tiddler='TiddlerOne' zero='Ferret'/>
|
||||
<$ubertransclude zero='Ferret' $tiddler='TiddlerOne'/>
|
||||
<$ubertransclude $tiddler='TiddlerOne' 0='Pigeon'/>
|
||||
<$ubertransclude 0='Pigeon' $tiddler='TiddlerOne'/>
|
||||
<$ubertransclude $tiddler='TiddlerOne' zero='Ferret' 0='Pigeon'/>
|
||||
<$ubertransclude zero='Ferret' 0='Pigeon' $tiddler='TiddlerOne'/>
|
||||
<$ubertransclude $tiddler='TiddlerOne'/>
|
||||
_
|
||||
title: TiddlerOne
|
||||
|
||||
\whitespace trim
|
||||
<$parameters zero='Jaguar'>
|
||||
<$text text=<<zero>>/>
|
||||
</$parameters>
|
||||
_
|
||||
title: ExpectedResult
|
||||
|
||||
<p>FerretFerretPigeonPigeonFerretFerretJaguar</p>
|
Loading…
Reference in New Issue
Block a user