mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 12:07:19 +00:00
Add import command
Unlike load, allows manual control of the deserializer to be used
This commit is contained in:
parent
c6e4b7a123
commit
e951047461
24
core/language/en-GB/Help/import.tid
Normal file
24
core/language/en-GB/Help/import.tid
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
title: $:/language/Help/import
|
||||||
|
description: Import tiddlers from a file
|
||||||
|
|
||||||
|
Import tiddlers from 2.x.x TiddlyWiki files (`.html`), `.tiddler`, `.tid`, `.json` or other files. The deserializer must be explicitly specified, unlike the load command which infers the deserializer from the file extension.
|
||||||
|
|
||||||
|
```
|
||||||
|
--import <filepath> <deserializer> [<title>] [<encoding>]
|
||||||
|
```
|
||||||
|
|
||||||
|
The deserializers in the core include:
|
||||||
|
|
||||||
|
* application/javascript
|
||||||
|
* application/json
|
||||||
|
* application/x-tiddler
|
||||||
|
* application/x-tiddler-html-div
|
||||||
|
* application/x-tiddlers
|
||||||
|
* text/html
|
||||||
|
* text/plain
|
||||||
|
|
||||||
|
The title of the imported tiddler defaults to the filename.
|
||||||
|
|
||||||
|
The encoding defaults to "utf8", but can be "base64" for importing binary files.
|
||||||
|
|
||||||
|
Note that TiddlyWiki will not import an older version of an already loaded plugin.
|
48
core/modules/commands/import.js
Normal file
48
core/modules/commands/import.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*\
|
||||||
|
title: $:/core/modules/commands/import.js
|
||||||
|
type: application/javascript
|
||||||
|
module-type: command
|
||||||
|
|
||||||
|
Command to import tiddlers from a file
|
||||||
|
|
||||||
|
\*/
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
/*jslint node: true, browser: true */
|
||||||
|
/*global $tw: false */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
exports.info = {
|
||||||
|
name: "import",
|
||||||
|
synchronous: true
|
||||||
|
};
|
||||||
|
|
||||||
|
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 < 2) {
|
||||||
|
return "Missing parameters";
|
||||||
|
}
|
||||||
|
var filename = self.params[0],
|
||||||
|
deserializer = self.params[1],
|
||||||
|
title = self.params[2] || filename,
|
||||||
|
encoding = self.params[3] || "utf8",
|
||||||
|
text = fs.readFileSync(filename,encoding),
|
||||||
|
tiddlers = this.commander.wiki.deserializeTiddlers(null,text,{title: title},{deserializer: deserializer});
|
||||||
|
$tw.utils.each(tiddlers,function(tiddler) {
|
||||||
|
self.commander.wiki.importTiddler(new $tw.Tiddler(tiddler));
|
||||||
|
});
|
||||||
|
this.commander.log(tiddlers.length + " tiddler(s) imported");
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.Command = Command;
|
||||||
|
|
||||||
|
})();
|
8
editions/tw5.com/tiddlers/commands/ImportCommand.tid
Normal file
8
editions/tw5.com/tiddlers/commands/ImportCommand.tid
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
created: 20170712153850528
|
||||||
|
modified: 20170712153850528
|
||||||
|
tags: Commands
|
||||||
|
title: ImportCommand
|
||||||
|
type: text/vnd.tiddlywiki
|
||||||
|
caption: fetch
|
||||||
|
|
||||||
|
{{$:/language/Help/import}}
|
Loading…
Reference in New Issue
Block a user