2012-05-05 12:15:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/serializers.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: tiddlerserializer
|
|
|
|
|
|
|
|
Plugins to serialise tiddlers to a block of text
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*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 : "";
|
|
|
|
};
|
|
|
|
|
2012-05-06 13:14:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tw.Tree.Element(
|
|
|
|
"style",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(tiddler.fields.text)]
|
|
|
|
).render("text/html");
|
|
|
|
};
|
|
|
|
|
2012-05-05 16:42:42 +00:00
|
|
|
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");
|
|
|
|
};
|
|
|
|
|
2012-05-05 13:15:48 +00:00
|
|
|
exports["application/x-tiddler-module"] = function(tiddler) {
|
2012-05-05 16:42:42 +00:00
|
|
|
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-06-08 09:41:58 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 16:42:42 +00:00
|
|
|
return $tw.Tree.Element(
|
|
|
|
"script",
|
|
|
|
attributes,
|
|
|
|
[$tw.Tree.Raw(text)]
|
|
|
|
).render("text/html");
|
2012-05-05 13:15:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports["application/x-tiddler-html-div"] = function(tiddler) {
|
2012-05-29 21:45:43 +00:00
|
|
|
var result = [],
|
|
|
|
fields = [],
|
|
|
|
pullField = function(name) {
|
|
|
|
var fieldIndex = fields.indexOf(name);
|
|
|
|
if(fieldIndex !== -1) {
|
|
|
|
fields.splice(fieldIndex,1);
|
|
|
|
fields.unshift(name);
|
|
|
|
}
|
|
|
|
};
|
2012-05-05 13:15:48 +00:00
|
|
|
result.push("<div");
|
2012-05-29 21:45:43 +00:00
|
|
|
// Collect the field names in the tiddler
|
2012-05-05 13:15:48 +00:00
|
|
|
for(var f in tiddler.fields) {
|
|
|
|
if(f !== "text") {
|
2012-05-08 16:42:49 +00:00
|
|
|
fields.push(f);
|
2012-05-05 13:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-29 21:45:43 +00:00
|
|
|
// Sort the fields
|
2012-05-08 16:42:49 +00:00
|
|
|
fields.sort();
|
2012-05-29 21:45:43 +00:00
|
|
|
// Pull the standard fields up to the top
|
|
|
|
pullField("tags");
|
|
|
|
pullField("modified");
|
|
|
|
pullField("created");
|
|
|
|
pullField("modifier");
|
|
|
|
pullField("creator");
|
|
|
|
pullField("title");
|
|
|
|
// Output the fields
|
2012-05-08 16:42:49 +00:00
|
|
|
for(f=0; f<fields.length; f++) {
|
|
|
|
result.push(" " + fields[f] + "=\"" + tiddler.getFieldString(fields[f]) + "\"");
|
|
|
|
}
|
2012-05-05 13:15:48 +00:00
|
|
|
result.push(">\n<pre>");
|
|
|
|
result.push($tw.utils.htmlEncode(tiddler.fields.text));
|
|
|
|
result.push("</pre>\n</div>");
|
|
|
|
return result.join("");
|
|
|
|
};
|
|
|
|
|
2012-05-05 12:15:31 +00:00
|
|
|
})();
|