mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-03-20 02:16:56 +00:00
Refactor edition-info handling
Introducing new filters that can be used under Node.js
This commit is contained in:
parent
3bbbda0819
commit
b768dc332b
@ -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;
|
||||
|
31
core/modules/filters/editiondescription.js
Normal file
31
core/modules/filters/editiondescription.js
Normal 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;
|
||||
};
|
||||
|
||||
})();
|
30
core/modules/filters/editions.js
Normal file
30
core/modules/filters/editions.js
Normal 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;
|
||||
};
|
||||
|
||||
})();
|
48
core/modules/utils/edition-info.js
Normal file
48
core/modules/utils/edition-info.js
Normal 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;
|
||||
};
|
||||
|
||||
})();
|
@ -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]].
|
8
editions/prerelease/tiddlers/FilterOperator editions.tid
Normal file
8
editions/prerelease/tiddlers/FilterOperator editions.tid
Normal 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.
|
Loading…
x
Reference in New Issue
Block a user