mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-07 22:33:50 +00:00
Refactor serialisers to work on an array of tiddlers
This commit is contained in:
parent
6a0998059e
commit
3070357d70
@ -26,6 +26,7 @@ exports.executeMacro = function() {
|
|||||||
t;
|
t;
|
||||||
if(this.hasParameter("filter")) {
|
if(this.hasParameter("filter")) {
|
||||||
var titles = this.wiki.filterTiddlers(this.params.filter),
|
var titles = this.wiki.filterTiddlers(this.params.filter),
|
||||||
|
tiddlers = [],
|
||||||
result = [];
|
result = [];
|
||||||
if(this.hasParameter("removePrefix")) {
|
if(this.hasParameter("removePrefix")) {
|
||||||
for(t=0; t<titles.length; t++) {
|
for(t=0; t<titles.length; t++) {
|
||||||
@ -33,15 +34,15 @@ exports.executeMacro = function() {
|
|||||||
title = titles[t];
|
title = titles[t];
|
||||||
if(title.indexOf(this.params.removePrefix) === 0) {
|
if(title.indexOf(this.params.removePrefix) === 0) {
|
||||||
title = title.substr(this.params.removePrefix.length);
|
title = title.substr(this.params.removePrefix.length);
|
||||||
var modifiedTiddler = new $tw.Tiddler(originalTiddler,{title: title});
|
tiddlers.push(new $tw.Tiddler(originalTiddler,{title: title}));
|
||||||
result.push(this.wiki.serializeTiddler(modifiedTiddler,as));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(t=0; t<titles.length; t++) {
|
for(t=0; t<titles.length; t++) {
|
||||||
result.push(this.wiki.serializeTiddler(titles[t],as));
|
tiddlers.push(this.wiki.getTiddler(titles[t]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.push(this.wiki.serializeTiddlers(tiddlers,as));
|
||||||
return $tw.Tree.Element("pre",{},[
|
return $tw.Tree.Element("pre",{},[
|
||||||
$tw.Tree.Text(result.join("\n"))
|
$tw.Tree.Text(result.join("\n"))
|
||||||
]);
|
]);
|
||||||
|
@ -12,16 +12,32 @@ Functions to serialise tiddlers to a block of text
|
|||||||
/*global $tw: false */
|
/*global $tw: false */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
exports["text/plain"] = function(tiddler) {
|
// Helper function
|
||||||
return tiddler ? tiddler.fields.text : "";
|
var mapEachTiddler = function(wiki,tiddlers,callback) {
|
||||||
|
var result = [];
|
||||||
|
for(var t=0; t<tiddlers.length; t++) {
|
||||||
|
var tiddler = tiddlers[t];
|
||||||
|
if(tiddler) {
|
||||||
|
result.push(callback.call(wiki,tiddler));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
exports["text/plain"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
|
return tiddler.fields.text;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["text/html"] = function(tiddler) {
|
exports["text/html"] = function(tiddlers) {
|
||||||
var text = this.renderTiddler("text/html",tiddler.fields.title);
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
return text ? text : "";
|
return this.renderTiddler("text/html",tiddler.fields.title);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["application/x-tiddler-css"] = function(tiddler) {
|
exports["application/x-tiddler-css"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
var attributes = {type: "text/css"}; // The script type is set to text/javascript for compatibility with old browsers
|
var attributes = {type: "text/css"}; // The script type is set to text/javascript for compatibility with old browsers
|
||||||
for(var f in tiddler.fields) {
|
for(var f in tiddler.fields) {
|
||||||
if(f !== "text") {
|
if(f !== "text") {
|
||||||
@ -33,9 +49,11 @@ exports["application/x-tiddler-css"] = function(tiddler) {
|
|||||||
attributes,
|
attributes,
|
||||||
[$tw.Tree.Raw(tiddler.fields.text)]
|
[$tw.Tree.Raw(tiddler.fields.text)]
|
||||||
).render("text/html");
|
).render("text/html");
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["application/javascript"] = function(tiddler) {
|
exports["application/javascript"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
var attributes = {type: "text/javascript"}; // The script type is set to text/javascript for compatibility with old browsers
|
var attributes = {type: "text/javascript"}; // The script type is set to text/javascript for compatibility with old browsers
|
||||||
for(var f in tiddler.fields) {
|
for(var f in tiddler.fields) {
|
||||||
if(f !== "text") {
|
if(f !== "text") {
|
||||||
@ -47,9 +65,11 @@ exports["application/javascript"] = function(tiddler) {
|
|||||||
attributes,
|
attributes,
|
||||||
[$tw.Tree.Raw(tiddler.fields.text)]
|
[$tw.Tree.Raw(tiddler.fields.text)]
|
||||||
).render("text/html");
|
).render("text/html");
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["application/x-tiddler-module"] = function(tiddler) {
|
exports["application/x-tiddler-module"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
var attributes = {
|
var attributes = {
|
||||||
type: "text/javascript",
|
type: "text/javascript",
|
||||||
"data-module": "yes"
|
"data-module": "yes"
|
||||||
@ -66,9 +86,11 @@ exports["application/x-tiddler-module"] = function(tiddler) {
|
|||||||
attributes,
|
attributes,
|
||||||
[$tw.Tree.Raw(text)]
|
[$tw.Tree.Raw(text)]
|
||||||
).render("text/html");
|
).render("text/html");
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["application/x-tiddler-library"] = function(tiddler) {
|
exports["application/x-tiddler-library"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
var attributes = {
|
var attributes = {
|
||||||
type: "text/javascript"
|
type: "text/javascript"
|
||||||
}, // The script type is set to text/javascript for compatibility with old browsers
|
}, // The script type is set to text/javascript for compatibility with old browsers
|
||||||
@ -83,9 +105,11 @@ exports["application/x-tiddler-library"] = function(tiddler) {
|
|||||||
attributes,
|
attributes,
|
||||||
[$tw.Tree.Raw(text)]
|
[$tw.Tree.Raw(text)]
|
||||||
).render("text/html");
|
).render("text/html");
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports["application/x-tiddler-html-div"] = function(tiddler) {
|
exports["application/x-tiddler-html-div"] = function(tiddlers) {
|
||||||
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
||||||
var result = [],
|
var result = [],
|
||||||
fields = [],
|
fields = [],
|
||||||
pullField = function(name) {
|
pullField = function(name) {
|
||||||
@ -119,6 +143,22 @@ exports["application/x-tiddler-html-div"] = function(tiddler) {
|
|||||||
result.push($tw.utils.htmlEncode(tiddler.fields.text));
|
result.push($tw.utils.htmlEncode(tiddler.fields.text));
|
||||||
result.push("</pre>\n</div>");
|
result.push("</pre>\n</div>");
|
||||||
return result.join("");
|
return result.join("");
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
exports["application/x-tiddler-encrypted-div"] = function(tiddlers) {
|
||||||
|
// Build up the JSON object representing the tiddlers
|
||||||
|
var jsonTiddlers = {},
|
||||||
|
t, f;
|
||||||
|
for(t=0; t<tiddlers.length; t++) {
|
||||||
|
var tiddler = tiddlers[t],
|
||||||
|
jsonTiddler = jsonTiddlers[tiddler.fields.title] = {};
|
||||||
|
for(f in tiddler.fields) {
|
||||||
|
jsonTiddler[f] = tiddler.getFieldString(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Encrypt the JSON of the tiddlers
|
||||||
|
return "<div data-tw-encrypted-tiddlers='yes'><pre>" + $tw.utils.htmlEncode($tw.crypto.encrypt(JSON.stringify(jsonTiddlers))) + "</pre></div>";
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -193,15 +193,12 @@ exports.addTiddler = function(tiddler,isShadow) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Serialise a tiddler to a specified text serialization format
|
Serialise tiddlers to a specified text serialization format
|
||||||
*/
|
*/
|
||||||
exports.serializeTiddler = function(tiddler,type) {
|
exports.serializeTiddlers = function(tiddlers,type) {
|
||||||
var serializer = $tw.Wiki.tiddlerSerializerModules[type];
|
var serializer = $tw.Wiki.tiddlerSerializerModules[type];
|
||||||
if(typeof tiddler === "string") {
|
if(serializer) {
|
||||||
tiddler = this.getTiddler(tiddler);
|
return serializer.call(this,tiddlers);
|
||||||
}
|
|
||||||
if(serializer && tiddler) {
|
|
||||||
return serializer.call(this,tiddler);
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user