1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-24 22:33:16 +00:00

Refactor edition-info handling

Introducing new filters that can be used under Node.js
This commit is contained in:
Jermolene 2015-01-11 15:00:54 +00:00
parent 3bbbda0819
commit b768dc332b
6 changed files with 131 additions and 27 deletions

View File

@ -23,35 +23,12 @@ var Command = function(params,commander) {
};
Command.prototype.execute = function() {
var fs = require("fs"),
path = require("path"),
self = this;
// Enumerate the edition paths
var editionPaths = $tw.getLibraryItemSearchPaths($tw.config.editionsPath,$tw.config.editionsEnvVar),
editions = {};
for(var editionIndex=0; editionIndex<editionPaths.length; editionIndex++) {
var editionPath = editionPaths[editionIndex];
// Enumerate the folders
var entries = fs.readdirSync(editionPath);
for(var entryIndex=0; entryIndex<entries.length; entryIndex++) {
var entry = entries[entryIndex];
// Check if directories have a valid tiddlywiki.info
if(!editions[entry] && $tw.utils.isDirectory(path.resolve(editionPath,entry))) {
var info;
try {
info = JSON.parse(fs.readFileSync(path.resolve(editionPath,entry,"tiddlywiki.info"),"utf8"));
} catch(ex) {
}
if(info) {
editions[entry] = info.description || "";
}
}
}
}
var self = this;
// Output the list
this.commander.streams.output.write("Available editions:\n\n");
$tw.utils.each(editions,function(description,name) {
self.commander.streams.output.write(" " + name + ": " + description + "\n");
var editionInfo = $tw.utils.getEditionInfo();
$tw.utils.each(editionInfo,function(info,name) {
self.commander.streams.output.write(" " + name + ": " + info.description + "\n");
});
this.commander.streams.output.write("\n");
return null;

View File

@ -0,0 +1,31 @@
/*\
title: $:/core/modules/filters/editiondescription.js
type: application/javascript
module-type: filteroperator
Filter operator for returning the descriptions of the specified edition names
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.editiondescription = function(source,operator,options) {
var results = [],
editionInfo = $tw.utils.getEditionInfo();
if(editionInfo) {
source(function(tiddler,title) {
if($tw.utils.hop(editionInfo,title)) {
results.push(editionInfo[title].description || "");
}
});
}
return results;
};
})();

View File

@ -0,0 +1,30 @@
/*\
title: $:/core/modules/filters/editions.js
type: application/javascript
module-type: filteroperator
Filter operator for returning the names of the available editions in this wiki
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.editions = function(source,operator,options) {
var results = [],
editionInfo = $tw.utils.getEditionInfo();
if(editionInfo) {
$tw.utils.each(editionInfo,function(info,name) {
results.push(name);
});
}
results.sort();
return results;
};
})();

View File

@ -0,0 +1,48 @@
/*\
title: $:/core/modules/utils/edition-info.js
type: application/javascript
module-type: utils-node
Information about the available editions
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var fs = require("fs"),
path = require("path");
var editionInfo;
exports.getEditionInfo = function() {
if(!editionInfo) {
// Enumerate the edition paths
var editionPaths = $tw.getLibraryItemSearchPaths($tw.config.editionsPath,$tw.config.editionsEnvVar);
editionInfo = {};
for(var editionIndex=0; editionIndex<editionPaths.length; editionIndex++) {
var editionPath = editionPaths[editionIndex];
// Enumerate the folders
var entries = fs.readdirSync(editionPath);
for(var entryIndex=0; entryIndex<entries.length; entryIndex++) {
var entry = entries[entryIndex];
// Check if directories have a valid tiddlywiki.info
if(!editionInfo[entry] && $tw.utils.isDirectory(path.resolve(editionPath,entry))) {
var info;
try {
info = JSON.parse(fs.readFileSync(path.resolve(editionPath,entry,"tiddlywiki.info"),"utf8"));
} catch(ex) {
}
if(info) {
editionInfo[entry] = info;
}
}
}
}
}
return editionInfo;
};
})();

View File

@ -0,0 +1,10 @@
created: 20150111145738451
modified: 20150111145738451
tags: Filters
caption: editiondescription
title: FilterOperator: editiondescription
type: text/vnd.tiddlywiki
The ''editiondescription'' filter operator returns a description string for each edition name in the current results. It is only available under Node.js.
Also see [[FilterOperator: editions]].

View File

@ -0,0 +1,8 @@
created: 20150111145738451
modified: 20150111145738451
tags: Filters
caption: editions
title: FilterOperator: editions
type: text/vnd.tiddlywiki
The ''editions'' filter operator returns the names of all the available editions. It is only available under Node.js.