From bdbbf94326f70db0f8ef196270ab9e92bfde10fb Mon Sep 17 00:00:00 2001 From: Jermolene Date: Mon, 17 Mar 2014 21:44:10 +0000 Subject: [PATCH] Update transclusion wikitext syntax to allow a template without a target tiddler This allows us to transclude a tiddler without changing the current tiddler with `{{||MyTiddler}}`. --- .../wikiparser/rules/transcludeblock.js | 60 ++++++++++++------- .../wikiparser/rules/transcludeinline.js | 56 ++++++++++------- .../tiddlers/concepts/TemplateTiddlers.tid | 4 +- .../wikitext/Transclusion in WikiText.tid | 10 +++- 4 files changed, 82 insertions(+), 48 deletions(-) diff --git a/core/modules/parsers/wikiparser/rules/transcludeblock.js b/core/modules/parsers/wikiparser/rules/transcludeblock.js index b1cfd6b4e..087e95706 100644 --- a/core/modules/parsers/wikiparser/rules/transcludeblock.js +++ b/core/modules/parsers/wikiparser/rules/transcludeblock.js @@ -23,19 +23,17 @@ exports.types = {block: true}; exports.init = function(parser) { this.parser = parser; // Regexp to match - this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|\|([^\|\{\}]+))?\}\}(?:\r?\n|$)/mg; + this.matchRegExp = /\{\{([^\{\}\|]*)(?:\|\|([^\|\{\}]+))?\}\}(?:\r?\n|$)/mg; }; exports.parse = function() { + // Move past the match + this.parser.pos = this.matchRegExp.lastIndex; // Move past the match this.parser.pos = this.matchRegExp.lastIndex; // Get the match details - var textRef = $tw.utils.trim(this.match[1]), - tr = $tw.utils.parseTextReference(textRef), - targetTitle = tr.title, - targetField = tr.field, - targetIndex = tr.index, - template = $tw.utils.trim(this.match[2]); + var template = $tw.utils.trim(this.match[2]), + textRef = $tw.utils.trim(this.match[1]); // Prepare the transclude widget var transcludeNode = { type: "element", @@ -43,27 +41,43 @@ exports.parse = function() { attributes: {}, isBlock: true }; - var tiddlerNode = { - type: "element", - tag: "$tiddler", - attributes: { - tiddler: {type: "string", value: targetTitle} - }, - isBlock: true, - children: [transcludeNode] - }; + // Prepare the tiddler widget + if(textRef) { + var tr = $tw.utils.parseTextReference(textRef), + targetTitle = tr.title, + targetField = tr.field, + targetIndex = tr.index, + tiddlerNode = { + type: "element", + tag: "$tiddler", + attributes: { + tiddler: {type: "string", value: targetTitle} + }, + isBlock: true, + children: [transcludeNode] + }; + } if(template) { transcludeNode.attributes.tiddler = {type: "string", value: template}; - } else { - transcludeNode.attributes.tiddler = {type: "string", value: targetTitle}; - if(targetField) { - transcludeNode.attributes.field = {type: "string", value: targetField}; + if(textRef) { + return [tiddlerNode]; + } else { + return [transcludeNode]; } - if(targetIndex) { - transcludeNode.attributes.index = {type: "string", value: targetIndex}; + } else { + if(textRef) { + transcludeNode.attributes.tiddler = {type: "string", value: targetTitle}; + if(targetField) { + transcludeNode.attributes.field = {type: "string", value: targetField}; + } + if(targetIndex) { + transcludeNode.attributes.index = {type: "string", value: targetIndex}; + } + return [tiddlerNode]; + } else { + return [transcludeNode]; } } - return [tiddlerNode]; }; })(); diff --git a/core/modules/parsers/wikiparser/rules/transcludeinline.js b/core/modules/parsers/wikiparser/rules/transcludeinline.js index 662cee32a..04a7ff5bb 100644 --- a/core/modules/parsers/wikiparser/rules/transcludeinline.js +++ b/core/modules/parsers/wikiparser/rules/transcludeinline.js @@ -23,45 +23,57 @@ exports.types = {inline: true}; exports.init = function(parser) { this.parser = parser; // Regexp to match - this.matchRegExp = /\{\{([^\{\}\|]+)(?:\|\|([^\|\{\}]+))?\}\}/mg; + this.matchRegExp = /\{\{([^\{\}\|]*)(?:\|\|([^\|\{\}]+))?\}\}/mg; }; exports.parse = function() { // Move past the match this.parser.pos = this.matchRegExp.lastIndex; // Get the match details - var textRef = $tw.utils.trim(this.match[1]), - tr = $tw.utils.parseTextReference(textRef), - targetTitle = tr.title, - targetField = tr.field, - targetIndex = tr.index, - template = $tw.utils.trim(this.match[2]); + var template = $tw.utils.trim(this.match[2]), + textRef = $tw.utils.trim(this.match[1]); // Prepare the transclude widget var transcludeNode = { type: "element", tag: "$transclude", attributes: {} }; - var tiddlerNode = { - type: "element", - tag: "$tiddler", - attributes: { - tiddler: {type: "string", value: targetTitle} - }, - children: [transcludeNode] - }; + // Prepare the tiddler widget + if(textRef) { + var tr = $tw.utils.parseTextReference(textRef), + targetTitle = tr.title, + targetField = tr.field, + targetIndex = tr.index, + tiddlerNode = { + type: "element", + tag: "$tiddler", + attributes: { + tiddler: {type: "string", value: targetTitle} + }, + children: [transcludeNode] + }; + } if(template) { transcludeNode.attributes.tiddler = {type: "string", value: template}; - } else { - transcludeNode.attributes.tiddler = {type: "string", value: targetTitle}; - if(targetField) { - transcludeNode.attributes.field = {type: "string", value: targetField}; + if(textRef) { + return [tiddlerNode]; + } else { + return [transcludeNode]; } - if(targetIndex) { - transcludeNode.attributes.index = {type: "string", value: targetIndex}; + } else { + if(textRef) { + transcludeNode.attributes.tiddler = {type: "string", value: targetTitle}; + if(targetField) { + transcludeNode.attributes.field = {type: "string", value: targetField}; + } + if(targetIndex) { + transcludeNode.attributes.index = {type: "string", value: targetIndex}; + } + return [tiddlerNode]; + } else { + return [transcludeNode]; } } - return [tiddlerNode]; }; })(); diff --git a/editions/tw5.com/tiddlers/concepts/TemplateTiddlers.tid b/editions/tw5.com/tiddlers/concepts/TemplateTiddlers.tid index fcc395685..fc82e953f 100644 --- a/editions/tw5.com/tiddlers/concepts/TemplateTiddlers.tid +++ b/editions/tw5.com/tiddlers/concepts/TemplateTiddlers.tid @@ -1,5 +1,5 @@ created: 20140107114355828 -modified: 20140107114636001 +modified: 20140317213147507 tags: concepts title: TemplateTiddlers type: text/vnd.tiddlywiki @@ -37,4 +37,4 @@ The TiddlerWidget is used to change the current tiddler. Consider a tiddler "C" It is still transcluding tiddler "A", but now it is also setting the current tiddler to "A". The result is therefore that the field "myfield" for tiddler "A" is displayed. -The shorthand syntax for transcluding actually generates both a TiddlerWidget and a TranscludeWidget. +The shorthand syntax for [[Transclusion in WikiText]] actually generates both a TiddlerWidget and a TranscludeWidget. diff --git a/editions/tw5.com/tiddlers/wikitext/Transclusion in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Transclusion in WikiText.tid index 6224df9ff..525e5ec94 100644 --- a/editions/tw5.com/tiddlers/wikitext/Transclusion in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Transclusion in WikiText.tid @@ -1,5 +1,5 @@ created: 20131205160146648 -modified: 20140107114340951 +modified: 20140317214256948 tags: wikitext title: Transclusion in WikiText type: text/vnd.tiddlywiki @@ -8,6 +8,14 @@ You can incorporate the content of one tiddler within another using the transclu * `{{MyTiddler}}` transcludes a single tiddler * `{{MyTiddler||TemplateTitle}}` displays the tiddler through a specified [[TemplateTiddler|TemplateTiddlers]] +* `{{||TemplateTitle}}` displays the specified template tiddler without altering the [[current tiddler|WidgetVariable: currentTiddler]] + +You can also use a TextReference instead of a tiddler title: + +* `{{MyTiddler!!field}}` transcludes a specified field of a tiddler +* `{{!!field}}` transcludes a specified field of the current tiddler +* `{{MyTiddler##index}}` transcludes a specified indexed property of a [[DataTiddler|DataTiddlers]] +* `{{##index}}` transcludes a specified indexed property of the current [[DataTiddler|DataTiddlers]] A similar syntax can be used to transclude a list of tiddlers matching a specified [[TiddlerFilter|TiddlerFilters]]: