From 362df18a19c174d448394cbe127ccc224a2e09a5 Mon Sep 17 00:00:00 2001 From: James Welford Anderson Date: Wed, 9 Apr 2014 06:20:51 +0900 Subject: [PATCH 1/2] make deserializing a bit more robust load.js references the encoding set in boot.js when loading a file. boot.js can now register file type with different deserialization from their actual type --- boot/boot.js | 40 ++++++++++++++++++++++++++--------- core/modules/commands/load.js | 5 +++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index e4a814d8f..47c382a3b 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -376,11 +376,25 @@ $tw.utils.checkVersions = function(versionStringA,versionStringB) { /* Register file type information -flags: "image" for image types +options: {flags: flags,deserializertype: deserializertype} + flags:"image" for image types + deserializertype: defaults to type if not specified */ -$tw.utils.registerFileType = function(type,encoding,extension,flags) { - $tw.config.fileExtensionInfo[extension] = {type: type}; - $tw.config.contentTypeInfo[type] = {encoding: encoding, extension: extension, flags: flags || []}; +$tw.utils.registerFileType = function(type,encoding,extension,options) { + options = options || {}; + $tw.config.fileExtensionInfo[extension] = {type: type}; + $tw.config.contentTypeInfo[type] = {encoding: encoding, extension: extension, flags: options.flags || [], deserializertype: options.deserializertype || type}; +}; + +/* +Given an extension, get the correct encoding for that file. +defaults to utf8 +*/ +$tw.utils.getTypeEncoding = function(ext) { + var extensionInfo = $tw.config.fileExtensionInfo[ext], + type = extensionInfo ? extensionInfo.type : null, + typeInfo = type ? $tw.config.contentTypeInfo[type] : null; + return typeInfo ? typeInfo.encoding : "utf8"; }; /* @@ -1042,6 +1056,11 @@ $tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) { type = $tw.config.fileExtensionInfo[type].type; deserializer = $tw.Wiki.tiddlerDeserializerModules[type]; } + if(!deserializer && $tw.config.contentTypeInfo[type]) { + // see if this type has a different deserializer registered with it + type = $tw.config.contentTypeInfo[type].deserializertype; + deserializer = $tw.Wiki.tiddlerDeserializerModules[type]; + } if(!deserializer) { // If we still don't have a deserializer, treat it as plain text deserializer = $tw.Wiki.tiddlerDeserializerModules["text/plain"]; @@ -1600,14 +1619,15 @@ $tw.boot.startup = function(options) { $tw.utils.registerFileType("text/plain","utf8",".txt"); $tw.utils.registerFileType("text/css","utf8",".css"); $tw.utils.registerFileType("text/html","utf8",".html"); + $tw.utils.registerFileType("application/hta","utf16le",".hta",{deserializertype:"text/html"}); $tw.utils.registerFileType("application/javascript","utf8",".js"); $tw.utils.registerFileType("application/json","utf8",".json"); - $tw.utils.registerFileType("application/pdf","base64",".pdf",["image"]); - $tw.utils.registerFileType("image/jpeg","base64",".jpg",["image"]); - $tw.utils.registerFileType("image/png","base64",".png",["image"]); - $tw.utils.registerFileType("image/gif","base64",".gif",["image"]); - $tw.utils.registerFileType("image/svg+xml","utf8",".svg",["image"]); - $tw.utils.registerFileType("image/x-icon","base64",".ico",["image"]); + $tw.utils.registerFileType("application/pdf","base64",".pdf",{flags:["image"]}); + $tw.utils.registerFileType("image/jpeg","base64",".jpg",{flags:["image"]}); + $tw.utils.registerFileType("image/png","base64",".png",{flags:["image"]}); + $tw.utils.registerFileType("image/gif","base64",".gif",{flags:["image"]}); + $tw.utils.registerFileType("image/svg+xml","utf8",".svg",{flags:["image"]}); + $tw.utils.registerFileType("image/x-icon","base64",".ico",{flags:["image"]}); $tw.utils.registerFileType("application/font-woff","base64",".woff"); // Create the wiki store for the app $tw.wiki = new $tw.Wiki(); diff --git a/core/modules/commands/load.js b/core/modules/commands/load.js index bc9cad0c0..95cee01be 100644 --- a/core/modules/commands/load.js +++ b/core/modules/commands/load.js @@ -30,8 +30,9 @@ Command.prototype.execute = function() { if(this.params.length < 1) { return "Missing filename"; } - fs.readFile(this.params[0],"utf8",function(err,data) { - if(err) { + var ext = path.extname(self.params[0]); + fs.readFile(this.params[0],$tw.utils.getTypeEncoding(ext),function(err,data) { + if (err) { self.callback(err); } else { var fields = {title: self.params[0]}, From 0f07977930e87ee159f74b3ba8be11ab5639b9c5 Mon Sep 17 00:00:00 2001 From: James Welford Anderson Date: Wed, 9 Apr 2014 06:26:01 +0900 Subject: [PATCH 2/2] deserializertype -> deserializerType --- boot/boot.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index 47c382a3b..1556ef7f0 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -376,14 +376,14 @@ $tw.utils.checkVersions = function(versionStringA,versionStringB) { /* Register file type information -options: {flags: flags,deserializertype: deserializertype} +options: {flags: flags,deserializerType: deserializerType} flags:"image" for image types - deserializertype: defaults to type if not specified + deserializerType: defaults to type if not specified */ $tw.utils.registerFileType = function(type,encoding,extension,options) { options = options || {}; $tw.config.fileExtensionInfo[extension] = {type: type}; - $tw.config.contentTypeInfo[type] = {encoding: encoding, extension: extension, flags: options.flags || [], deserializertype: options.deserializertype || type}; + $tw.config.contentTypeInfo[type] = {encoding: encoding, extension: extension, flags: options.flags || [], deserializerType: options.deserializerType || type}; }; /* @@ -1058,7 +1058,7 @@ $tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) { } if(!deserializer && $tw.config.contentTypeInfo[type]) { // see if this type has a different deserializer registered with it - type = $tw.config.contentTypeInfo[type].deserializertype; + type = $tw.config.contentTypeInfo[type].deserializerType; deserializer = $tw.Wiki.tiddlerDeserializerModules[type]; } if(!deserializer) { @@ -1619,7 +1619,7 @@ $tw.boot.startup = function(options) { $tw.utils.registerFileType("text/plain","utf8",".txt"); $tw.utils.registerFileType("text/css","utf8",".css"); $tw.utils.registerFileType("text/html","utf8",".html"); - $tw.utils.registerFileType("application/hta","utf16le",".hta",{deserializertype:"text/html"}); + $tw.utils.registerFileType("application/hta","utf16le",".hta",{deserializerType:"text/html"}); $tw.utils.registerFileType("application/javascript","utf8",".js"); $tw.utils.registerFileType("application/json","utf8",".json"); $tw.utils.registerFileType("application/pdf","base64",".pdf",{flags:["image"]});