<commit_before>/*
 *
 * Diff to HTML CLI (main.js)
 * Author: rtfpessoa
 *
 */

var argv = require('yargs')
  .usage('Usage: diff2html [options]')
  .options({
    's': {
      alias: 'style',
      describe: 'Output style',
      nargs: 1,
      type: 'string',
      choices: ['line', 'side'],
      default: 'line'
    }
  })
  .options({
    'f': {
      alias: 'format',
      describe: 'Output format',
      nargs: 1,
      type: 'string',
      choices: ['html', 'json'],
      default: 'html'
    }
  })
  .options({
    'd': {
      alias: 'diff',
      describe: 'Diff style',
      nargs: 1,
      type: 'string',
      choices: ['word', 'char'],
      default: 'word'
    }
  })
  .options({
    'i': {
      alias: 'input',
      describe: 'Diff input source',
      nargs: 1,
      type: 'string',
      choices: ['file', 'command'],
      default: 'command'
    }
  })
  .options({
    'o': {
      alias: 'output',
      describe: 'Output destination',
      nargs: 1,
      type: 'string',
      choices: ['preview', 'stdout'],
      default: 'preview'
    }
  })
  .options({
    'F': {
      alias: 'file',
      describe: 'Send output to file (overrides output option)',
      nargs: 1,
      type: 'string'
    }
  })
  .example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
  'diff last commit, line by line, word comparison between lines,' +
  'previewed in the browser and input from git diff command')
  .example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
  .example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
  .example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
  .version(function() {
    return require('../package').version;
  })
  .help('h')
  .alias('h', 'help')
  .epilog('© 2015 rtfpessoa\n' +
  'For support, check out https://github.com/rtfpessoa/diff2html-cli')
  .argv;

main();

function main() {
  var input = getInput();
  if (input) {
    var content = getOutput(input);

    if (argv.F) {
      writeFile(argv.F, content);
    } else if (argv.output == 'preview') {
      preview(content);
    } else {
      print(content);
    }
  } else {
    error('The input is empty. Try again.');
    argv.help();
  }

  process.exit(0);
}

function getInput() {
  if (argv.input == 'file') {
    return readFile(argv._[0]);
  } else {
    var gitArgs;
    if (argv._.length && argv._[0]) {
      gitArgs = argv._.join(' ');
    } else {
      gitArgs = '-M HEAD~1'
    }

    var diffCommand = 'git diff ' + gitArgs;
    return runCmd(diffCommand);
  }
}

function getOutput(input) {
  var diff2Html = require('diff2html').Diff2Html;

  var config = {};
  config.wordByWord = (argv.diff === 'word');
  config.charByChar = (argv.diff === 'char');

  if (argv.format === 'html') {
    var htmlContent;
    if (argv.style === 'side') {
      htmlContent = diff2Html.getPrettySideBySideHtmlFromDiff(input, config);
    } else {
      htmlContent = diff2Html.getPrettyHtmlFromDiff(input, config);<commit_msg>use === intead of ==, use full arg name file intead of F<commit_after>/*
 *
 * Diff to HTML CLI (main.js)
 * Author: rtfpessoa
 *
 */

var argv = require('yargs')
  .usage('Usage: diff2html [options]')
  .options({
    's': {
      alias: 'style',
      describe: 'Output style',
      nargs: 1,
      type: 'string',
      choices: ['line', 'side'],
      default: 'line'
    }
  })
  .options({
    'f': {
      alias: 'format',
      describe: 'Output format',
      nargs: 1,
      type: 'string',
      choices: ['html', 'json'],
      default: 'html'
    }
  })
  .options({
    'd': {
      alias: 'diff',
      describe: 'Diff style',
      nargs: 1,
      type: 'string',
      choices: ['word', 'char'],
      default: 'word'
    }
  })
  .options({
    'i': {
      alias: 'input',
      describe: 'Diff input source',
      nargs: 1,
      type: 'string',
      choices: ['file', 'command'],
      default: 'command'
    }
  })
  .options({
    'o': {
      alias: 'output',
      describe: 'Output destination',
      nargs: 1,
      type: 'string',
      choices: ['preview', 'stdout'],
      default: 'preview'
    }
  })
  .options({
    'F': {
      alias: 'file',
      describe: 'Send output to file (overrides output option)',
      nargs: 1,
      type: 'string'
    }
  })
  .example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
  'diff last commit, line by line, word comparison between lines,' +
  'previewed in the browser and input from git diff command')
  .example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
  .example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
  .example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
  .version(function() {
    return require('../package').version;
  })
  .help('h')
  .alias('h', 'help')
  .epilog('© 2015 rtfpessoa\n' +
  'For support, check out https://github.com/rtfpessoa/diff2html-cli')
  .argv;

main();

function main() {
  var input = getInput();
  if (input) {
    var content = getOutput(input);

    if (argv.file) {
      writeFile(argv.file, content);
    } else if (argv.output === 'preview') {
      preview(content);
    } else {
      print(content);
    }
  } else {
    error('The input is empty. Try again.');
    argv.help();
  }

  process.exit(0);
}

function getInput() {
  if (argv.input === 'file') {
    return readFile(argv._[0]);
  } else {
    var gitArgs;
    if (argv._.length && argv._[0]) {
      gitArgs = argv._.join(' ');
    } else {
      gitArgs = '-M HEAD~1'
    }

    var diffCommand = 'git diff ' + gitArgs;
    return runCmd(diffCommand);
  }
}

function getOutput(input) {
  var diff2Html = require('diff2html').Diff2Html;

  var config = {};
  config.wordByWord = (argv.diff === 'word');
  config.charByChar = (argv.diff === 'char');

  if (argv.format === 'html') {
    var htmlContent;
    if (argv.style === 'side') {
      htmlContent = diff2Html.getPrettySideBySideHtmlFromDiff(input, config);
    } else {
      htmlContent = diff2Html.getPrettyHtmlFromDiff(input, config);
{'fraction_non_alphanumeric': 0.17584917926214855, 'fraction_numerical': 0.009101251422070534, 'mean_word_length': 2.4207893274041132, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278914', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2111, 'n_words': 557}