From 3070357d70810edcaae657f2a0af2a9e2d0ee134 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sun, 2 Sep 2012 20:28:32 +0100 Subject: [PATCH] Refactor serialisers to work on an array of tiddlers --- core/modules/macros/serialize.js | 7 +- core/modules/serializers.js | 248 ++++++++++++++++++------------- core/modules/wiki.js | 11 +- 3 files changed, 152 insertions(+), 114 deletions(-) diff --git a/core/modules/macros/serialize.js b/core/modules/macros/serialize.js index e42c5f802..cea5e1ad5 100644 --- a/core/modules/macros/serialize.js +++ b/core/modules/macros/serialize.js @@ -26,6 +26,7 @@ exports.executeMacro = function() { t; if(this.hasParameter("filter")) { var titles = this.wiki.filterTiddlers(this.params.filter), + tiddlers = [], result = []; if(this.hasParameter("removePrefix")) { for(t=0; t\n
");
-	result.push($tw.utils.htmlEncode(tiddler.fields.text));
-	result.push("
\n"); return result.join(""); +} + +exports["text/plain"] = function(tiddlers) { + return mapEachTiddler(this,tiddlers,function(tiddler) { + return tiddler.fields.text; + }); +}; + +exports["text/html"] = function(tiddlers) { + return mapEachTiddler(this,tiddlers,function(tiddler) { + return this.renderTiddler("text/html",tiddler.fields.title); + }); +}; + +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 + for(var f in tiddler.fields) { + if(f !== "text") { + attributes["data-tiddler-" + f] = tiddler.getFieldString(f); + } + } + return $tw.Tree.Element( + "style", + attributes, + [$tw.Tree.Raw(tiddler.fields.text)] + ).render("text/html"); + }); +}; + +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 + for(var f in tiddler.fields) { + if(f !== "text") { + attributes["data-tiddler-" + f] = tiddler.getFieldString(f); + } + } + return $tw.Tree.Element( + "script", + attributes, + [$tw.Tree.Raw(tiddler.fields.text)] + ).render("text/html"); + }); +}; + +exports["application/x-tiddler-module"] = function(tiddlers) { + return mapEachTiddler(this,tiddlers,function(tiddler) { + var attributes = { + type: "text/javascript", + "data-module": "yes" + }, // The script type is set to text/javascript for compatibility with old browsers + text = tiddler.fields.text; + text = "$tw.modules.define(\"" + tiddler.fields.title + "\",\"" + tiddler.fields["module-type"] + "\",function(module,exports,require) {" + text + "});\n"; + for(var f in tiddler.fields) { + if(f !== "text") { + attributes["data-tiddler-" + f] = tiddler.getFieldString(f); + } + } + return $tw.Tree.Element( + "script", + attributes, + [$tw.Tree.Raw(text)] + ).render("text/html"); + }); +}; + +exports["application/x-tiddler-library"] = 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 + text = tiddler.fields.text; + for(var f in tiddler.fields) { + if(f !== "text") { + attributes["data-tiddler-" + f] = tiddler.getFieldString(f); + } + } + return $tw.Tree.Element( + "script", + attributes, + [$tw.Tree.Raw(text)] + ).render("text/html"); + }); +}; + +exports["application/x-tiddler-html-div"] = function(tiddlers) { + return mapEachTiddler(this,tiddlers,function(tiddler) { + var result = [], + fields = [], + pullField = function(name) { + var fieldIndex = fields.indexOf(name); + if(fieldIndex !== -1) { + fields.splice(fieldIndex,1); + fields.unshift(name); + } + }; + result.push("\n
");
+		result.push($tw.utils.htmlEncode(tiddler.fields.text));
+		result.push("
\n"); + 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
" + $tw.utils.htmlEncode($tw.crypto.encrypt(JSON.stringify(jsonTiddlers))) + "
"; }; })(); diff --git a/core/modules/wiki.js b/core/modules/wiki.js index a0ec0682d..152752ccc 100644 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -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]; - if(typeof tiddler === "string") { - tiddler = this.getTiddler(tiddler); - } - if(serializer && tiddler) { - return serializer.call(this,tiddler); + if(serializer) { + return serializer.call(this,tiddlers); } else { return null; }