2013-10-21 17:33:00 +00:00
|
|
|
/*\
|
2013-11-08 08:51:14 +00:00
|
|
|
title: $:/core/modules/commands/rendertiddlers.js
|
2013-10-21 17:33:00 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: command
|
|
|
|
|
|
|
|
Command to render several tiddlers to a folder of files
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var widget = require("$:/core/modules/widgets/widget.js");
|
2013-10-21 17:33:00 +00:00
|
|
|
|
|
|
|
exports.info = {
|
2013-11-08 08:51:14 +00:00
|
|
|
name: "rendertiddlers",
|
2013-10-21 17:33:00 +00:00
|
|
|
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 < 2) {
|
|
|
|
return "Missing filename";
|
|
|
|
}
|
|
|
|
var self = this,
|
|
|
|
fs = require("fs"),
|
|
|
|
path = require("path"),
|
|
|
|
wiki = this.commander.wiki,
|
|
|
|
filter = this.params[0],
|
|
|
|
template = this.params[1],
|
2015-02-20 21:49:34 +00:00
|
|
|
outputPath = this.commander.outputPath,
|
2021-05-30 18:20:17 +00:00
|
|
|
pathname = path.resolve(outputPath,this.params[2]),
|
2013-10-21 17:33:00 +00:00
|
|
|
type = this.params[3] || "text/html",
|
|
|
|
extension = this.params[4] || ".html",
|
2015-09-29 16:04:01 +00:00
|
|
|
deleteDirectory = (this.params[5] || "").toLowerCase() !== "noclean",
|
2013-10-21 17:33:00 +00:00
|
|
|
tiddlers = wiki.filterTiddlers(filter);
|
2015-02-20 22:17:28 +00:00
|
|
|
if(deleteDirectory) {
|
2015-01-10 21:44:56 +00:00
|
|
|
$tw.utils.deleteDirectory(pathname);
|
|
|
|
}
|
2013-10-21 17:33:00 +00:00
|
|
|
$tw.utils.each(tiddlers,function(title) {
|
2013-11-08 08:51:14 +00:00
|
|
|
var parser = wiki.parseTiddler(template),
|
2022-06-29 07:58:40 +00:00
|
|
|
widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title, storyTiddler: title}}),
|
2015-02-20 22:17:28 +00:00
|
|
|
container = $tw.fakeDocument.createElement("div");
|
2013-10-21 17:33:00 +00:00
|
|
|
widgetNode.render(container,null);
|
2015-02-20 22:17:28 +00:00
|
|
|
var text = type === "text/html" ? container.innerHTML : container.textContent,
|
|
|
|
exportPath = null;
|
2015-02-20 21:45:40 +00:00
|
|
|
if($tw.utils.hop($tw.macros,"tv-get-export-path")) {
|
2015-02-20 22:06:59 +00:00
|
|
|
var macroPath = $tw.macros["tv-get-export-path"].run.apply(self,[title]);
|
2015-02-20 22:17:28 +00:00
|
|
|
if(macroPath) {
|
|
|
|
exportPath = path.resolve(outputPath,macroPath + extension);
|
2015-02-20 22:06:59 +00:00
|
|
|
}
|
2015-02-20 21:45:40 +00:00
|
|
|
}
|
2023-01-19 17:45:54 +00:00
|
|
|
var finalPath = exportPath || path.resolve(pathname,$tw.utils.encodeURIComponentExtended(title) + extension);
|
2015-02-20 22:17:28 +00:00
|
|
|
$tw.utils.createFileDirectories(finalPath);
|
|
|
|
fs.writeFileSync(finalPath,text,"utf8");
|
2013-10-21 17:33:00 +00:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.Command = Command;
|
|
|
|
|
|
|
|
})();
|