http://trac.tiddlywiki.org/changeset/10067
JeremyRuston 2009-07-22 10:41:30 +0000 (Wed, 22 Jul 2009) 72 [core & fileplugin] Moved path manipulation functions into twFile plugin --------------- U Trunk/core/jquery/plugins/jQuery.twFile.js U Trunk/core/js/Saving.js --------------- Modified: Trunk/core/jquery/plugins/jQuery.twFile.js =================================================================== --- Trunk/core/jquery/plugins/jQuery.twFile.js 2009-07-22 10:34:43 UTC (rev 10066) +++ Trunk/core/jquery/plugins/jQuery.twFile.js 2009-07-22 10:41:30 UTC (rev 10067) @@ -11,6 +11,7 @@ http://www.gnu.org/licenses/gpl.html */ + (function($) { if(!$.twFile) { $.twFile = {}; @@ -20,18 +21,65 @@ currentDriver: null, driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"], + // Loads the contents of a text file from the local file system + // filePath is the path to the file in these formats: + // x:\path\path\path\filename - PC local file + // \\server\share\path\path\path\filename - PC network file + // /path/path/path/filename - Mac/Unix local file + // returns the text of the file, or null if the operation cannot be performed or false if there was an error load: function(filePath) { return this.getDriver().loadFile(filePath); }, + // Saves a string to a text file on the local file system + // filePath is the path to the file in the format described above + // content is the string to save + // returns true if the file was saved successfully, or null if the operation cannot be performed or false if there was an error save: function(filePath,content) { return this.getDriver().saveFile(filePath,content); }, + // Copies a file on the local file system + // dest is the path to the destination file in the format described above + // source is the path to the source file in the format described above + // returns true if the file was copied successfully, or null if the operation cannot be performed or false if there was an error copy: function(dest,source) { if(this.getDriver().copyFile) return this.currentDriver.copyFile(dest,source); else - return false; + return null; }, + // Converts a local file path from the format returned by document.location into the format expected by this plugin + // url is the original URL of the file + // charSet is the optional character set identifier for the URL + // returns the equivalent local file path + convertUriToLocalPath: function (url,charSet) { + var originalPath = convertUriToUTF8(url,charSet); + // Remove any location or query part of the URL + originalPath = originalPath.split("#")[0].split("?")[0]; + // Convert file://localhost/ to file:/// + if(originalPath.indexOf("file://localhost/") == 0) + originalPath = "file://" + originalPath.substr(16); + // Convert to a native file format + //# "file:///x:/path/path/path..." - pc local file --> "x:\path\path\path..." + //# "file://///server/share/path/path/path..." - FireFox pc network file --> "\\server\share\path\path\path..." + //# "file:///path/path/path..." - mac/unix local file --> "/path/path/path..." + //# "file://server/share/path/path/path..." - pc network file --> "\\server\share\path\path\path..." + var localPath; + if(originalPath.charAt(9) == ":") // pc local file + localPath = unescape(originalPath.substr(8)).replace(new RegExp("/","g"),"\\"); + else if(originalPath.indexOf("file://///") == 0) // FireFox pc network file + localPath = "\\\\" + unescape(originalPath.substr(10)).replace(new RegExp("/","g"),"\\"); + else if(originalPath.indexOf("file:///") == 0) // mac/unix local file + localPath = unescape(originalPath.substr(7)); + else if(originalPath.indexOf("file:/") == 0) // mac/unix local file + localPath = unescape(originalPath.substr(5)); + else if(originalPath.indexOf("//") == 0) // pc network file + localPath = "\\\\" + unescape(originalPath.substr(7)).replace(new RegExp("/","g"),"\\"); + return localPath; + }, + + // Private functions + + // Returns a reference to the current driver getDriver: function() { if(this.currentDriver === null) { for(var t=0; t<this.driverList.length; t++) { @@ -145,7 +193,7 @@ var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath(filePath); if(!file.exists()) - return null; + return false; var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); inputStream.init(file,0x01,4,null); var converter = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream); @@ -262,7 +310,7 @@ function javaUrlToFilename(url) { var f = "//localhost"; if(url.indexOf(f) == 0) - return url.substring(f.length); + return url.substring(f.length); var i = url.indexOf(":"); return i > 0 ? url.substring(i-1) : url; } Modified: Trunk/core/js/Saving.js =================================================================== --- Trunk/core/js/Saving.js 2009-07-22 10:34:43 UTC (rev 10066) +++ Trunk/core/js/Saving.js 2009-07-22 10:41:30 UTC (rev 10067) @@ -188,34 +188,7 @@ function getLocalPath(origPath) { - var originalPath = convertUriToUTF8(origPath,config.options.txtFileSystemCharSet); - // Remove any location or query part of the URL - var argPos = originalPath.indexOf("?"); - if(argPos != -1) - originalPath = originalPath.substr(0,argPos); - var hashPos = originalPath.indexOf("#"); - if(hashPos != -1) - originalPath = originalPath.substr(0,hashPos); - // Convert file://localhost/ to file:/// - if(originalPath.indexOf("file://localhost/") == 0) - originalPath = "file://" + originalPath.substr(16); - // Convert to a native file format - //# "file:///x:/path/path/path..." - pc local file --> "x:\path\path\path..." - //# "file://///server/share/path/path/path..." - FireFox pc network file --> "\\server\share\path\path\path..." - //# "file:///path/path/path..." - mac/unix local file --> "/path/path/path..." - //# "file://server/share/path/path/path..." - pc network file --> "\\server\share\path\path\path..." - var localPath; - if(originalPath.charAt(9) == ":") // pc local file - localPath = unescape(originalPath.substr(8)).replace(new RegExp("/","g"),"\\"); - else if(originalPath.indexOf("file://///") == 0) // FireFox pc network file - localPath = "\\\\" + unescape(originalPath.substr(10)).replace(new RegExp("/","g"),"\\"); - else if(originalPath.indexOf("file:///") == 0) // mac/unix local file - localPath = unescape(originalPath.substr(7)); - else if(originalPath.indexOf("file:/") == 0) // mac/unix local file - localPath = unescape(originalPath.substr(5)); - else // pc network file - localPath = "\\\\" + unescape(originalPath.substr(7)).replace(new RegExp("/","g"),"\\"); - return localPath; + return jQuery.twFile.convertUriToLocalPath(origPath,config.options.txtFileSystemCharSet); } function getBackupPath(localPath,title,extension) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TiddlyWikiDev" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/TiddlyWikiDev?hl=en -~----------~----~----~----~------~----~------~--~---
