1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +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() {
// Get our parameters
this.transcludeTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
this.transcludeSubTiddler = this.getAttribute("subtiddler");
this.transcludeField = this.getAttribute("field");
this.transcludeIndex = this.getAttribute("index");
this.transcludeMode = this.getAttribute("mode");
@ -53,7 +54,10 @@ TranscludeWidget.prototype.execute = function() {
this.transcludeTitle,
this.transcludeField,
this.transcludeIndex,
{parseAsInline: parseAsInline}),
{
parseAsInline: parseAsInline,
subTiddler: this.transcludeSubTiddler
}),
parseTreeNodes = parser ? parser.tree : this.parseTreeNode.children;
// Set context variables for recursion detection
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
*/
@ -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/x-tiddler-dictionary: the tiddler is parsed as sequence of name:value pairs
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) {
var tiddler = this.getTiddler(title),
exports.getTiddlerData = function(titleOrTiddler,defaultData) {
var tiddler = titleOrTiddler,
data;
if(!(tiddler instanceof $tw.Tiddler)) {
tiddler = this.getTiddler(tiddler)
}
if(tiddler && tiddler.fields.text) {
switch(tiddler.fields.type) {
case "application/json":
@ -601,8 +618,8 @@ exports.getTiddlerData = function(title,defaultData) {
/*
Extract an indexed field from within a data tiddler
*/
exports.extractTiddlerDataItem = function(title,index,defaultText) {
var data = this.getTiddlerData(title,Object.create(null)),
exports.extractTiddlerDataItem = function(titleOrTiddler,index,defaultText) {
var data = this.getTiddlerData(titleOrTiddler,Object.create(null)),
text;
if(data && $tw.utils.hop(data,index)) {
text = data[index];
@ -786,31 +803,34 @@ exports.parseTiddler = function(title,options) {
};
exports.parseTextReference = function(title,field,index,options) {
if(field === "text" || (!field && !index)) {
// Force the tiddler to be lazily loaded
this.getTiddlerText(title);
// Parse it
return this.parseTiddler(title,options);
var tiddler,text;
if(options.subTiddler) {
tiddler = this.getSubTiddler(title,options.subTiddler);
} else {
var text;
if(field) {
if(field === "title") {
text = title;
} else {
var tiddler = this.getTiddler(title);
if(!tiddler || !tiddler.hasField(field)) {
return null;
}
text = tiddler.fields[field];
}
return this.parseText("text/vnd.tiddlywiki",text.toString(),options);
} else if(index) {
text = this.extractTiddlerDataItem(title,index,"");
if(text === undefined) {
tiddler = this.getTiddler(title);
if(field === "text" || (!field && !index)) {
this.getTiddlerText(title); // Force the tiddler to be lazily loaded
return this.parseTiddler(title,options);
}
}
if(field === "text" || (!field && !index)) {
return this.parseText(tiddler.fields.type || "text/vnd.tiddlywiki",tiddler.fields.text,options);
} else if(field) {
if(field === "title") {
text = title;
} else {
if(!tiddler || !tiddler.hasField(field)) {
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
modified: 20140526175900970
modified: 20140717175900970
tags: widget
title: TranscludeWidget
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) |
|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]] |
|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" |
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"/>
# 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"/>
'>>