1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-02 20:29:10 +00:00

Add subtiddler support to the transclude widget

Useful to be able to reach into plugins. It will enable us to do things
like extract readmes from themes that are not active.
This commit is contained in:
Jermolene 2014-07-17 18:41:20 +01:00
parent ed0bf7aed1
commit f793816dfa
3 changed files with 64 additions and 28 deletions

View File

@ -39,6 +39,7 @@ Compute the internal state of the widget
TranscludeWidget.prototype.execute = function() { TranscludeWidget.prototype.execute = function() {
// Get our parameters // Get our parameters
this.transcludeTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler")); this.transcludeTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
this.transcludeSubTiddler = this.getAttribute("subtiddler");
this.transcludeField = this.getAttribute("field"); this.transcludeField = this.getAttribute("field");
this.transcludeIndex = this.getAttribute("index"); this.transcludeIndex = this.getAttribute("index");
this.transcludeMode = this.getAttribute("mode"); this.transcludeMode = this.getAttribute("mode");
@ -53,7 +54,10 @@ TranscludeWidget.prototype.execute = function() {
this.transcludeTitle, this.transcludeTitle,
this.transcludeField, this.transcludeField,
this.transcludeIndex, this.transcludeIndex,
{parseAsInline: parseAsInline}), {
parseAsInline: parseAsInline,
subTiddler: this.transcludeSubTiddler
}),
parseTreeNodes = parser ? parser.tree : this.parseTreeNode.children; parseTreeNodes = parser ? parser.tree : this.parseTreeNode.children;
// Set context variables for recursion detection // Set context variables for recursion detection
var recursionMarker = this.makeRecursionMarker(); var recursionMarker = this.makeRecursionMarker();

View File

@ -554,6 +554,17 @@ exports.sortByList = function(array,listTitle) {
} }
}; };
exports.getSubTiddler = function(title,subTiddlerTitle) {
var bundleInfo = this.getPluginInfo(title) || this.getTiddlerData(title);
if(bundleInfo) {
var subTiddler = bundleInfo.tiddlers[subTiddlerTitle];
if(subTiddler) {
return new $tw.Tiddler(subTiddler);
}
}
return null;
};
/* /*
Retrieve a tiddler as a JSON string of the fields Retrieve a tiddler as a JSON string of the fields
*/ */
@ -571,16 +582,22 @@ exports.getTiddlerAsJson = function(title) {
}; };
/* /*
Get a tiddlers content as a JavaScript object. How this is done depends on the type of the tiddler: Get the content of a tiddler as a JavaScript object. How this is done depends on the type of the tiddler:
application/json: the tiddler JSON is parsed into an object application/json: the tiddler JSON is parsed into an object
application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs application/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
Other types currently just return null. Other types currently just return null.
titleOrTiddler: string tiddler title or a tiddler object
defaultData: default data to be returned if the tiddler is missing or doesn't contain data
*/ */
exports.getTiddlerData = function(title,defaultData) { exports.getTiddlerData = function(titleOrTiddler,defaultData) {
var tiddler = this.getTiddler(title), var tiddler = titleOrTiddler,
data; data;
if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = this.getTiddler(tiddler)
}
if(tiddler && tiddler.fields.text) { if(tiddler && tiddler.fields.text) {
switch(tiddler.fields.type) { switch(tiddler.fields.type) {
case "application/json": case "application/json":
@ -601,8 +618,8 @@ exports.getTiddlerData = function(title,defaultData) {
/* /*
Extract an indexed field from within a data tiddler Extract an indexed field from within a data tiddler
*/ */
exports.extractTiddlerDataItem = function(title,index,defaultText) { exports.extractTiddlerDataItem = function(titleOrTiddler,index,defaultText) {
var data = this.getTiddlerData(title,Object.create(null)), var data = this.getTiddlerData(titleOrTiddler,Object.create(null)),
text; text;
if(data && $tw.utils.hop(data,index)) { if(data && $tw.utils.hop(data,index)) {
text = data[index]; text = data[index];
@ -786,31 +803,34 @@ exports.parseTiddler = function(title,options) {
}; };
exports.parseTextReference = function(title,field,index,options) { exports.parseTextReference = function(title,field,index,options) {
if(field === "text" || (!field && !index)) { var tiddler,text;
// Force the tiddler to be lazily loaded if(options.subTiddler) {
this.getTiddlerText(title); tiddler = this.getSubTiddler(title,options.subTiddler);
// Parse it
return this.parseTiddler(title,options);
} else { } else {
var text; tiddler = this.getTiddler(title);
if(field) { if(field === "text" || (!field && !index)) {
if(field === "title") { this.getTiddlerText(title); // Force the tiddler to be lazily loaded
text = title; return this.parseTiddler(title,options);
} else { }
var tiddler = this.getTiddler(title); }
if(!tiddler || !tiddler.hasField(field)) { if(field === "text" || (!field && !index)) {
return null; return this.parseText(tiddler.fields.type || "text/vnd.tiddlywiki",tiddler.fields.text,options);
} } else if(field) {
text = tiddler.fields[field]; if(field === "title") {
} text = title;
return this.parseText("text/vnd.tiddlywiki",text.toString(),options); } else {
} else if(index) { if(!tiddler || !tiddler.hasField(field)) {
text = this.extractTiddlerDataItem(title,index,"");
if(text === undefined) {
return null; return null;
} }
return this.parseText("text/vnd.tiddlywiki",text,options); text = tiddler.fields[field];
} }
return this.parseText("text/vnd.tiddlywiki",text.toString(),options);
} else if(index) {
text = this.extractTiddlerDataItem(tiddler,index,"");
if(text === undefined) {
return null;
}
return this.parseText("text/vnd.tiddlywiki",text,options);
} }
}; };

View File

@ -1,5 +1,5 @@
created: 20130824142500000 created: 20130824142500000
modified: 20140526175900970 modified: 20140717175900970
tags: widget tags: widget
title: TranscludeWidget title: TranscludeWidget
type: text/vnd.tiddlywiki type: text/vnd.tiddlywiki
@ -14,6 +14,7 @@ The TranscludeWidget dynamically imports content from another tiddler.
|tiddler |The title of the tiddler to transclude (defaults to the current tiddler) | |tiddler |The title of the tiddler to transclude (defaults to the current tiddler) |
|field |The field name of the current tiddler (defaults to "text"; if present takes precedence over the index attribute) | |field |The field name of the current tiddler (defaults to "text"; if present takes precedence over the index attribute) |
|index |The index of a property in a [[DataTiddler|DataTiddlers]] | |index |The index of a property in a [[DataTiddler|DataTiddlers]] |
|subtiddler |Optional SubTiddler title when the target tiddler is a [[plugin|Plugins]] (see below) |
|mode |Override the default parsing mode for the transcluded text to "block" or "inline" | |mode |Override the default parsing mode for the transcluded text to "block" or "inline" |
The TranscludeWidget treats any contained content as a fallback if the target of the transclusion is not defined (ie a missing tiddler or a missing field). The TranscludeWidget treats any contained content as a fallback if the target of the transclusion is not defined (ie a missing tiddler or a missing field).
@ -55,3 +56,14 @@ This can be fixed by amending tiddler "A":
#<$transclude tiddler="B" mode="block"/> #<$transclude tiddler="B" mode="block"/>
# Item two # Item two
``` ```
! SubTiddler Access
The transclude widget allows access to the individual tiddlers stored within a [[plugin|Plugins]].
The following example will transclude the core version of the tiddler [[$:/DefaultTiddlers]] even if it has been overridden:
<<wikitext-example-without-html '
<$transclude tiddler="$:/core" subtiddler="$:/DefaultTiddlers"/>
'>>