2012-05-05 12:15:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/serializers.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: tiddlerserializer
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
Functions to serialise tiddlers to a block of text
|
2012-05-05 12:15:31 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
// 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 result.join("");
|
2012-10-10 15:32:55 +00:00
|
|
|
};
|
2012-09-02 19:28:32 +00:00
|
|
|
|
|
|
|
exports["text/plain"] = function(tiddlers) {
|
|
|
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
|
|
|
return tiddler.fields.text;
|
|
|
|
});
|
2012-05-05 12:15:31 +00:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
exports["text/html"] = function(tiddlers) {
|
|
|
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
|
|
|
return this.renderTiddler("text/html",tiddler.fields.title);
|
|
|
|
});
|
2012-05-05 12:15:31 +00:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-05-06 13:14:27 +00:00
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
return $tw.Tree.Element(
|
|
|
|
"style",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(tiddler.fields.text)]
|
|
|
|
).render("text/html");
|
|
|
|
});
|
2012-05-06 13:14:27 +00:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-05-05 16:42:42 +00:00
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
return $tw.Tree.Element(
|
|
|
|
"script",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(tiddler.fields.text)]
|
|
|
|
).render("text/html");
|
|
|
|
});
|
2012-05-05 16:42:42 +00:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-05-05 16:42:42 +00:00
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
return $tw.Tree.Element(
|
|
|
|
"script",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(text)]
|
|
|
|
).render("text/html");
|
|
|
|
});
|
2012-06-08 09:41:58 +00:00
|
|
|
};
|
|
|
|
|
2012-10-10 15:32:55 +00:00
|
|
|
exports["application/x-tiddler-module-plain"] = function(tiddlers) {
|
|
|
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
|
|
|
return "$tw.modules.define(\"" + tiddler.fields.title + "\",\"" + tiddler.fields["module-type"] + "\",function(module,exports,require) {" + tiddler.fields.text + "});\n";
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-06-08 09:41:58 +00:00
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
return $tw.Tree.Element(
|
|
|
|
"script",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(text)]
|
|
|
|
).render("text/html");
|
|
|
|
});
|
2012-05-05 13:15:48 +00:00
|
|
|
};
|
|
|
|
|
2012-09-02 19:28:32 +00:00
|
|
|
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);
|
2012-05-29 21:45:43 +00:00
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
}
|
|
|
|
// 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);
|
2012-05-05 13:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-02 19:28:32 +00:00
|
|
|
// 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>";
|
2012-05-05 13:15:48 +00:00
|
|
|
};
|
|
|
|
|
2012-10-12 18:01:19 +00:00
|
|
|
exports["application/x-tiddler-javascript"] = function(tiddlers) {
|
|
|
|
return mapEachTiddler(this,tiddlers,function(tiddler) {
|
|
|
|
return "$tw.preloadTiddler(" + JSON.stringify(tiddler.fields) + ");\n"
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-05-05 12:15:31 +00:00
|
|
|
})();
|