From 1ae428e323d475a5f244610fa963a814c0ab9e8b Mon Sep 17 00:00:00 2001 From: nome Date: Mon, 25 Apr 2016 09:36:32 +0200 Subject: [PATCH] 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. --- core/modules/utils/filesystem.js | 21 ++++++++++ .../tiddlers/concepts/TiddlyWikiFolders.tid | 2 +- .../Customizing_Tiddler_File_Naming.tid | 23 ++++++++++ .../filesystem/filesystemadaptor.js | 42 +++++++++++++++++-- 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 editions/tw5.com/tiddlers/nodejs/Customizing_Tiddler_File_Naming.tid diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 553209c6b..7ffe07ee7 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -160,4 +160,25 @@ exports.isDirectoryEmpty = function(dirPath) { return empty; }; +/* +Recursively delete a tree of empty directories +*/ +exports.deleteEmptyDirs = function(dirpath,callback) { + var self = this; + fs.readdir(dirpath,function(err,files) { + if(err) { + return callback(err); + } + if(files.length > 0) { + return callback(null); + } + fs.rmdir(dirpath,function(err) { + if(err) { + return callback(err); + } + self.deleteEmptyDirs(path.dirname(dirpath),callback); + }); + }); +}; + })(); diff --git a/editions/tw5.com/tiddlers/concepts/TiddlyWikiFolders.tid b/editions/tw5.com/tiddlers/concepts/TiddlyWikiFolders.tid index 2b846b0d5..813be0dbf 100644 --- a/editions/tw5.com/tiddlers/concepts/TiddlyWikiFolders.tid +++ b/editions/tw5.com/tiddlers/concepts/TiddlyWikiFolders.tid @@ -74,7 +74,7 @@ For example: !! Content of `tiddlers` folder -All the TiddlerFiles in the `tiddlers` folder are read into the wiki at startup. Sub-folders are scanned recursively for TiddlerFiles. +All the TiddlerFiles in the `tiddlers` folder are read into the wiki at startup. Sub-folders are scanned recursively for TiddlerFiles. Newly created tiddlers are stored in TiddlerFiles directly beneath the `tiddlers` folder, unless [[configured otherwise|Customizing Tiddler File Naming]]. Sub-folders within the `tiddlers` folder can also be given a `tiddlywiki.files` JSON file that overrides the default processing for that folder. The file format is illustrated with this example: diff --git a/editions/tw5.com/tiddlers/nodejs/Customizing_Tiddler_File_Naming.tid b/editions/tw5.com/tiddlers/nodejs/Customizing_Tiddler_File_Naming.tid new file mode 100644 index 000000000..fffec83bd --- /dev/null +++ b/editions/tw5.com/tiddlers/nodejs/Customizing_Tiddler_File_Naming.tid @@ -0,0 +1,23 @@ +created: 20160424181300000 +modified: 20160424181300000 +tags: [[TiddlyWiki on Node.js]] +title: Customizing Tiddler File Naming +type: text/vnd.tiddlywiki + +By default, a [[TiddlyWiki on Node.js]] instance using a [[wiki folder|TiddlyWikiFolders]] will create new tiddler files by using the sanitized and disambiguated title as filename. + +This can be customized by creating a tiddler [[$:/config/FileSystemPaths]] containing one or more [[filter expressions|Filter Syntax]], each on a line of its own. Newly created tiddlers are matched to each filter in turn, and the first output of the first filter to produce any output is taken as a logical path to be used for the tiddler file. Logical paths don't include the `.tid` extension, and they always use `/` as directory separator (when generating the physical path, this is replaced by the correct separator for the platform ~TiddlyWiki is running on). If none of the filters matches, the logical path is simply the title with all occurences of `/` replaced by `_` (for backwards compatibility). + +In both cases, the characters `<>:"\|?*^ ` are replaced by `_` in order to guarantee that the resulting path is legal on all supported platforms. + +!! Example + +``` +[is[system]removeprefix[$:/]addprefix[_system/]] +[tag[task][addprefix[mytasks/]] +[!has[draft.of]] +``` + +This will store newly created system tiddlers in `tiddlers/_system` (after stripping the `$:/` prefix), tiddlers tagged [[task]] in a subdirectory `tiddlers/mytasks`, and also create subdirectory structures for all other non-draft tiddlers. + +Thus, $:/config/FileSystemPaths itself will end up in `tiddlers/_system/config/FileSystemPaths.tid` or `tiddlers\_system\config\FileSystemPaths.tid`, depending on the platform. diff --git a/plugins/tiddlywiki/filesystem/filesystemadaptor.js b/plugins/tiddlywiki/filesystem/filesystemadaptor.js index ce06dc3ee..3f808e14f 100644 --- a/plugins/tiddlywiki/filesystem/filesystemadaptor.js +++ b/plugins/tiddlywiki/filesystem/filesystemadaptor.js @@ -87,12 +87,42 @@ Transliterate string from cyrillic russian to latin }).join(""); }; +/* +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 0) { + return result[0]; + } + } +}; + + /* 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) { - // First remove any of the characters that are illegal in Windows filenames - var baseFilename = transliterate(title.replace(/<|>|\:|\"|\/|\\|\||\?|\*|\^|\s/g,"_")); + 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,"_")); // Truncate the filename if it is too long if(baseFilename.length > 200) { baseFilename = baseFilename.substr(0,200); @@ -121,6 +151,10 @@ FileSystemAdaptor.prototype.saveTiddler = function(tiddler,callback) { if(err) { return callback(err); } + var error = $tw.utils.createDirectory(path.dirname(fileInfo.filepath)); + if(error) { + return callback(error); + } var typeInfo = $tw.config.contentTypeInfo[fileInfo.type]; if(fileInfo.hasMetaFile || typeInfo.encoding === "base64") { // Save the tiddler as a separate body and meta file @@ -181,10 +215,10 @@ FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) { if(err) { return callback(err); } - callback(null); + $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback); }); } else { - callback(null); + $tw.utils.deleteEmptyDirs(path.dirname(fileInfo.filepath),callback); } }); } else {