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}}`.
This commit is contained in:
Jermolene
2014-03-17 21:44:10 +00:00
parent cdf3e101a8
commit bdbbf94326
4 changed files with 82 additions and 48 deletions

View File

@@ -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];
};
})();