1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-26 07:13:15 +00:00

Added new transclude wikitext rule and macro

Which allows us to get rid of the tags macro
This commit is contained in:
Jeremy Ruston 2012-06-19 08:57:29 +01:00
parent 987a50c8c4
commit 0d928c05a5
5 changed files with 135 additions and 49 deletions

View File

@ -1,47 +0,0 @@
/*\
title: $:/core/modules/macros/tags.js
type: application/javascript
module-type: macro
Implements the tags macro.
{{{
<<tags template:MyTagTemplate>>
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "tags",
dependentOnContextTiddler: true,
params: {
template: {byName: true, type: "tiddler"},
filter: {byName: true, type: "filter"}
}
};
exports.executeMacro = function() {
var tiddler = this.wiki.getTiddler(this.tiddlerTitle),
child = $tw.Tree.Element("div",{"class":"tw-tags-wrapper"},[]),
parents = this.parents.slice(0);
if(tiddler && tiddler.fields.tags) {
for(var t=0; t<tiddler.fields.tags.length; t++) {
var tag = tiddler.fields.tags[t];
child.children.push($tw.Tree.Macro("tiddler",{
srcParams: {target: tag,template: this.params.template},
wiki: this.wiki,
isBlock: false
}));
}
}
parents.push(this.tiddlerTitle);
child.execute(parents,this.tiddlerTitle);
return child;
};
})();

View File

@ -0,0 +1,72 @@
/*\
title: $:/core/modules/macros/transclude.js
type: application/javascript
module-type: macro
Transclude macro
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "transclude",
params: {
filter: {byPos: 0, type: "filter"},
title: {byPos: 1, type: "tiddler"},
templateTitle: {byName: true, type: "text"},
templateText: {byName: true, type: "text"}
}
};
exports.executeMacro = function() {
var titles,templateTiddler,templateText,t,title,templateParseTree,
nodes,node,c,
parents = this.parents;
// Get the tiddlers we're transcluding
if(this.hasParameter("filter")) {
titles = this.wiki.filterTiddlers(this.params.filter,this.tiddlerTitle);
} else if(this.hasParameter("title")) {
titles = [this.params.title];
} else {
titles = [this.tiddlerTitle];
}
// Get the template
if(this.hasParameter("templateText")) {
// Parse the template
templateParseTree = this.wiki.parseText("text/x-tiddlywiki",this.params.templateText);
} else {
if(this.hasParameter("templateTitle")) {
// Check for recursion
if(parents.indexOf(this.params.templateTitle) !== -1) {
return $tw.Tree.errorNode("Tiddler recursion error in <<transclude>> macro");
}
parents = parents.slice(0);
parents.push(this.params.templateTitle);
templateParseTree = this.wiki.parseTiddler(this.params.templateTitle);
} else {
templateParseTree = this.wiki.parseText("text/x-tiddlywiki","<<view text wikified>>");
}
}
// Render the tiddlers through the template
nodes = [];
for(t=0; t<titles.length; t++) {
title = titles[t];
for(c=0; c<templateParseTree.tree.length; c++) {
node = templateParseTree.tree[c].clone();
node.execute(parents,title);
nodes.push(node);
}
}
// Return
var attributes = {"class": ["tw-transclusion"]};
if(this.classes) {
$tw.utils.pushTop(attributes["class"],this.classes);
}
return $tw.Tree.Element("div",attributes,nodes);
};
})();

View File

@ -0,0 +1,59 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/rules/transclude.js
type: application/javascript
module-type: wikitextrule
Wiki text rule for transclusion. For example:
{{{
((MyTiddler))
((MyTiddler)(MyTemplate))
((MyTiddler)Template <<view text>>)
(((My filter expression)))
(((My filter expression))(MyTemplate))
(((My filter expression))Template <<view text>>)
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "transclude";
exports.runParser = true;
exports.blockParser = true;
exports.regExpString = "\\(\\((?:(?:[^\\(\\)]+)|(?:\\([^\\(\\)]+\\)))\\)(?:\\([^\\)]+\\)|(?:[^\\)]+))?\\)";
exports.parse = function(match,isBlock) {
var regExp = /\(\((?:([^\(\)]+)|(?:\(([^\(\)]+)\)))\)(?:\(([^\)]+)\)|([^\)]+))?\)((?:\r?\n)?)/mg;
regExp.lastIndex = this.pos;
match = regExp.exec(this.source);
if(match && match.index === this.pos) {
this.pos = match.index + match[0].length;
var params = {};
// Check if it is a single tiddler
if(match[1]) {
params.title = match[1];
} else {
// Else it is a filter
params.filter = match[2];
}
if(match[3]) {
params.templateTitle = match[3];
}
if(match[4]) {
params.templateText = match[4];
}
return [$tw.Tree.Macro("transclude",{
srcParams: params,
wiki: this.wiki
})];
}
return [];
};
})();

View File

@ -7,7 +7,9 @@ modifier: JeremyRuston
{{small{
<div><<view modifier link>> <<view modified date>></div>
}}}
<<tags template:"$:/templates/TagTemplate">>
{{tw-tags-wrapper{
((([is[current]tags[]]))($:/templates/TagTemplate))
}}}
<div class="body">
<<view text wikified>>
</div>

File diff suppressed because one or more lines are too long