2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/TiddlerOutput.js
|
2011-12-12 10:52:04 +00:00
|
|
|
|
2012-04-05 11:21:49 +00:00
|
|
|
Serializers that output tiddlers in a variety of formats.
|
|
|
|
|
|
|
|
store.serializeTiddlers(tiddlers,type)
|
|
|
|
|
|
|
|
tiddlers: An array of tiddler objects
|
|
|
|
type: The target output type as a file extension like `.tid` or a MIME type like `application/x-tiddler`. If `null` or `undefined` then the best type is chosen automatically
|
|
|
|
|
|
|
|
The serializer returns an array of information defining one or more files containing the tiddlers:
|
|
|
|
|
|
|
|
[
|
|
|
|
{name: "title.tid", type: "application/x-tiddler", ext: ".tid", data: "xxxxx"},
|
|
|
|
{name: "title.jpg", type: "image/jpeg", ext: ".jpg", binary: true, data: "xxxxx"},
|
|
|
|
{name: "title.jpg.meta", type: "application/x-tiddler-metadata", ext: ".meta", data: "xxxxx"}
|
|
|
|
]
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
* The `type` field is the type of the file, which is not necessrily the same as the type of the tiddler.
|
|
|
|
* The `binary` field may be omitted if it is not `true`
|
2011-12-13 12:30:09 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
2011-11-22 17:42:03 +00:00
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-11-30 17:27:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2011-12-11 14:51:48 +00:00
|
|
|
var utils = require("./Utils.js"),
|
|
|
|
util = require("util");
|
2011-11-22 17:42:03 +00:00
|
|
|
|
|
|
|
var tiddlerOutput = exports;
|
|
|
|
|
2012-04-05 11:21:49 +00:00
|
|
|
var outputMetaDataBlock = function(tiddler) {
|
|
|
|
var result = [],
|
|
|
|
fields = tiddler.getFieldStrings(),
|
|
|
|
t;
|
|
|
|
for(t=0; t<fields.length; t++) {
|
|
|
|
result.push(fields[t].name + ": " + fields[t].value);
|
2011-12-11 14:51:48 +00:00
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
return result.join("\n");
|
2011-12-11 14:51:48 +00:00
|
|
|
};
|
|
|
|
|
2011-11-25 19:20:35 +00:00
|
|
|
/*
|
2012-04-05 11:21:49 +00:00
|
|
|
Output tiddlers as separate files in their native formats (ie. `.tid` or `.jpg`/`.jpg.meta`)
|
2011-11-25 19:20:35 +00:00
|
|
|
*/
|
2012-04-05 11:21:49 +00:00
|
|
|
var outputTiddlers = function(tiddlers) {
|
|
|
|
var result = [];
|
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
|
|
|
var tiddler = tiddlers[t],
|
|
|
|
extension,
|
|
|
|
binary = false;
|
|
|
|
switch(tiddler.type) {
|
|
|
|
case "image/jpeg":
|
|
|
|
extension = ".jpg";
|
|
|
|
binary = true;
|
|
|
|
break;
|
|
|
|
case "image/gif":
|
|
|
|
extension = ".gif";
|
|
|
|
binary = true;
|
2011-11-25 19:20:35 +00:00
|
|
|
break;
|
2012-04-05 11:21:49 +00:00
|
|
|
case "image/png":
|
|
|
|
extension = ".png";
|
|
|
|
binary = true;
|
2011-11-25 19:20:35 +00:00
|
|
|
break;
|
2012-04-05 11:21:49 +00:00
|
|
|
case "image/svg+xml":
|
|
|
|
extension = ".svg";
|
2011-11-25 19:20:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-04-05 11:21:49 +00:00
|
|
|
extension = ".tid";
|
2011-11-25 19:20:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
if(extension === ".tid") {
|
|
|
|
result.push({
|
|
|
|
name: tiddler.title + ".tid",
|
|
|
|
type: "application/x-tiddler",
|
|
|
|
extension: ".tid",
|
|
|
|
data: outputMetaDataBlock(tiddler) + "\n\n" + tiddler.text,
|
|
|
|
binary: false
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result.push({
|
|
|
|
name: tiddler.title,
|
|
|
|
type: tiddler.type,
|
|
|
|
extension: extension,
|
|
|
|
data: tiddler.text,
|
|
|
|
binary: binary
|
|
|
|
});
|
|
|
|
result.push({
|
|
|
|
name: tiddler.title + ".meta",
|
|
|
|
type: "application/x-tiddler-metadata",
|
|
|
|
extension: ".meta",
|
|
|
|
data: outputMetaDataBlock(tiddler),
|
|
|
|
binary: false
|
|
|
|
});
|
|
|
|
}
|
2011-11-25 19:20:35 +00:00
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
return result;
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-25 19:20:35 +00:00
|
|
|
|
2011-11-22 17:42:03 +00:00
|
|
|
/*
|
2012-04-05 11:21:49 +00:00
|
|
|
Output an array of tiddlers as HTML <DIV>s
|
2011-11-22 17:42:03 +00:00
|
|
|
out - array to push the output strings
|
|
|
|
tid - the tiddler to be output
|
2011-12-03 13:01:24 +00:00
|
|
|
The fields are in the order title, creator, modifier, created, modified, tags, followed by any others
|
2011-11-22 17:42:03 +00:00
|
|
|
*/
|
2012-04-05 11:21:49 +00:00
|
|
|
var outputTiddlerDivs = function(tiddlers) {
|
|
|
|
var result = [];
|
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
|
|
|
var tiddler = tiddlers[t],
|
|
|
|
output = [],
|
|
|
|
fieldStrings = tiddler.getFieldStrings();
|
|
|
|
output.push("<div");
|
|
|
|
for(var f=0; f<fieldStrings.length; f++) {
|
|
|
|
output.push(" " + fieldStrings[f].name + "=\"" + fieldStrings[f].value + "\"");
|
|
|
|
}
|
|
|
|
output.push(">\n<pre>");
|
|
|
|
output.push(utils.htmlEncode(tiddler.text));
|
|
|
|
output.push("</pre>\n</div>");
|
|
|
|
result.push({
|
|
|
|
name: tiddler.title,
|
|
|
|
type: "application/x-tiddler-html-div",
|
|
|
|
extension: ".tiddler",
|
|
|
|
data: output.join("")
|
|
|
|
});
|
2011-11-22 17:42:03 +00:00
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
return result;
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-11-22 17:42:03 +00:00
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
tiddlerOutput.register = function(store) {
|
2012-04-05 11:21:49 +00:00
|
|
|
store.registerTiddlerSerializer(".tid","application/x-tiddler",outputTiddlers);
|
|
|
|
store.registerTiddlerSerializer(".tiddler","application/x-tiddler-html-div",outputTiddlerDivs);
|
2011-12-01 10:19:21 +00:00
|
|
|
};
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|