<commit_before>#!/usr/bin/env node
/*
 * Tool to crawl a Reveal.js slide deck and capture each fragment to build a PDF.
 * Author: <NAME> (codemiller)
 */

var fs = require('fs');
var path = require('path');
var build = require('./../build.js');
var capture = require('./../capture.js');
var resolutionPattern = /(\d+)x(\d+)/;

var argv = require('yargs')
      .options('t', {
        alias: 'target',
        default: 'output',
        describe: 'Directory to write the images and PDF'
      })
      .options('o', {
        alias: 'output',
        default: 'presentation',
        describe: 'Filename for the PDF (without an extension)'
      })
      .options('resolution', {
        describe: 'Resolution for viewport; if unspecified, tool will use the Reveal.js slide dimensions'
      })
      .options('format', {
        default: 'jpeg',
        describe: 'Format of images captured'
      })
      .options('controls', {
        default: false,
        boolean: true
      })
      .options('wait', {
        alias: 'wait-time',
        default: 1000,
        describe: 'Number of milliseconds to wait for each slide/fragment to load before capture'
      })
      .options('quality', {
        default: 100,
        describe: 'Quality of images captured'
      })
      .options('build', {
        default: true,
        boolean: true,
        describe: 'Skip slide capture and build a PDF from images in the target directory'
      })
      .options('capture', {
        default: true,
        boolean: true,
        describe: 'Capture the slides, but do not render to PDF'
      })
      .options('controls', {
        default: false,
        describe: 'Show the Reveal.js controls'
      })
      .options('max-index', {
        default: 9999,
        describe: 'Maximum to use for frame indices'
      })
      .check(function(argv){
        if (['jpeg', 'png'].indexOf(argv.format)) {
          throw new TypeError('--format should be either png or jpeg.');
        }
      })
      .check(function(argv){
        if (argv.resolution && !argv.resolution.match(resolutionPattern)){
          throw new TypeError('Resolution must have format {height}x{width}');
        }
      })
      .usage('Save Reveal.js as PDF.\nUsage: $0 http://myrevealpres.com')
      .demand(1, 'Reveal.js URL needs to be provided as an argument.')
      .strict()
      .argv;

var targetDir = path.join(process.cwd(), argv.target);
var resolution = null;
var url = argv._[0];

fs.mkdir(targetDir, function (err) {
   if (err && err.code !== 'EEXIST') {
       console.log('Directory ' + targetDir + ' could not be created');
       process.exit(1);
   }
});

if (argv.resolution) {
  var matches = argv.resolution.match(pattern);

  resolution = { 'width': matches[1], 'height': matches[2] };
}

require('async').waterfall([
  function captureStep(done){
    if (!argv.capture){
      return done();
    }

    capture(url, targetDir, resolution, argv.quality, argv.format, argv.controls, argv.waitTime, argv.maxIndex, done);
  },
  function buildStep(done){
    build(targetDir, argv.output, argv.format, done);
  }
]);<commit_msg>Fix the `--capture` and `--build` flags behaviours.<commit_after>#!/usr/bin/env node
/*
 * Tool to crawl a Reveal.js slide deck and capture each fragment to build a PDF.
 * Author: <NAME> (codemiller)
 */

var fs = require('fs');
var path = require('path');
var build = require('./../build.js');
var capture = require('./../capture.js');
var resolutionPattern = /(\d+)x(\d+)/;

var argv = require('yargs')
      .options('t', {
        alias: 'target',
        default: 'output',
        describe: 'Directory to write the images and PDF'
      })
      .options('o', {
        alias: 'output',
        default: 'presentation',
        describe: 'Filename for the PDF (without an extension)'
      })
      .options('resolution', {
        describe: 'Resolution for viewport; if unspecified, tool will use the Reveal.js slide dimensions'
      })
      .options('format', {
        default: 'jpeg',
        describe: 'Format of images captured'
      })
      .options('controls', {
        default: false,
        boolean: true
      })
      .options('wait', {
        alias: 'wait-time',
        default: 1000,
        describe: 'Number of milliseconds to wait for each slide/fragment to load before capture'
      })
      .options('quality', {
        default: 100,
        describe: 'Quality of images captured'
      })
      .options('build', {
        default: false,
        boolean: true,
        describe: 'Skip slide capture and build a PDF from images in the target directory'
      })
      .options('capture', {
        default: false,
        boolean: true,
        describe: 'Capture the slides, but do not render to PDF'
      })
      .options('controls', {
        default: false,
        describe: 'Show the Reveal.js controls'
      })
      .options('max-index', {
        default: 9999,
        describe: 'Maximum to use for frame indices'
      })
      .check(function(argv){
        if (['jpeg', 'png'].indexOf(argv.format)) {
          throw new TypeError('--format should be either png or jpeg.');
        }
      })
      .check(function(argv){
        if (argv.resolution && !argv.resolution.match(resolutionPattern)){
          throw new TypeError('Resolution must have format {height}x{width}');
        }
      })
      .usage('Save Reveal.js as PDF.\nUsage: $0 http://myrevealpres.com')
      .example('$0 http://myrevealpres.com --capture', 'downloads the slides only')
      .example('$0 http://myrevealpres.com --build', 'renders the PDF only')
      .demand(1, 'Reveal.js URL needs to be provided as an argument.')
      .strict()
      .argv;

var targetDir = path.join(process.cwd(), argv.target);
var resolution = null;
var url = argv._[0];
var buildAndCatpure = argv.capture === false && argv.build === false;

fs.mkdir(targetDir, function (err) {
   if (err && err.code !== 'EEXIST') {
       console.log('Directory ' + targetDir + ' could not be created');
       process.exit(1);
   }
});

if (argv.resolution) {
  var matches = argv.resolution.match(pattern);

  resolution = { 'width': matches[1], 'height': matches[2] };
}

require('async').waterfall([
  function captureStep(done){
    if (!argv.capture && !buildAndCatpure){
      return done();
    }

    capture(url, targetDir, resolution, argv.quality, argv.format, argv.controls, argv.waitTime, argv.maxIndex, done);
  },
  function buildStep(done){
    if (!argv.build && !buildAndCatpure){
      return done();
    }

    build(targetDir, argv.output, argv.format, done);
  }
]);
{'fraction_non_alphanumeric': 0.14910025706940874, 'fraction_numerical': 0.0054438227733252685, 'mean_word_length': 2.4573967590172505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2531499', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1907, 'n_words': 566}