2011-12-13 12:30:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/TiddlerConverters.js
|
|
|
|
|
|
|
|
\*/
|
2011-12-12 10:52:04 +00:00
|
|
|
(function(){
|
|
|
|
|
2011-12-11 14:51:48 +00:00
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var TiddlerConverters = function() {
|
|
|
|
this.serializers = {};
|
|
|
|
this.deserializers = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
TiddlerConverters.prototype.registerSerializer = function(extension,mimeType,serializer) {
|
|
|
|
this.serializers[extension] = serializer;
|
|
|
|
this.serializers[mimeType] = serializer;
|
|
|
|
};
|
|
|
|
|
|
|
|
TiddlerConverters.prototype.registerDeserializer = function(extension,mimeType,deserializer) {
|
|
|
|
this.deserializers[extension] = deserializer;
|
|
|
|
this.deserializers[mimeType] = deserializer;
|
|
|
|
};
|
|
|
|
|
|
|
|
TiddlerConverters.prototype.serialize = function(type,tiddler) {
|
|
|
|
var serializer = this.serializers[type];
|
|
|
|
if(serializer) {
|
|
|
|
return serializer(tiddler);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TiddlerConverters.prototype.deserialize = function(type,text,srcFields) {
|
|
|
|
var fields = {},
|
|
|
|
deserializer = this.deserializers[type],
|
|
|
|
t;
|
|
|
|
if(srcFields) {
|
|
|
|
for(t in srcFields) {
|
|
|
|
fields[t] = srcFields[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(deserializer) {
|
|
|
|
return deserializer(text,fields);
|
|
|
|
} else {
|
|
|
|
// Return a raw tiddler for unknown types
|
|
|
|
fields.text = text;
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.TiddlerConverters = TiddlerConverters;
|
2011-12-12 10:52:04 +00:00
|
|
|
|
|
|
|
})();
|