From 28aef5185580dabf0f0b54cf4931dd9ceb066966 Mon Sep 17 00:00:00 2001 From: WhiteFall <32425955+Zacharia2@users.noreply.github.com> Date: Thu, 29 Jun 2023 22:40:35 +0800 Subject: [PATCH] Add explodePlugins=yes option to savewikifolder command (#7490) * add option explodeplugins refactor Maintain Parameter Compatibility change let to var Refactoring the code add conditional judgment use === to avoid type-casting errors refactor use [=] delete redundant code backward compatibility support easier to use improve code add documentation * Update Notes * Improve the code --- core/language/en-GB/Help/savewikifolder.tid | 13 +++++++- core/modules/commands/savewikifolder.js | 34 ++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/core/language/en-GB/Help/savewikifolder.tid b/core/language/en-GB/Help/savewikifolder.tid index bda1d19a3..6cd6c30c4 100644 --- a/core/language/en-GB/Help/savewikifolder.tid +++ b/core/language/en-GB/Help/savewikifolder.tid @@ -4,7 +4,7 @@ description: Saves a wiki to a new wiki folder <<.from-version "5.1.20">> Saves the current wiki as a wiki folder, including tiddlers, plugins and configuration: ``` ---savewikifolder [] +--savewikifolder [] [ [=] ]* ``` * The target wiki folder must be empty or non-existent @@ -12,8 +12,19 @@ description: Saves a wiki to a new wiki folder * Plugins from the official plugin library are replaced with references to those plugins in the `tiddlywiki.info` file * Custom plugins are unpacked into their own folder +The following options are supported: + +* ''filter'': a filter expression that defines the tiddlers to include in the output. +* ''explodePlugins'': set to "no" to save plugins to the tiddlers directory of the target wiki folder, to suppress breaking plugins into their constituent shadow tiddlers (default is "yes"). + A common usage is to convert a TiddlyWiki HTML file into a wiki folder: ``` tiddlywiki --load ./mywiki.html --savewikifolder ./mywikifolder ``` + +Save the plugin to the tiddlers directory of the target wiki folder: + +``` +tiddlywiki --load ./mywiki.html --savewikifolder ./mywikifolder explodePlugins=no +``` \ No newline at end of file diff --git a/core/modules/commands/savewikifolder.js b/core/modules/commands/savewikifolder.js index 48e9be56a..c0fccd775 100644 --- a/core/modules/commands/savewikifolder.js +++ b/core/modules/commands/savewikifolder.js @@ -5,7 +5,14 @@ module-type: command Command to save the current wiki as a wiki folder ---savewikifolder [] +--savewikifolder [ [=] ]* + +The following options are supported: + +* ''filter'': a filter expression defining the tiddlers to be included in the output +* ''explodePlugins'': set to "no" to suppress exploding plugins into their constituent shadow tiddlers (defaults to "yes") + +Supports backward compatibility with --savewikifolder [] [ [=] ]* \*/ (function(){ @@ -35,14 +42,28 @@ Command.prototype.execute = function() { if(this.params.length < 1) { return "Missing wiki folder path"; } - var wikifoldermaker = new WikiFolderMaker(this.params[0],this.params[1],this.commander); + var regFilter = /^[a-zA-Z0-9\.\-_]+=/g, // dynamic parameters + namedParames, + tiddlerFilter, + options = {}; + if (regFilter.test(this.params[1])) { + namedParames = this.commander.extractNamedParameters(this.params.slice(1)); + tiddlerFilter = namedParames.filter || "[all[tiddlers]]"; + } else { + namedParames = this.commander.extractNamedParameters(this.params.slice(2)); + tiddlerFilter = this.params[1]; + } + tiddlerFilter = tiddlerFilter || "[all[tiddlers]]"; + options.explodePlugins = namedParames.explodePlugins || "yes"; + var wikifoldermaker = new WikiFolderMaker(this.params[0],tiddlerFilter,this.commander,options); return wikifoldermaker.save(); }; -function WikiFolderMaker(wikiFolderPath,wikiFilter,commander) { +function WikiFolderMaker(wikiFolderPath,wikiFilter,commander,options) { this.wikiFolderPath = wikiFolderPath; - this.wikiFilter = wikiFilter || "[all[tiddlers]]"; + this.wikiFilter = wikiFilter; this.commander = commander; + this.explodePlugins = options.explodePlugins; this.wiki = commander.wiki; this.savedPaths = []; // So that we can detect filename clashes } @@ -93,10 +114,13 @@ WikiFolderMaker.prototype.save = function() { self.log("Adding built-in plugin: " + libraryDetails.name); newWikiInfo[libraryDetails.type] = newWikiInfo[libraryDetails.type] || []; $tw.utils.pushTop(newWikiInfo[libraryDetails.type],libraryDetails.name); - } else { + } else if(self.explodePlugins !== "no") { // A custom plugin self.log("Processing custom plugin: " + title); self.saveCustomPlugin(tiddler); + } else if(self.explodePlugins === "no") { + self.log("Processing custom plugin to tiddlders folder: " + title); + self.saveTiddler("tiddlers", tiddler); } } else { // Ordinary tiddler