mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
Text-slicer: better support for specifying the output title
Can be specified directly, or taken from a field of the tiddler being sliced, falling back to a default.
This commit is contained in:
parent
cd0a0aa1fb
commit
7498989ebb
@ -16,7 +16,7 @@ var widget = require("$:/core/modules/widgets/widget.js");
|
||||
|
||||
exports.info = {
|
||||
name: "slice",
|
||||
synchronous: true
|
||||
synchronous: false
|
||||
};
|
||||
|
||||
var Command = function(params,commander,callback) {
|
||||
@ -31,11 +31,15 @@ Command.prototype.execute = function() {
|
||||
}
|
||||
var self = this,
|
||||
wiki = this.commander.wiki,
|
||||
tiddlerTitle = this.params[0],
|
||||
slicer = new $tw.Slicer(wiki,tiddlerTitle);
|
||||
slicer.sliceTiddler(tiddlerTitle)
|
||||
sourceTitle = this.params[0],
|
||||
destTitle = this.params[1],
|
||||
slicer = new $tw.Slicer(wiki,sourceTitle,{
|
||||
destTitle: destTitle
|
||||
});
|
||||
slicer.sliceTiddler()
|
||||
slicer.outputTiddlers();
|
||||
slicer.destroy();
|
||||
$tw.utils.nextTick(this.callback);
|
||||
return null;
|
||||
};
|
||||
|
||||
|
@ -16,9 +16,12 @@ var DOMParser = require("$:/plugins/tiddlywiki/xmldom/dom-parser").DOMParser;
|
||||
|
||||
var SLICER_OUTPUT_TITLE = "$:/TextSlicer";
|
||||
|
||||
function Slicer(wiki,sourceTitle) {
|
||||
function Slicer(wiki,sourceTitle,options) {
|
||||
options = options || {};
|
||||
this.wiki = wiki;
|
||||
this.sourceTitle = sourceTitle;
|
||||
this.sourceTiddler = wiki.getTiddler(this.sourceTitle);
|
||||
this.destTitle = options.destTitle || this.sourceTiddler.fields["doc-split-to"] || ("Sliced up " + this.sourceTitle);
|
||||
this.iframe = null; // Reference to iframe used for HTML parsing
|
||||
this.stopWordList = "the and a of on i".split(" ");
|
||||
this.tiddlers = {};
|
||||
@ -126,11 +129,10 @@ Slicer.prototype.getSourceWikiDocument = function(tiddler) {
|
||||
};
|
||||
|
||||
Slicer.prototype.getSourceDocument = function() {
|
||||
var tiddler = $tw.wiki.getTiddler(this.sourceTitle);
|
||||
if(tiddler.fields.type === "text/html") {
|
||||
return this.getSourceHtmlDocument(tiddler);
|
||||
if(this.sourceTiddler.fields.type === "text/html") {
|
||||
return this.getSourceHtmlDocument(this.sourceTiddler);
|
||||
} else {
|
||||
return this.getSourceWikiDocument(tiddler);
|
||||
return this.getSourceWikiDocument(this.sourceTiddler);
|
||||
}
|
||||
};
|
||||
|
||||
@ -198,18 +200,12 @@ Slicer.prototype.processNode = function(domNode) {
|
||||
};
|
||||
|
||||
// Slice a tiddler into individual tiddlers
|
||||
Slicer.prototype.sliceTiddler = function(title) {
|
||||
var sourceTiddler = $tw.wiki.getTiddler(this.sourceTitle),
|
||||
sliceTitle,sliceTiddler = {};
|
||||
if(sourceTiddler) {
|
||||
sliceTiddler = $tw.utils.extend({},sourceTiddler.fields);
|
||||
}
|
||||
if(sliceTiddler["doc-santovia-id"]) {
|
||||
sliceTiddler.title = sliceTiddler["doc-santovia-id"];
|
||||
delete sliceTiddler["doc-santovia-id"];
|
||||
} else {
|
||||
sliceTiddler.title = "Sliced up " + title;
|
||||
Slicer.prototype.sliceTiddler = function() {
|
||||
var sliceTitle,sliceTiddler = {};
|
||||
if(this.sourceTiddler) {
|
||||
sliceTiddler = $tw.utils.extend({},this.sourceTiddler.fields);
|
||||
}
|
||||
sliceTiddler.title = this.destTitle;
|
||||
sliceTiddler.text = "Document sliced at " + (new Date());
|
||||
sliceTiddler.type = "text/vnd.tiddlywiki";
|
||||
sliceTiddler.tags = [];
|
||||
@ -217,7 +213,7 @@ Slicer.prototype.sliceTiddler = function(title) {
|
||||
sliceTiddler["toc-type"] = "document";
|
||||
var domNode = this.getSourceDocument();
|
||||
this.parentStack.push({type: "h0", title: this.addTiddler(sliceTiddler)});
|
||||
this.currentTiddler = title;
|
||||
this.currentTiddler = sliceTiddler.title;
|
||||
this.containerStack.push(sliceTiddler.title);
|
||||
this.processNodeList(domNode.childNodes);
|
||||
this.containerStack.pop();
|
||||
|
@ -21,7 +21,9 @@ exports.synchronous = true;
|
||||
// Install the root widget event handlers
|
||||
exports.startup = function() {
|
||||
$tw.rootWidget.addEventListener("tm-slice-tiddler",function(event) {
|
||||
var slicer = new $tw.Slicer($tw.wiki,event.param);
|
||||
var slicer = new $tw.Slicer($tw.wiki,event.param,{
|
||||
destTitle: event.paramObject.destTitle
|
||||
});
|
||||
slicer.sliceTiddler(event.param)
|
||||
slicer.outputTiddlers();
|
||||
slicer.destroy();
|
||||
|
Loading…
Reference in New Issue
Block a user