decktape/decktape.js

339 lines
10 KiB
JavaScript
Raw Normal View History

2017-08-18 13:51:31 +03:00
#!/usr/bin/env node
2017-08-22 11:48:55 +03:00
'use strict';
const BufferReader = require('./libs/buffer'),
2017-08-21 23:56:27 +03:00
fs = require('fs'),
hummus = require('hummus'),
os = require('os'),
parser = require('./libs/nomnom'),
path = require('path'),
2017-08-21 23:56:27 +03:00
puppeteer = require('puppeteer');
2017-08-25 16:10:44 +03:00
const { delay, pause } = require('./libs/util');
2017-08-18 13:51:31 +03:00
const plugins = loadAvailablePlugins(path.join(path.dirname(__filename), 'plugins'));
2017-08-18 13:51:31 +03:00
2017-08-22 11:48:55 +03:00
parser.script('decktape').options({
url : {
position : 1,
required : true,
help : 'URL of the slides deck',
},
filename : {
position : 2,
required : true,
help : 'Filename of the output PDF file',
},
size : {
abbr : 's',
metavar : '<size>',
type : 'string',
callback : parseSize,
transform : parseSize,
help : 'Size of the slides deck viewport: <width>x<height> (ex. 1280x720)',
},
pause : {
abbr : 'p',
metavar : '<ms>',
default : 1000,
help : 'Duration in milliseconds before each slide is exported',
},
loadPause : {
full : 'load-pause',
metavar : '<ms>',
default : 0,
help : 'Duration in milliseconds between the page has loaded and starting to export slides',
},
screenshots : {
default : false,
flag : true,
help : 'Capture each slide as an image',
},
screenshotDirectory : {
full : 'screenshots-directory',
metavar : '<dir>',
default : 'screenshots',
help : 'Screenshots output directory',
},
screenshotSize : {
full : 'screenshots-size',
metavar : '<size>',
type : 'string',
list : true,
callback : parseSize,
transform : parseSize,
help : 'Screenshots resolution, can be repeated',
},
screenshotFormat : {
full : 'screenshots-format',
metavar : '<format>',
default : 'png',
choices : ['jpg', 'png'],
help : 'Screenshots image format, one of [jpg, png]',
},
slides : {
metavar : '<range>',
type : 'string',
callback : parseRange,
transform : parseRange,
help : 'Range of slides to be exported, a combination of slide indexes and ranges (e.g. \'1-3,5,8\')',
},
// Chrome options
executablePath : {
full : '--executablePath',
metavar : '<path>',
hidden : true,
type : 'string',
},
noSandbox : {
full : '--no-sandbox',
hidden : true,
flag : true,
},
2017-08-22 11:48:55 +03:00
});
2017-08-18 13:51:31 +03:00
function parseSize(size) {
2017-08-22 11:48:55 +03:00
// TODO: support device viewport sizes and graphics display standard resolutions
// see http://viewportsizes.com/ and https://en.wikipedia.org/wiki/Graphics_display_resolution
const [, width, height] = size.match(/^(\d+)x(\d+)$/);
if (!width || !height)
return '<size> must follow the <width>x<height> notation, e.g., 1280x720';
else
return { width: parseInt(width), height: parseInt(height) };
2017-08-18 13:51:31 +03:00
}
function parseRange(range) {
2017-08-22 11:48:55 +03:00
const regex = /(\d+)(?:-(\d+))?/g;
if (!range.match(regex))
return '<range> must be a combination of slide indexes and ranges, e.g., \'1-3,5,8\'';
let slide, slides = {};
while ((slide = regex.exec(range)) !== null) {
2017-08-31 19:36:58 +03:00
const [, m, n] = slide.map(i => parseInt(i));
if (isNaN(n)) {
slides[m] = true;
} else {
for (let i = m; i <= n; i++) {
2017-08-22 11:48:55 +03:00
slides[i] = true;
}
}
}
return slides;
2017-08-18 13:51:31 +03:00
}
2017-08-22 18:53:50 +03:00
parser.command('version')
.root(true)
.help('Display decktape package version')
.callback(_ => {
console.log(require('./package.json').version);
process.exit();
});
parser.nocommand()
.help(
2017-08-22 11:48:55 +03:00
`Defaults to the automatic command.
Iterates over the available plugins, picks the compatible one for presentation at the
specified <url> and uses it to export and write the PDF into the specified <filename>.`
2017-08-22 18:53:50 +03:00
);
parser.command('automatic')
.help(
2017-08-22 11:48:55 +03:00
`Iterates over the available plugins, picks the compatible one for presentation at the
specified <url> and uses it to export and write the PDF into the specified <filename>.`
2017-08-22 18:53:50 +03:00
);
2017-09-04 11:32:42 +03:00
Object.entries(plugins).forEach(([id, plugin]) => {
2017-08-22 11:48:55 +03:00
const command = parser.command(id);
2017-09-04 11:32:42 +03:00
if (typeof plugin.options === 'object')
command.options(plugin.options);
if (typeof plugin.help === 'string')
command.help(plugin.help);
2017-08-18 13:51:31 +03:00
});
// TODO: should be deactivated as well when it does not execute in a TTY context
2017-08-22 11:48:55 +03:00
if (os.name === 'windows') parser.nocolors();
const options = parser.parse(process.argv.slice(2));
(async () => {
const browser = await puppeteer.launch({
headless : true,
executablePath: options.executablePath,
args : Object.keys(options).reduce((args, option) => {
switch (option) {
case 'sandbox':
if (options.sandbox === false) args.push('--no-sandbox');
break;
}
return args;
}, [])
});
2017-08-25 18:49:39 +03:00
const page = await browser.newPage();
2017-08-28 22:49:35 +03:00
await page.emulateMedia('screen');
2017-08-22 11:48:55 +03:00
const printer = hummus.createWriter(options.filename);
2017-08-25 18:49:39 +03:00
const info = printer.getDocumentContext().getInfoDictionary();
info.creator = 'Decktape';
2017-08-22 11:48:55 +03:00
2017-08-25 16:10:44 +03:00
// TODO: add coloring
2017-08-23 11:43:03 +03:00
page
.on('console', console.log)
.on('pageerror', error => console.log('\nPage error:', error.message))
.on('requestfailed', request => console.log('\nUnable to load resource from URL:', request.url));
2017-08-22 11:48:55 +03:00
2017-08-23 11:43:03 +03:00
console.log('Loading page', options.url, '...');
2017-08-22 11:48:55 +03:00
page.goto(options.url, { waitUntil: 'load', timeout: 60000 })
2017-08-23 11:43:03 +03:00
.then(response => console.log('Loading page finished with status:', response.status))
2017-08-22 11:48:55 +03:00
.then(delay(options.loadPause))
2017-08-23 18:04:37 +03:00
.then(_ => createPlugin(page))
.then(plugin => configurePlugin(plugin)
.then(_ => configurePage(plugin, page))
.then(_ => exportSlides(plugin, page, printer))
.then(_ => {
printer.end();
process.stdout.write(`\nPrinted ${plugin.exportedSlides} slides\n`);
}))
.catch(console.log)
2017-08-23 18:04:37 +03:00
.then(_ => {
2017-08-22 11:48:55 +03:00
browser.close();
process.exit(1);
2017-08-18 13:51:31 +03:00
});
2017-08-22 11:48:55 +03:00
2017-08-23 18:04:37 +03:00
})();
2017-08-18 13:51:31 +03:00
function loadAvailablePlugins(pluginsPath) {
2017-08-22 11:48:55 +03:00
return fs.readdirSync(pluginsPath).reduce((plugins, pluginPath) => {
const [, plugin] = pluginPath.match(/^(.*)\.js$/);
if (plugin && fs.statSync(path.join(pluginsPath, pluginPath)).isFile())
plugins[plugin] = require('./plugins/' + plugin);
return plugins;
}, {});
2017-08-18 13:51:31 +03:00
}
2017-08-23 18:04:37 +03:00
async function createPlugin(page) {
let plugin;
if (!options.command || options.command === 'automatic') {
plugin = await createActivePlugin(page);
if (!plugin) {
console.log('No supported DeckTape plugin detected, falling back to generic plugin');
plugin = plugins['generic'].create(page, options);
}
} else {
plugin = plugins[options.command].create(page, options);
if (!await plugin.isActive()) {
throw Error(`Unable to activate the ${plugin.getName()} DeckTape plugin for the address: ${options.url}`);
}
}
console.log(plugin.getName(), 'DeckTape plugin activated');
return plugin;
}
async function createActivePlugin(page) {
for (let id in plugins) {
if (id === 'generic') continue;
const plugin = plugins[id].create(page, options);
if (await plugin.isActive()) return plugin;
}
}
async function configurePage(plugin, page) {
if (!options.size) {
options.size = typeof plugin.size === 'function'
? await plugin.size()
// TODO: per-plugin default size
: { width: 1280, height: 720 };
}
await page.setViewport(options.size);
}
2017-08-22 11:48:55 +03:00
async function configurePlugin(plugin) {
if (typeof plugin.configure === 'function')
await plugin.configure();
plugin.progressBarOverflow = 0;
plugin.currentSlide = 1;
plugin.exportedSlides = 0;
plugin.totalSlides = await plugin.slideCount();
2017-08-18 13:51:31 +03:00
}
2017-08-23 18:04:37 +03:00
async function exportSlides(plugin, page, printer) {
// TODO: support a more advanced "fragment to pause" mapping
// for special use cases like GIF animations
// TODO: support plugin optional promise to wait until a particular mutation
// instead of a pause
await pause(options.pause);
await exportSlide(plugin, page, printer);
2017-09-04 11:32:42 +03:00
const maxSlide = options.slides ? Math.max(...Object.keys(options.slides)) : Infinity;
2017-08-23 18:04:37 +03:00
let hasNext = await hasNextSlide(plugin);
2017-09-04 11:32:42 +03:00
while (hasNext && plugin.currentSlide < maxSlide) {
2017-08-23 18:04:37 +03:00
await nextSlide(plugin);
await pause(options.pause);
2017-08-23 20:37:14 +03:00
if (options.slides && !options.slides[plugin.currentSlide]) {
process.stdout.write('\r' + await progressBar(plugin, { skip: true }));
} else {
await exportSlide(plugin, page, printer);
}
2017-08-23 18:04:37 +03:00
hasNext = await hasNextSlide(plugin);
}
}
async function exportSlide(plugin, page, printer) {
process.stdout.write('\r' + await progressBar(plugin));
await printSlide(plugin, page, printer);
if (options.screenshots) {
for (let resolution of options.screenshotSize || [options.size]) {
await page.setViewport(resolution);
// Delay page rendering to wait for the resize event to complete,
// e.g. for impress.js (may be needed to be configurable)
await pause(1000);
await page.screenshot({
path : path.join(options.screenshotDirectory, options.filename.replace('.pdf',
`_${plugin.currentSlide}_${resolution.width}x${resolution.height}.${options.screenshotFormat}`)),
fullPage : false,
omitBackground : true,
});
await page.setViewport(options.size);
await pause(1000);
}
}
2017-08-23 18:04:37 +03:00
}
async function printSlide(plugin, page, printer) {
const buffer = await page.pdf({
width : options.size.width + 'px',
height : options.size.height + 'px',
printBackground : true,
pageRanges : '1',
displayHeaderFooter : false,
});
printer.appendPDFPagesFromPDF(new BufferReader(buffer), { specificRanges: [[0, 0]] });
plugin.exportedSlides++;
}
async function hasNextSlide(plugin) {
2017-08-22 11:48:55 +03:00
if (typeof plugin.hasNextSlide === 'function')
2017-08-23 18:04:37 +03:00
return await plugin.hasNextSlide();
2017-08-22 11:48:55 +03:00
else
return plugin.currentSlide < plugin.totalSlides;
2017-08-18 13:51:31 +03:00
}
2017-08-23 18:04:37 +03:00
async function nextSlide(plugin) {
2017-08-22 11:48:55 +03:00
plugin.currentSlide++;
2017-08-23 18:04:37 +03:00
return plugin.nextSlide();
2017-08-18 13:51:31 +03:00
}
// TODO: add progress bar, duration, ETA and file size
async function progressBar(plugin, { skip } = { skip : false }) {
2017-08-22 11:48:55 +03:00
const cols = [];
const index = await plugin.currentSlideIndex();
2017-08-23 20:37:14 +03:00
cols.push(`${skip ? 'Skipping' : 'Printing'} slide `);
2017-09-04 11:32:42 +03:00
cols.push(`#${index}`.padEnd(8));
2017-08-22 11:48:55 +03:00
cols.push(' (');
2017-09-04 11:32:42 +03:00
cols.push(`${plugin.currentSlide}`.padStart(plugin.totalSlides ? plugin.totalSlides.toString().length : 3));
2017-08-22 11:48:55 +03:00
cols.push('/');
cols.push(plugin.totalSlides || ' ?');
cols.push(') ...');
// erase overflowing slide fragments
2017-09-04 11:32:42 +03:00
cols.push(' '.repeat(Math.max(plugin.progressBarOverflow - Math.max(index.length + 1 - 8, 0), 0)));
2017-08-22 11:48:55 +03:00
plugin.progressBarOverflow = Math.max(index.length + 1 - 8, 0);
return cols.join('');
2017-08-18 13:51:31 +03:00
}