2012-12-15 17:35:35 +00:00
|
|
|
/*\
|
2012-12-20 16:41:06 +00:00
|
|
|
title: $:/core/modules/parsers/wikiparser/rules/transcludeblock.js
|
2012-12-15 17:35:35 +00:00
|
|
|
type: application/javascript
|
2012-12-20 16:02:03 +00:00
|
|
|
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";
|
|
|
|
|
2012-12-26 19:32:54 +00:00
|
|
|
exports.name = "transcludeblock";
|
2012-12-20 16:02:03 +00:00
|
|
|
exports.types = {block: true};
|
2012-12-15 17:35:35 +00:00
|
|
|
|
|
|
|
exports.init = function(parser) {
|
|
|
|
this.parser = parser;
|
|
|
|
// Regexp to match
|
2014-03-17 21:44:10 +00:00
|
|
|
this.matchRegExp = /\{\{([^\{\}\|]*)(?:\|\|([^\|\{\}]+))?\}\}(?:\r?\n|$)/mg;
|
2012-12-15 17:35:35 +00:00
|
|
|
};
|
|
|
|
|
2012-12-20 15:07:38 +00:00
|
|
|
exports.parse = function() {
|
2014-03-17 21:44:10 +00:00
|
|
|
// Move past the match
|
|
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
2012-12-15 17:35:35 +00:00
|
|
|
// Move past the match
|
|
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
|
|
|
// Get the match details
|
2014-03-17 21:44:10 +00:00
|
|
|
var template = $tw.utils.trim(this.match[2]),
|
|
|
|
textRef = $tw.utils.trim(this.match[1]);
|
2013-08-24 15:45:45 +00:00
|
|
|
// Prepare the transclude widget
|
|
|
|
var transcludeNode = {
|
2014-05-14 07:51:08 +00:00
|
|
|
type: "transclude",
|
2013-11-11 22:29:38 +00:00
|
|
|
attributes: {},
|
2013-08-24 15:45:45 +00:00
|
|
|
isBlock: true
|
|
|
|
};
|
2014-03-17 21:44:10 +00:00
|
|
|
// Prepare the tiddler widget
|
2014-08-30 19:44:26 +00:00
|
|
|
var tr, targetTitle, targetField, targetIndex, tiddlerNode;
|
2014-03-17 21:44:10 +00:00
|
|
|
if(textRef) {
|
2014-08-30 19:44:26 +00:00
|
|
|
tr = $tw.utils.parseTextReference(textRef);
|
|
|
|
targetTitle = tr.title;
|
|
|
|
targetField = tr.field;
|
|
|
|
targetIndex = tr.index;
|
|
|
|
tiddlerNode = {
|
|
|
|
type: "tiddler",
|
|
|
|
attributes: {
|
|
|
|
tiddler: {type: "string", value: targetTitle}
|
|
|
|
},
|
|
|
|
isBlock: true,
|
|
|
|
children: [transcludeNode]
|
|
|
|
};
|
2014-03-17 21:44:10 +00:00
|
|
|
}
|
2013-11-11 22:29:38 +00:00
|
|
|
if(template) {
|
|
|
|
transcludeNode.attributes.tiddler = {type: "string", value: template};
|
2014-03-17 21:44:10 +00:00
|
|
|
if(textRef) {
|
|
|
|
return [tiddlerNode];
|
|
|
|
} else {
|
|
|
|
return [transcludeNode];
|
2013-11-11 22:29:38 +00:00
|
|
|
}
|
2014-03-17 21:44:10 +00:00
|
|
|
} 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];
|
2013-11-11 22:29:38 +00:00
|
|
|
}
|
2013-01-15 17:50:47 +00:00
|
|
|
}
|
2012-12-15 17:35:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|