summaryrefslogtreecommitdiffstats
path: root/lib/output/ebook/onFinish.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output/ebook/onFinish.js')
-rw-r--r--lib/output/ebook/onFinish.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/output/ebook/onFinish.js b/lib/output/ebook/onFinish.js
new file mode 100644
index 0000000..17a8e5e
--- /dev/null
+++ b/lib/output/ebook/onFinish.js
@@ -0,0 +1,90 @@
+var path = require('path');
+
+var WebsiteGenerator = require('../website');
+var JSONUtils = require('../../json');
+var Templating = require('../../templating');
+var Promise = require('../../utils/promise');
+var error = require('../../utils/error');
+var command = require('../../utils/command');
+var writeFile = require('../helper/writeFile');
+
+var getConvertOptions = require('./getConvertOptions');
+
+/**
+ Write the SUMMARY.html
+
+ @param {Output}
+ @return {Output}
+*/
+function writeSummary(output) {
+ var options = output.getOptions();
+ var prefix = options.get('prefix');
+
+ var filePath = 'SUMMARY.html';
+ var engine = WebsiteGenerator.createTemplateEngine(output, filePath);
+ var context = JSONUtils.encodeOutput(output);
+
+ // Render the theme
+ return Templating.renderFile(engine, prefix + '/SUMMARY.html', context)
+
+ // Write it to the disk
+ .then(function(html) {
+ return writeFile(output, filePath, html);
+ });
+}
+
+/**
+ Generate the ebook file as "index.pdf"
+
+ @param {Output}
+ @return {Output}
+*/
+function runEbookConvert(output) {
+ var logger = output.getLogger();
+ var options = output.getOptions();
+ var format = options.get('format');
+ var outputFolder = output.getRoot();
+
+ if (!format) {
+ return Promise(output);
+ }
+
+ return getConvertOptions(output)
+ .then(function(options) {
+ var cmd = [
+ 'ebook-convert',
+ path.resolve(outputFolder, 'SUMMARY.html'),
+ path.resolve(outputFolder, 'index.' + format),
+ command.optionsToShellArgs(options)
+ ].join(' ');
+
+ return command.exec(cmd)
+ .progress(function(data) {
+ logger.debug(data);
+ })
+ .fail(function(err) {
+ if (err.code == 127) {
+ throw error.RequireInstallError({
+ cmd: 'ebook-convert',
+ install: 'Install it from Calibre: https://calibre-ebook.com'
+ });
+ }
+
+ throw error.EbookError(err);
+ });
+ })
+ .thenResolve(output);
+}
+
+/**
+ Finish the generation, generates the SUMMARY.html
+
+ @param {Output}
+ @return {Output}
+*/
+function onFinish(output) {
+ return writeSummary(output)
+ .then(runEbookConvert);
+}
+
+module.exports = onFinish;