1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-28 02:50:27 +00:00

Refactor serialisers to work on an array of tiddlers

This commit is contained in:
Jeremy Ruston 2012-09-02 20:28:32 +01:00
parent 6a0998059e
commit 3070357d70
3 changed files with 152 additions and 114 deletions

View File

@ -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<titles.length; t++) {
@ -33,15 +34,15 @@ exports.executeMacro = function() {
title = titles[t];
if(title.indexOf(this.params.removePrefix) === 0) {
title = title.substr(this.params.removePrefix.length);
var modifiedTiddler = new $tw.Tiddler(originalTiddler,{title: title});
result.push(this.wiki.serializeTiddler(modifiedTiddler,as));
tiddlers.push(new $tw.Tiddler(originalTiddler,{title: title}));
}
}
} else {
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",{},[
$tw.Tree.Text(result.join("\n"))
]);

View File

@ -12,113 +12,153 @@ Functions to serialise tiddlers to a block of text
/*global $tw: false */
"use strict";
exports["text/plain"] = function(tiddler) {
return tiddler ? tiddler.fields.text : "";
};
exports["text/html"] = function(tiddler) {
var text = this.renderTiddler("text/html",tiddler.fields.title);
return text ? text : "";
};
exports["application/x-tiddler-css"] = 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);
// Helper function
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 $tw.Tree.Element(
"style",
attributes,
[$tw.Tree.Raw(tiddler.fields.text)]
).render("text/html");
};
exports["application/javascript"] = 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(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(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(tiddler) {
var result = [],
fields = [],
pullField = function(name) {
var fieldIndex = fields.indexOf(name);
if(fieldIndex !== -1) {
fields.splice(fieldIndex,1);
fields.unshift(name);
}
};
result.push("<div");
// Collect the field names in the tiddler
for(var f in tiddler.fields) {
if(f !== "text") {
fields.push(f);
}
}
// Sort the fields
fields.sort();
// Pull the standard fields up to the top
pullField("tags");
pullField("modified");
pullField("created");
pullField("modifier");
pullField("creator");
pullField("title");
// Output the fields
for(f=0; f<fields.length; f++) {
result.push(" " + fields[f] + "=\"" + $tw.utils.htmlEncode(tiddler.getFieldString(fields[f])) + "\"");
}
result.push(">\n<pre>");
result.push($tw.utils.htmlEncode(tiddler.fields.text));
result.push("</pre>\n</div>");
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("<div");
// Collect the field names in the tiddler
for(var f in tiddler.fields) {
if(f !== "text") {
fields.push(f);
}
}
// Sort the fields
fields.sort();
// Pull the standard fields up to the top
pullField("tags");
pullField("modified");
pullField("created");
pullField("modifier");
pullField("creator");
pullField("title");
// Output the fields
for(f=0; f<fields.length; f++) {
result.push(" " + fields[f] + "=\"" + $tw.utils.htmlEncode(tiddler.getFieldString(fields[f])) + "\"");
}
result.push(">\n<pre>");
result.push($tw.utils.htmlEncode(tiddler.fields.text));
result.push("</pre>\n</div>");
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>";
};
})();

View File

@ -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;
}