-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadnew.js
More file actions
executable file
·69 lines (60 loc) · 2.11 KB
/
Copy pathloadnew.js
File metadata and controls
executable file
·69 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*\
title: $:/plugins/malgam/loadnew/loadnew.js
type: application/javascript
module-type: command
Command to load new or modified tiddlers from a file
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "loadnew",
synchronous: false
};
var Command = function(params,commander,callback) {
this.params = params;
this.commander = commander;
this.callback = callback;
};
Command.prototype.execute = function() {
var self = this,
fs = require("fs"),
path = require("path");
if(this.params.length < 1) {
return "Missing filename";
}
var ext = path.extname(self.params[0]);
fs.readFile(this.params[0],$tw.utils.getTypeEncoding(ext),function(err,data) {
if (err) {
self.callback(err);
} else {
var fields = {title: self.params[0]},
type = path.extname(self.params[0]);
var tiddlers = self.commander.wiki.deserializeTiddlers(type,data,fields);
if(!tiddlers) {
self.callback("No tiddlers found in file \"" + self.params[0] + "\"");
} else {
for(var t=0; t<tiddlers.length; t++) {
var tid = new $tw.Tiddler(tiddlers[t]);
var tidDate = tid.hasField("modified")
? $tw.utils.formatDateString(tid.fields.modified,
'YYYY0MM0DD0hh0mm0ss')
: "";
var existingTid = self.commander.wiki.getTiddler(tid.fields.title);
var existingTidDate = existingTid && existingTid.hasField("modified")
? $tw.utils.formatDateString(existingTid.fields.modified,
'YYYY0MM0DD0hh0mm0ss')
: "";
if(!existingTid || tidDate > existingTidDate) {
$tw.wiki.addTiddler(tid);
}
}
self.callback(null);
}
}
});
return null;
};
exports.Command = Command;
})();