2012-01-07 17:33:42 +00:00
|
|
|
/*\
|
|
|
|
title: js/macros/tiddler.js
|
|
|
|
|
2012-01-27 18:00:49 +00:00
|
|
|
The tiddler macros transcludes another macro into the tiddler being rendered.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
target: the title of the tiddler to transclude
|
|
|
|
template: the title of the tiddler to use as a template for the transcluded tiddler
|
|
|
|
with: optional parameters to be substituted into the rendered tiddler
|
|
|
|
|
|
|
|
The simplest case is to just supply a target tiddler:
|
|
|
|
|
|
|
|
<<tiddler Foo>> or <<transclude target:Foo>>
|
|
|
|
|
|
|
|
This will render the tiddler Foo within the current tiddler. If the tiddler Foo includes
|
|
|
|
the view macro (or other macros that reference the fields of the current tiddler), then the
|
|
|
|
fields of the tiddler Foo will be accessed.
|
|
|
|
|
|
|
|
If you want to transclude the tiddler as a template, so that the fields referenced by the view
|
|
|
|
macro are those of the tiddler doing the transcluding, then you can instead specify the tiddler
|
|
|
|
as a template:
|
|
|
|
|
|
|
|
<<tiddler template:Foo>>
|
|
|
|
|
|
|
|
The effect is the same as the previous example: the text of the tiddler Foo is rendered. The
|
|
|
|
difference is that the view macro will access the fields of the tiddler doing the transcluding.
|
|
|
|
|
|
|
|
The `target` and `template` parameters may be combined:
|
|
|
|
|
|
|
|
<<tiddler target:Foo template:Bar>>
|
|
|
|
|
|
|
|
Here, the text of the tiddler `Bar` will be transcluded, with the macros within it accessing the fields
|
|
|
|
of the tiddler `Foo`.
|
|
|
|
|
|
|
|
Finally, the `with` parameter is used to substitute values for the special markers $1, $2, $3 etc. The
|
|
|
|
substitutions are performed on the tiddler whose text is being rendered: either the tiddler named in
|
|
|
|
the `template` parameter or, if that parameter is missing, the tiddler named in the `target` parameter.
|
|
|
|
|
2012-01-07 17:33:42 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
2012-02-16 20:38:10 +00:00
|
|
|
var Renderer = require("../Renderer.js").Renderer;
|
2012-01-07 17:33:42 +00:00
|
|
|
|
|
|
|
exports.macro = {
|
|
|
|
name: "tiddler",
|
|
|
|
cascadeParams: true, // Cascade names of named parameters to following anonymous parameters
|
|
|
|
params: {
|
2012-02-17 17:32:32 +00:00
|
|
|
target: {byName: "default", type: "tiddler"},
|
|
|
|
template: {byName: true, type: "tiddler"},
|
|
|
|
"with": {byName: true, type: "text", dependentAll: true}
|
2012-01-07 17:33:42 +00:00
|
|
|
},
|
2012-02-21 21:57:30 +00:00
|
|
|
execute: function() {
|
2012-03-29 12:44:45 +00:00
|
|
|
var renderTitle = this.params.target,
|
2012-02-21 21:57:30 +00:00
|
|
|
renderTemplate = this.params.template,
|
2012-02-16 20:38:10 +00:00
|
|
|
content,
|
|
|
|
contentClone = [],
|
2012-02-19 17:20:16 +00:00
|
|
|
t,
|
2012-02-21 21:57:30 +00:00
|
|
|
parents = this.parents.slice(0);
|
2012-03-29 12:44:45 +00:00
|
|
|
// If there's no render title specified then use the current tiddler title
|
2012-02-01 16:12:49 +00:00
|
|
|
if(typeof renderTitle !== "string") {
|
2012-03-29 12:44:45 +00:00
|
|
|
renderTitle = this.tiddlerTitle;
|
2012-02-01 16:12:49 +00:00
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
// If there's no template specified then use the target tiddler title
|
2012-02-01 16:12:49 +00:00
|
|
|
if(typeof renderTemplate !== "string") {
|
|
|
|
renderTemplate = renderTitle;
|
2012-01-27 18:43:39 +00:00
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
// Check for recursion
|
|
|
|
if(parents.indexOf(renderTemplate) !== -1) {
|
|
|
|
content = [Renderer.ErrorNode("Tiddler recursion error in <<tiddler>> macro")];
|
|
|
|
} else {
|
2012-02-21 21:57:30 +00:00
|
|
|
if("with" in this.params) {
|
2012-02-19 17:20:16 +00:00
|
|
|
// Parameterised transclusion
|
2012-02-21 21:57:30 +00:00
|
|
|
var targetTiddler = this.store.getTiddler(renderTemplate),
|
2012-02-19 17:20:16 +00:00
|
|
|
text = targetTiddler.text;
|
2012-02-21 21:57:30 +00:00
|
|
|
var withTokens = [this.params["with"]];
|
2012-02-19 17:20:16 +00:00
|
|
|
for(t=0; t<withTokens.length; t++) {
|
|
|
|
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
|
|
|
|
text = text.replace(placeholderRegExp,withTokens[t]);
|
|
|
|
}
|
2012-03-03 18:39:13 +00:00
|
|
|
content = this.store.parseText(targetTiddler.type,text).nodes;
|
2012-02-19 17:20:16 +00:00
|
|
|
} else {
|
|
|
|
// There's no parameterisation, so we can just render the target tiddler directly
|
2012-02-21 21:57:30 +00:00
|
|
|
var parseTree = this.store.parseTiddler(renderTemplate);
|
2012-03-03 18:39:13 +00:00
|
|
|
content = parseTree ? parseTree.nodes : [];
|
2012-01-07 20:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
// Update the stack of tiddler titles for recursion detection
|
2012-02-19 17:20:16 +00:00
|
|
|
parents.push(renderTemplate);
|
2012-03-29 12:44:45 +00:00
|
|
|
// Clone the content
|
2012-02-16 20:38:10 +00:00
|
|
|
for(t=0; t<content.length; t++) {
|
|
|
|
contentClone.push(content[t].clone());
|
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
// Execute macros within the content
|
2012-02-16 20:38:10 +00:00
|
|
|
for(t=0; t<contentClone.length; t++) {
|
2012-03-29 13:30:22 +00:00
|
|
|
contentClone[t].execute(parents,renderTitle);
|
2012-02-16 20:38:10 +00:00
|
|
|
}
|
2012-03-29 13:57:54 +00:00
|
|
|
// Set up the attributes for the wrapper element
|
|
|
|
var attributes = {
|
|
|
|
"data-tiddler-target": renderTitle,
|
|
|
|
"data-tiddler-template": renderTemplate,
|
|
|
|
"class": ["tiddlerFrame"]
|
|
|
|
};
|
|
|
|
if(!this.store.tiddlerExists(renderTitle)) {
|
|
|
|
attributes["class"].push("tiddlerMissing");
|
|
|
|
}
|
2012-03-29 12:44:45 +00:00
|
|
|
// Return the content
|
2012-03-29 13:57:54 +00:00
|
|
|
return [Renderer.ElementNode("div",attributes,contentClone)];
|
2012-01-07 17:33:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|