1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-11-14 06:17:20 +00:00

Add "raw-file(s)" options to "fetch" command

This commit is contained in:
Jermolene
2017-07-03 16:52:10 +01:00
parent 4db950cc45
commit c179dc93cb
2 changed files with 60 additions and 27 deletions

View File

@@ -27,24 +27,36 @@ Command.prototype.execute = function() {
if(this.params.length < 2) {
return "Missing subcommand and url";
}
var subcommand = this.params[0],
url = this.params[1],
importFilter = this.params[2] || "[all[tiddlers]]",
transformFilter = this.params[3] || "";
switch(subcommand) {
switch(this.params[0]) {
case "raw-file":
return this.fetchFiles({
raw: true,
url: this.params[1],
transformFilter: this.params[2] || "",
callback: this.callback
});
break;
case "file":
return this.fetchFiles({
url: url,
importFilter: importFilter,
transformFilter: transformFilter,
url: this.params[1],
importFilter: this.params[2],
transformFilter: this.params[3] || "",
callback: this.callback
});
break;
case "raw-files":
return this.fetchFiles({
raw: true,
urlFilter: this.params[1],
transformFilter: this.params[2] || "",
callback: this.callback
});
break;
case "files":
return this.fetchFiles({
urlFilter: url,
importFilter: importFilter,
transformFilter: transformFilter,
urlFilter: this.params[1],
importFilter: this.params[2],
transformFilter: this.params[3] || "",
callback: this.callback
});
break;
@@ -85,16 +97,16 @@ Command.prototype.fetchFile = function(url,options,callback) {
lib = url.substr(0,8) === "https://" ? require("https") : require("http");
lib.get(url).on("response",function(response) {
var type = (response.headers["content-type"] || "").split(";")[0],
body = "";
data = [];
self.commander.write("Reading " + url + ": ");
response.on("data",function(chunk) {
body += chunk;
data.push(chunk);
self.commander.write(".");
});
response.on("end",function() {
self.commander.write("\n");
if(response.statusCode === 200) {
self.processBody(body,type,options);
self.processBody(Buffer.concat(data),type,options,url);
callback(null);
} else {
callback("Error " + response.statusCode + " retrieving " + url)
@@ -108,16 +120,27 @@ Command.prototype.fetchFile = function(url,options,callback) {
return null;
};
Command.prototype.processBody = function(body,type,options) {
// Deserialise the HTML file and put the tiddlers in their own wiki
var self = this,
incomingWiki = new $tw.Wiki(),
tiddlers = this.commander.wiki.deserializeTiddlers(type || "text/html",body,{});
$tw.utils.each(tiddlers,function(tiddler) {
incomingWiki.addTiddler(new $tw.Tiddler(tiddler));
});
Command.prototype.processBody = function(body,type,options,url) {
var self = this;
// Collect the tiddlers in a wiki
var incomingWiki = new $tw.Wiki();
if(options.raw) {
var typeInfo = type ? $tw.config.contentTypeInfo[type] : null,
encoding = typeInfo ? typeInfo.encoding : "utf8";
incomingWiki.addTiddler(new $tw.Tiddler({
title: url,
type: type,
text: body.toString(encoding)
}));
} else {
// Deserialise the file to extract the tiddlers
var tiddlers = this.commander.wiki.deserializeTiddlers(type || "text/html",body.toString("utf8"),{});
$tw.utils.each(tiddlers,function(tiddler) {
incomingWiki.addTiddler(new $tw.Tiddler(tiddler));
});
}
// Filter the tiddlers to select the ones we want
var filteredTitles = incomingWiki.filterTiddlers(options.importFilter);
var filteredTitles = incomingWiki.filterTiddlers(options.importFilter || "[all[tiddlers]]");
// Import the selected tiddlers
var count = 0;
incomingWiki.each(function(tiddler,title) {