1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-19 18:59:42 +00:00
TiddlyWiki5/core/modules/parsers/wikiparser/rules/transcludeblock.js

90 lines
2.2 KiB
JavaScript
Raw Normal View History

2012-12-15 17:35:35 +00:00
/*\
title: $:/core/modules/parsers/wikiparser/rules/transcludeblock.js
2012-12-15 17:35:35 +00:00
type: application/javascript
module-type: wikirule
2012-12-15 17:35:35 +00:00
Wiki text rule for block-level transclusion. For example:
2012-12-22 23:09:44 +00:00
```
2012-12-15 17:35:35 +00:00
{{MyTiddler}}
2012-12-23 10:36:55 +00:00
{{MyTiddler||TemplateTitle}}
2012-12-22 23:09:44 +00:00
```
2012-12-15 17:35:35 +00:00
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "transcludeblock";
exports.types = {block: true};
2012-12-15 17:35:35 +00:00
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /\{\{([^\{\}\|]*)(?:\|\|([^\|\{\}]+))?(?:\|([^\{\}]+))?\}\}(?:\r?\n|$)/mg;
2012-12-15 17:35:35 +00:00
};
exports.parse = function() {
2012-12-15 17:35:35 +00:00
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
// Get the match details
var template = $tw.utils.trim(this.match[2]),
textRef = $tw.utils.trim(this.match[1]),
params = this.match[3] ? this.match[3].split("|") : [];
// Prepare the transclude widget
var transcludeNode = {
type: "transclude",
attributes: {},
isBlock: true
};
$tw.utils.each(params,function(paramValue,index) {
var name = "" + index;
2022-09-03 15:36:36 +00:00
transcludeNode.attributes[name] = {
name: name,
type: "string",
value: paramValue
}
});
// Prepare the tiddler widget
var tr, targetTitle, targetField, targetIndex, tiddlerNode;
if(textRef) {
tr = $tw.utils.parseTextReference(textRef);
targetTitle = tr.title;
targetField = tr.field;
targetIndex = tr.index;
tiddlerNode = {
type: "tiddler",
attributes: {
2022-04-26 20:55:43 +00:00
tiddler: {name: "tiddler", type: "string", value: targetTitle}
},
isBlock: true,
children: [transcludeNode]
};
}
if(template) {
2022-04-26 20:55:43 +00:00
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: template};
if(textRef) {
return [tiddlerNode];
} else {
return [transcludeNode];
}
} else {
if(textRef) {
2022-04-26 20:55:43 +00:00
transcludeNode.attributes["$tiddler"] = {name: "$tiddler", type: "string", value: targetTitle};
if(targetField) {
2022-04-26 20:55:43 +00:00
transcludeNode.attributes["$field"] = {name: "$field", type: "string", value: targetField};
}
if(targetIndex) {
2022-04-26 20:55:43 +00:00
transcludeNode.attributes["$index"] = {name: "$index", type: "string", value: targetIndex};
}
return [tiddlerNode];
} else {
return [transcludeNode];
}
}
2012-12-15 17:35:35 +00:00
};
})();