1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Add "save" and "render" commands

A first pass at fixing #2973 - comments are welcome. If everybody is
happy, the next step is to deprecate the existing
rendertiddler(s)/savetiddler(s) commands and replace them in all
examples and tiddlywiki.info files with the new equivalents.
This commit is contained in:
Jermolene 2017-09-12 16:04:07 +01:00
parent 378e00aee8
commit 35a7a66b01
8 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,34 @@
title: $:/language/Help/render
description: Renders individual tiddlers to files
Render individual tiddlers and save the results to the specified files.
Optionally the title of a template tiddler can be specified, in which case the template tiddler is rendered with the "currentTiddler" variable set to the title of the tiddler that is being rendered.
A name and value for an additional variable may optionally also be specified.
```
--render <tiddler-filter> [<filename-filter>] [<render-type>] [<template>] [<name>] [<value>]
```
* ''tiddler-filter'': A filter identifying the tiddler(s) to be rendered
* ''filename-filter'': Optional filter transforming tiddler titles into pathnames. If omitted, defaults to `[is[tiddler]addsuffix[.html]]`, which uses the unchanged tiddler title as the filename
* ''template'': Optional template through which each tiddler is rendered
* ''render-type'': Optional render type: `text/html` (the default) returns the full HTML text and `text/plain` just returns the text content (ie it ignores HTML tags and other unprintable material)
* ''name'': Name of optional variable
* ''value'': Value of optional variable
By default, the filename is resolved relative to the `output` subdirectory of the edition directory. The `--output` command can be used to direct output to a different directory.
Notes:
* The output directory is not cleared of any existing files
* Any missing directories in the path to the filename are automatically created.
* When referring to a tiddler with spaces in its title, take care to use both the quotes required by your shell and also TiddlyWiki's double square brackets : `--render "[[Motovun Jack.jpg]]"`
* The filename filter is evaluated with the selected items being set to the title of the tiddler currently being rendered, allowing the title to be used as the basis for computing the filename. For example `[encodeuricomponent[]addprefix[static/]]` applies URI encoding to each title, and then adds the prefix `static/`
* The `--render` command is a more flexible replacement for both the `--rendertiddler` and `--rendertiddlers` commands, which are deprecated
Examples:
* `--render "[!is[system]]" "[encodeuricomponent[]addprefix[tiddlers/]addsuffix[.html]]"` -- renders all non-system tiddlers as files in the subdirectory "tiddlers" with URL-encoded titles and the extension HTML

View File

@ -1,6 +1,8 @@
title: $:/language/Help/rendertiddler
description: Render an individual tiddler as a specified ContentType
(Note: The `--rendertiddler` command is deprecated in favour of the new, more flexible `--render` command)
Render an individual tiddler as a specified ContentType, defaulting to `text/html` and save it to the specified filename.
Optionally the title of a template tiddler can be specified, in which case the template tiddler is rendered with the "currentTiddler" variable set to the tiddler that is being rendered (the first parameter value).

View File

@ -1,6 +1,8 @@
title: $:/language/Help/rendertiddlers
description: Render tiddlers matching a filter to a specified ContentType
(Note: The `--rendertiddlers` command is deprecated in favour of the new, more flexible `--render` command)
Render a set of tiddlers matching a filter to separate files of a specified ContentType (defaults to `text/html`) and extension (defaults to `.html`).
```

View File

@ -0,0 +1,25 @@
title: $:/language/Help/save
description: Saves individual raw tiddlers to files
Saves individual tiddlers in their raw text or binary format to the specified files.
```
--save <tiddler-filter> <filename-filter>
```
* ''tiddler-filter'': A filter identifying the tiddler(s) to be saved
* ''filename-filter'': Optional filter transforming tiddler titles into pathnames. If omitted, defaults to `[is[tiddler]]`, which uses the unchanged tiddler title as the filename
By default, the filename is resolved relative to the `output` subdirectory of the edition directory. The `--output` command can be used to direct output to a different directory.
Notes:
* The output directory is not cleared of any existing files
* Any missing directories in the path to the filename are automatically created.
* When saving a tiddler with spaces in its title, take care to use both the quotes required by your shell and also TiddlyWiki's double square brackets : `--save "[[Motovun Jack.jpg]]"`
* The filename filter is evaluated with the selected items being set to the title of the tiddler currently being saved, allowing the title to be used as the basis for computing the filename. For example `[encodeuricomponent[]addprefix[static/]]` applies URI encoding to each title, and then adds the prefix `static/`
* The `--save` command is a more flexible replacement for both the `--savetiddler` and `--savetiddlers` commands, which are deprecated
Examples:
* `--save "[!is[system]is[image]]" "[encodeuricomponent[]addprefix[tiddlers/]]"` -- saves all non-system image tiddlers as files in the subdirectory "tiddlers" with URL-encoded titles

View File

@ -1,6 +1,8 @@
title: $:/language/Help/savetiddler
description: Saves a raw tiddler to a file
(Note: The `--savetiddler` command is deprecated in favour of the new, more flexible `--save` command)
Saves an individual tiddler in its raw text or binary format to the specified filename.
```

View File

@ -1,6 +1,8 @@
title: $:/language/Help/savetiddlers
description: Saves a group of raw tiddlers to a directory
(Note: The `--savetiddlers` command is deprecated in favour of the new, more flexible `--save` command)
Saves a group of tiddlers in their raw text or binary format to the specified directory.
```

View File

@ -0,0 +1,65 @@
/*\
title: $:/core/modules/commands/render.js
type: application/javascript
module-type: command
Render individual tiddlers and save the results to the specified files
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var widget = require("$:/core/modules/widgets/widget.js");
exports.info = {
name: "render",
synchronous: true
};
var Command = function(params,commander,callback) {
this.params = params;
this.commander = commander;
this.callback = callback;
};
Command.prototype.execute = function() {
if(this.params.length < 1) {
return "Missing tiddler filter";
}
var self = this,
fs = require("fs"),
path = require("path"),
wiki = this.commander.wiki,
tiddlerFilter = this.params[0],
filenameFilter = this.params[1] || "[is[tiddler]addsuffix[.html]]",
type = this.params[2] || "text/html",
template = this.params[3],
varName = this.params[4],
varValue = this.params[5],
tiddlers = wiki.filterTiddlers(tiddlerFilter);
$tw.utils.each(tiddlers,function(title) {
var parser = wiki.parseTiddler(template || title),
variables = {currentTiddler: title};
if(varName) {
variables[varName] = varValue || "";
}
var widgetNode = wiki.makeWidget(parser,{variables: variables}),
container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
var text = type === "text/html" ? container.innerHTML : container.textContent,
filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]);
if(self.commander.verbose) {
console.log("Rendering \"" + title + "\" to \"" + filepath + "\"");
}
$tw.utils.createFileDirectories(filepath);
fs.writeFileSync(filepath,text,"utf8");
});
return null;
};
exports.Command = Command;
})();

View File

@ -0,0 +1,53 @@
/*\
title: $:/core/modules/commands/save.js
type: application/javascript
module-type: command
Saves individual tiddlers in their raw text or binary format to the specified files
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "save",
synchronous: true
};
var Command = function(params,commander,callback) {
this.params = params;
this.commander = commander;
this.callback = callback;
};
Command.prototype.execute = function() {
if(this.params.length < 1) {
return "Missing filename filter";
}
var self = this,
fs = require("fs"),
path = require("path"),
wiki = this.commander.wiki,
tiddlerFilter = this.params[0],
filenameFilter = this.params[1] || "[is[tiddler]]",
tiddlers = wiki.filterTiddlers(tiddlerFilter);
$tw.utils.each(tiddlers,function(title) {
var tiddler = self.commander.wiki.getTiddler(title),
type = tiddler.fields.type || "text/vnd.tiddlywiki",
contentTypeInfo = $tw.config.contentTypeInfo[type] || {encoding: "utf8"},
filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]);
if(self.commander.verbose) {
console.log("Saving \"" + title + "\" to \"" + filepath + "\"");
}
$tw.utils.createFileDirectories(filepath);
fs.writeFileSync(filepath,tiddler.fields.text,contentTypeInfo.encoding);
});
return null;
};
exports.Command = Command;
})();