1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 10:29:54 +00:00

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.
This commit is contained in:
nome 2016-04-25 09:36:32 +02:00 committed by Jeremy Ruston
parent d1f2c399ce
commit 1ae428e323
4 changed files with 83 additions and 5 deletions

View File

@ -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);
});
});
};
})();

View File

@ -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:

View File

@ -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.

View File

@ -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<numFilters; i++) {
var result = this.wiki.filterTiddlers(filters[i],null,source);
if(result.length > 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 {