2013-03-24 12:22:21 +00:00
|
|
|
|
/*\
|
|
|
|
|
title: $:/plugins/tiddlywiki/filesystem/filesystemadaptor.js
|
|
|
|
|
type: application/javascript
|
|
|
|
|
module-type: syncadaptor
|
|
|
|
|
|
2014-08-14 10:12:25 +00:00
|
|
|
|
A sync adaptor module for synchronising with the local filesystem via node.js APIs
|
2013-03-24 12:22:21 +00:00
|
|
|
|
|
|
|
|
|
\*/
|
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
|
/*global $tw: false */
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
// Get a reference to the file system
|
2014-12-18 19:52:15 +00:00
|
|
|
|
var fs = $tw.node ? require("fs") : null,
|
|
|
|
|
path = $tw.node ? require("path") : null;
|
2013-10-11 21:10:10 +00:00
|
|
|
|
|
2014-08-14 10:12:25 +00:00
|
|
|
|
function FileSystemAdaptor(options) {
|
2013-10-11 22:43:51 +00:00
|
|
|
|
var self = this;
|
2014-08-14 10:12:25 +00:00
|
|
|
|
this.wiki = options.wiki;
|
2014-02-14 07:53:41 +00:00
|
|
|
|
this.logger = new $tw.utils.Logger("FileSystem");
|
2013-12-18 21:11:52 +00:00
|
|
|
|
// Create the <wiki>/tiddlers folder if it doesn't exist
|
2014-09-28 22:46:55 +00:00
|
|
|
|
$tw.utils.createDirectory($tw.boot.wikiTiddlersPath);
|
2013-03-24 12:22:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileSystemAdaptor.prototype.getTiddlerInfo = function(tiddler) {
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-25 10:43:46 +00:00
|
|
|
|
$tw.config.typeInfo = {
|
|
|
|
|
"text/vnd.tiddlywiki": {
|
|
|
|
|
fileType: "application/x-tiddler",
|
2013-03-25 12:12:53 +00:00
|
|
|
|
extension: ".tid"
|
2013-03-25 11:09:34 +00:00
|
|
|
|
},
|
2013-03-25 10:43:46 +00:00
|
|
|
|
"image/jpeg" : {
|
|
|
|
|
hasMetaFile: true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-25 11:09:34 +00:00
|
|
|
|
$tw.config.typeTemplates = {
|
|
|
|
|
"application/x-tiddler": "$:/core/templates/tid-tiddler"
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-25 10:43:46 +00:00
|
|
|
|
FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
|
|
|
|
|
// See if we've already got information about this file
|
|
|
|
|
var self = this,
|
|
|
|
|
title = tiddler.fields.title,
|
|
|
|
|
fileInfo = $tw.boot.files[title];
|
2013-03-25 12:11:34 +00:00
|
|
|
|
// Get information about how to save tiddlers of this type
|
|
|
|
|
var type = tiddler.fields.type || "text/vnd.tiddlywiki",
|
|
|
|
|
typeInfo = $tw.config.typeInfo[type];
|
|
|
|
|
if(!typeInfo) {
|
|
|
|
|
typeInfo = $tw.config.typeInfo["text/vnd.tiddlywiki"];
|
|
|
|
|
}
|
|
|
|
|
var extension = typeInfo.extension || "";
|
2013-03-25 10:43:46 +00:00
|
|
|
|
if(!fileInfo) {
|
|
|
|
|
// If not, we'll need to generate it
|
|
|
|
|
// Start by getting a list of the existing files in the directory
|
|
|
|
|
fs.readdir($tw.boot.wikiTiddlersPath,function(err,files) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
// Assemble the new fileInfo
|
|
|
|
|
fileInfo = {};
|
2014-03-31 16:17:36 +00:00
|
|
|
|
fileInfo.filepath = $tw.boot.wikiTiddlersPath + path.sep + self.generateTiddlerFilename(title,extension,files);
|
2013-03-25 12:11:34 +00:00
|
|
|
|
fileInfo.type = typeInfo.fileType || tiddler.fields.type;
|
2013-03-25 10:43:46 +00:00
|
|
|
|
fileInfo.hasMetaFile = typeInfo.hasMetaFile;
|
|
|
|
|
// Save the newly created fileInfo
|
|
|
|
|
$tw.boot.files[title] = fileInfo;
|
|
|
|
|
// Pass it to the callback
|
|
|
|
|
callback(null,fileInfo);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise just invoke the callback
|
|
|
|
|
callback(null,fileInfo);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-23 05:48:43 +00:00
|
|
|
|
/*
|
|
|
|
|
Transliterate string from cyrillic russian to latin
|
|
|
|
|
*/
|
|
|
|
|
var transliterate = function(cyrillyc) {
|
|
|
|
|
var a = {"Ё":"YO","Й":"I","Ц":"TS","У":"U","К":"K","Е":"E","Н":"N","Г":"G","Ш":"SH","Щ":"SCH","З":"Z","Х":"H","Ъ":"'","ё":"yo","й":"i","ц":"ts","у":"u","к":"k","е":"e","н":"n","г":"g","ш":"sh","щ":"sch","з":"z","х":"h","ъ":"'","Ф":"F","Ы":"I","В":"V","А":"a","П":"P","Р":"R","О":"O","Л":"L","Д":"D","Ж":"ZH","Э":"E","ф":"f","ы":"i","в":"v","а":"a","п":"p","р":"r","о":"o","л":"l","д":"d","ж":"zh","э":"e","Я":"Ya","Ч":"CH","С":"S","М":"M","И":"I","Т":"T","Ь":"'","Б":"B","Ю":"YU","я":"ya","ч":"ch","с":"s","м":"m","и":"i","т":"t","ь":"'","б":"b","ю":"yu"};
|
|
|
|
|
return cyrillyc.split("").map(function (char) {
|
|
|
|
|
return a[char] || char;
|
|
|
|
|
}).join("");
|
|
|
|
|
};
|
|
|
|
|
|
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the
generated .tid file. This is done by creating a tiddler
$:/config/FileSystemPaths which contains one or more filter expressions, one
per line. These filters are applied in turn to the tiddler to be saved, and
the first output produced is taken as a logical path relative to the wiki's
tiddlers directory. Any occurences of "/" in the logical path are replaced with
the platform's path separator, the extension ".tid" is appended, illegal
characters are replaced by "_" and the path is disambiguated (if necessary) in
order to arrive at the final tiddler file path. If none of the filters matches,
or the configuration tiddler does not exist, fall back to the previous file
naming scheme (i.e. replacing "/" by "_").
This implies we will now, for tiddlers matching the user-specified filters,
create directory trees below the tiddlers directory. In order to avoid
cluttering it with empty directory trees when renaming or removing tiddlers, any
directories that become empty by deleting a tiddler file are removed
(recursively).
Benefits of this configuration option include the ability to organize git
repositories of TiddlyWikis running on node.js, ability to replace characters
that cause trouble with particular operating systems or workflows (e.g. '$' on
unix) and the ability to replicate tiddler "paths" in the filesystem (by
including a filter like "[!has[draft.of]]") without forcing such a (potentially
problematic) change on all users.
2016-04-25 07:36:32 +00:00
|
|
|
|
/*
|
|
|
|
|
Given a list of filters, apply every one in turn to source, and return the first result of the first filter with non-empty result.
|
|
|
|
|
*/
|
|
|
|
|
FileSystemAdaptor.prototype.findFirstFilter = function(filters,source) {
|
|
|
|
|
var numFilters = filters.length;
|
|
|
|
|
for(var i=0; i<numFilters; i++) {
|
|
|
|
|
var result = this.wiki.filterTiddlers(filters[i],null,source);
|
|
|
|
|
if(result.length > 0) {
|
|
|
|
|
return result[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-03-25 10:43:46 +00:00
|
|
|
|
/*
|
|
|
|
|
Given a tiddler title and an array of existing filenames, generate a new legal filename for the title, case insensitively avoiding the array of existing filenames
|
|
|
|
|
*/
|
|
|
|
|
FileSystemAdaptor.prototype.generateTiddlerFilename = function(title,extension,existingFilenames) {
|
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the
generated .tid file. This is done by creating a tiddler
$:/config/FileSystemPaths which contains one or more filter expressions, one
per line. These filters are applied in turn to the tiddler to be saved, and
the first output produced is taken as a logical path relative to the wiki's
tiddlers directory. Any occurences of "/" in the logical path are replaced with
the platform's path separator, the extension ".tid" is appended, illegal
characters are replaced by "_" and the path is disambiguated (if necessary) in
order to arrive at the final tiddler file path. If none of the filters matches,
or the configuration tiddler does not exist, fall back to the previous file
naming scheme (i.e. replacing "/" by "_").
This implies we will now, for tiddlers matching the user-specified filters,
create directory trees below the tiddlers directory. In order to avoid
cluttering it with empty directory trees when renaming or removing tiddlers, any
directories that become empty by deleting a tiddler file are removed
(recursively).
Benefits of this configuration option include the ability to organize git
repositories of TiddlyWikis running on node.js, ability to replace characters
that cause trouble with particular operating systems or workflows (e.g. '$' on
unix) and the ability to replicate tiddler "paths" in the filesystem (by
including a filter like "[!has[draft.of]]") without forcing such a (potentially
problematic) change on all users.
2016-04-25 07:36:32 +00:00
|
|
|
|
var baseFilename;
|
|
|
|
|
// Check whether the user has configured a tiddler -> pathname mapping
|
|
|
|
|
var pathNameFilters = this.wiki.getTiddlerText("$:/config/FileSystemPaths");
|
|
|
|
|
if(pathNameFilters) {
|
|
|
|
|
var source = this.wiki.makeTiddlerIterator([title]);
|
|
|
|
|
var result = this.findFirstFilter(pathNameFilters.split("\n"),source);
|
|
|
|
|
if(result) {
|
|
|
|
|
// interpret "/" as path separator
|
|
|
|
|
baseFilename = result.replace(/\//g,path.sep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!baseFilename) {
|
|
|
|
|
// no mapping configured, or it did not match this tiddler
|
|
|
|
|
// in this case, we fall back to legacy behaviour
|
|
|
|
|
baseFilename = title.replace(/\//g,"_");
|
|
|
|
|
}
|
|
|
|
|
// Remove any of the characters that are illegal in Windows filenames
|
|
|
|
|
var baseFilename = transliterate(baseFilename.replace(/<|>|\:|\"|\\|\||\?|\*|\^|\s/g,"_"));
|
2013-03-25 10:43:46 +00:00
|
|
|
|
// Truncate the filename if it is too long
|
|
|
|
|
if(baseFilename.length > 200) {
|
2015-01-02 17:25:54 +00:00
|
|
|
|
baseFilename = baseFilename.substr(0,200);
|
2013-03-25 10:43:46 +00:00
|
|
|
|
}
|
|
|
|
|
// Start with the base filename plus the extension
|
2015-01-03 15:33:22 +00:00
|
|
|
|
var filename = baseFilename + extension,
|
2013-03-25 10:43:46 +00:00
|
|
|
|
count = 1;
|
2015-01-03 15:33:22 +00:00
|
|
|
|
// Add a discriminator if we're clashing with an existing filename while
|
|
|
|
|
// handling case-insensitive filesystems (NTFS, FAT/FAT32, etc.)
|
|
|
|
|
while(existingFilenames.some(function(value) {return value.toLocaleLowerCase() === filename.toLocaleLowerCase();})) {
|
2013-03-25 10:43:46 +00:00
|
|
|
|
filename = baseFilename + " " + (count++) + extension;
|
|
|
|
|
}
|
|
|
|
|
return filename;
|
|
|
|
|
};
|
|
|
|
|
|
2013-03-24 12:22:21 +00:00
|
|
|
|
/*
|
|
|
|
|
Save a tiddler and invoke the callback with (err,adaptorInfo,revision)
|
|
|
|
|
*/
|
|
|
|
|
FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) {
|
2013-10-11 21:10:10 +00:00
|
|
|
|
var self = this;
|
2013-03-25 10:43:46 +00:00
|
|
|
|
this.getTiddlerFileInfo(tiddler,function(err,fileInfo) {
|
2014-09-28 22:46:55 +00:00
|
|
|
|
var template, content, encoding,
|
|
|
|
|
_finish = function() {
|
|
|
|
|
callback(null, {}, 0);
|
|
|
|
|
};
|
2013-03-25 10:43:46 +00:00
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the
generated .tid file. This is done by creating a tiddler
$:/config/FileSystemPaths which contains one or more filter expressions, one
per line. These filters are applied in turn to the tiddler to be saved, and
the first output produced is taken as a logical path relative to the wiki's
tiddlers directory. Any occurences of "/" in the logical path are replaced with
the platform's path separator, the extension ".tid" is appended, illegal
characters are replaced by "_" and the path is disambiguated (if necessary) in
order to arrive at the final tiddler file path. If none of the filters matches,
or the configuration tiddler does not exist, fall back to the previous file
naming scheme (i.e. replacing "/" by "_").
This implies we will now, for tiddlers matching the user-specified filters,
create directory trees below the tiddlers directory. In order to avoid
cluttering it with empty directory trees when renaming or removing tiddlers, any
directories that become empty by deleting a tiddler file are removed
(recursively).
Benefits of this configuration option include the ability to organize git
repositories of TiddlyWikis running on node.js, ability to replace characters
that cause trouble with particular operating systems or workflows (e.g. '$' on
unix) and the ability to replicate tiddler "paths" in the filesystem (by
including a filter like "[!has[draft.of]]") without forcing such a (potentially
problematic) change on all users.
2016-04-25 07:36:32 +00:00
|
|
|
|
var error = $tw.utils.createDirectory(path.dirname(fileInfo.filepath));
|
|
|
|
|
if(error) {
|
|
|
|
|
return callback(error);
|
|
|
|
|
}
|
2015-09-17 18:43:10 +00:00
|
|
|
|
var typeInfo = $tw.config.contentTypeInfo[fileInfo.type];
|
|
|
|
|
if(fileInfo.hasMetaFile || typeInfo.encoding === "base64") {
|
2013-03-25 12:11:34 +00:00
|
|
|
|
// Save the tiddler as a separate body and meta file
|
|
|
|
|
fs.writeFile(fileInfo.filepath,tiddler.fields.text,{encoding: typeInfo.encoding},function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-08-14 10:12:25 +00:00
|
|
|
|
content = self.wiki.renderTiddler("text/plain","$:/core/templates/tiddler-metadata",{variables: {currentTiddler: tiddler.fields.title}});
|
2013-03-25 12:11:34 +00:00
|
|
|
|
fs.writeFile(fileInfo.filepath + ".meta",content,{encoding: "utf8"},function (err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-02-14 07:53:41 +00:00
|
|
|
|
self.logger.log("Saved file",fileInfo.filepath);
|
2013-10-11 21:10:10 +00:00
|
|
|
|
_finish();
|
2013-03-25 12:11:34 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Save the tiddler as a self contained templated file
|
|
|
|
|
template = $tw.config.typeTemplates[fileInfo.type];
|
2014-08-14 10:12:25 +00:00
|
|
|
|
content = self.wiki.renderTiddler("text/plain",template,{variables: {currentTiddler: tiddler.fields.title}});
|
2013-03-25 12:11:34 +00:00
|
|
|
|
fs.writeFile(fileInfo.filepath,content,{encoding: "utf8"},function (err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2014-02-14 07:53:41 +00:00
|
|
|
|
self.logger.log("Saved file",fileInfo.filepath);
|
2013-10-11 21:10:10 +00:00
|
|
|
|
_finish();
|
2013-03-25 12:11:34 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2013-03-25 10:43:46 +00:00
|
|
|
|
});
|
2013-03-24 12:22:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Load a tiddler and invoke the callback with (err,tiddlerFields)
|
2013-12-11 11:45:15 +00:00
|
|
|
|
|
|
|
|
|
We don't need to implement loading for the file system adaptor, because all the tiddler files will have been loaded during the boot process.
|
2013-03-24 12:22:21 +00:00
|
|
|
|
*/
|
|
|
|
|
FileSystemAdaptor.prototype.loadTiddler = function(title,callback) {
|
2013-12-11 11:45:15 +00:00
|
|
|
|
callback(null,null);
|
2013-03-24 12:22:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Delete a tiddler and invoke the callback with (err)
|
|
|
|
|
*/
|
2014-08-14 10:12:25 +00:00
|
|
|
|
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
|
2013-03-25 20:16:12 +00:00
|
|
|
|
var self = this,
|
|
|
|
|
fileInfo = $tw.boot.files[title];
|
|
|
|
|
// Only delete the tiddler if we have writable information for the file
|
|
|
|
|
if(fileInfo) {
|
2014-02-06 21:36:30 +00:00
|
|
|
|
// Delete the file
|
|
|
|
|
fs.unlink(fileInfo.filepath,function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
2013-10-11 21:10:10 +00:00
|
|
|
|
}
|
2014-02-14 07:53:41 +00:00
|
|
|
|
self.logger.log("Deleted file",fileInfo.filepath);
|
2014-02-06 21:36:30 +00:00
|
|
|
|
// Delete the metafile if present
|
|
|
|
|
if(fileInfo.hasMetaFile) {
|
|
|
|
|
fs.unlink(fileInfo.filepath + ".meta",function(err) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the
generated .tid file. This is done by creating a tiddler
$:/config/FileSystemPaths which contains one or more filter expressions, one
per line. These filters are applied in turn to the tiddler to be saved, and
the first output produced is taken as a logical path relative to the wiki's
tiddlers directory. Any occurences of "/" in the logical path are replaced with
the platform's path separator, the extension ".tid" is appended, illegal
characters are replaced by "_" and the path is disambiguated (if necessary) in
order to arrive at the final tiddler file path. If none of the filters matches,
or the configuration tiddler does not exist, fall back to the previous file
naming scheme (i.e. replacing "/" by "_").
This implies we will now, for tiddlers matching the user-specified filters,
create directory trees below the tiddlers directory. In order to avoid
cluttering it with empty directory trees when renaming or removing tiddlers, any
directories that become empty by deleting a tiddler file are removed
(recursively).
Benefits of this configuration option include the ability to organize git
repositories of TiddlyWikis running on node.js, ability to replace characters
that cause trouble with particular operating systems or workflows (e.g. '$' on
unix) and the ability to replicate tiddler "paths" in the filesystem (by
including a filter like "[!has[draft.of]]") without forcing such a (potentially
problematic) change on all users.
2016-04-25 07:36:32 +00:00
|
|
|
|
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
2014-02-06 21:36:30 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
Make tiddler file paths configurable (#2379)
When saving new tiddlers on node.js, allow the user to override the path of the
generated .tid file. This is done by creating a tiddler
$:/config/FileSystemPaths which contains one or more filter expressions, one
per line. These filters are applied in turn to the tiddler to be saved, and
the first output produced is taken as a logical path relative to the wiki's
tiddlers directory. Any occurences of "/" in the logical path are replaced with
the platform's path separator, the extension ".tid" is appended, illegal
characters are replaced by "_" and the path is disambiguated (if necessary) in
order to arrive at the final tiddler file path. If none of the filters matches,
or the configuration tiddler does not exist, fall back to the previous file
naming scheme (i.e. replacing "/" by "_").
This implies we will now, for tiddlers matching the user-specified filters,
create directory trees below the tiddlers directory. In order to avoid
cluttering it with empty directory trees when renaming or removing tiddlers, any
directories that become empty by deleting a tiddler file are removed
(recursively).
Benefits of this configuration option include the ability to organize git
repositories of TiddlyWikis running on node.js, ability to replace characters
that cause trouble with particular operating systems or workflows (e.g. '$' on
unix) and the ability to replicate tiddler "paths" in the filesystem (by
including a filter like "[!has[draft.of]]") without forcing such a (potentially
problematic) change on all users.
2016-04-25 07:36:32 +00:00
|
|
|
|
$tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback);
|
2014-02-06 21:36:30 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-25 20:16:12 +00:00
|
|
|
|
} else {
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
2013-03-24 12:22:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(fs) {
|
|
|
|
|
exports.adaptorClass = FileSystemAdaptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|