<commit_before>#!/usr/bin/env node


'use strict';


// stdlib
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;


// 3rd-party
var _               = require('underscore');
var async           = require('async');
var FsTools         = require('fs-tools');
var ArgumentParser  = require('argparse').ArgumentParser;


// internal
var NDoc      = require('..');
var template  = require('../lib/ndoc/common').template;


////////////////////////////////////////////////////////////////////////////////


// parse string in a BASH style
// inspired by Shellwords module of Ruby
var SHELLWORDS_PATTERN = /\s*(?:([^\s\\\'\"]+)|'((?:[^\'\\]|\\.)*)'|"((?:[^\"\\]|\\.)*)")/;
function shellwords(line) {
  var words = [], match, field;

  while (line) {
    match = SHELLWORDS_PATTERN.exec(line);

    if (!match || !match[0]) {
      line = false;
    } else {
      line  = line.substr(match[0].length);
      field = (match[1] || match[2] || match[3] || '').replace(/\\(.)/, '$1');

      words.push(field);
    }
  }

  return words;
}


function exit(err) {
    if (err) {
      console.error(err.message || err);
      process.exit(1);
    }

    process.exit(0);
}


////////////////////////////////////////////////////////////////////////////////


NDoc.cli.addArgument(['--noenv'], {
  help:   'Ignore .ndocrc',
  action: 'storeTrue'
});


if (-1 === process.argv.indexOf('--noenv')) {
  if (fs.existsSync('./.ndocrc')) {
    var rcflags = shellwords(fs.readFileSync('./.ndocrc', 'utf8'));
    [].splice.apply(process.argv, [2, 0].concat(rcflags));
  }
}


//
// preprocess plugins
//


NDoc.cli.parseKnownArgs().shift().use.forEach(function (pathname) {
  if (/^\./.test(pathname)) {
    pathname = path.resolve(process.cwd(), pathname);
  }

  try {
    NDoc.use(require(pathname));
  } catch (err) {
    exit('Failed add renderer: ' + pathname + '\n\n' + err.toString());
  }
});


//
// parse options
//


var options = NDoc.cli.parseArgs();


//
// Post-process some of the options
//

options.title = template(options.title || '', {'package': options.package});

//
// collect sources, parse into ast, render
//

async.waterfall([
  function collect_files(next) {
    NDoc.cli.findFiles(options.paths, options.exclude, next);
  },
  function parse_files(files, next) {
    NDoc.parse(files, options, next);
  },
  function render_ast(ast, next) {
    NDoc.render(options.renderer, ast, options, next);
  }
], exit);
<commit_msg>Fix options.index<commit_after>#!/usr/bin/env node


'use strict';


// stdlib
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;


// 3rd-party
var _               = require('underscore');
var async           = require('async');
var FsTools         = require('fs-tools');
var ArgumentParser  = require('argparse').ArgumentParser;


// internal
var NDoc      = require('..');
var template  = require('../lib/ndoc/common').template;


////////////////////////////////////////////////////////////////////////////////


// parse string in a BASH style
// inspired by Shellwords module of Ruby
var SHELLWORDS_PATTERN = /\s*(?:([^\s\\\'\"]+)|'((?:[^\'\\]|\\.)*)'|"((?:[^\"\\]|\\.)*)")/;
function shellwords(line) {
  var words = [], match, field;

  while (line) {
    match = SHELLWORDS_PATTERN.exec(line);

    if (!match || !match[0]) {
      line = false;
    } else {
      line  = line.substr(match[0].length);
      field = (match[1] || match[2] || match[3] || '').replace(/\\(.)/, '$1');

      words.push(field);
    }
  }

  return words;
}


function exit(err) {
    if (err) {
      console.error(err.message || err);
      process.exit(1);
    }

    process.exit(0);
}


////////////////////////////////////////////////////////////////////////////////


NDoc.cli.addArgument(['--noenv'], {
  help:   'Ignore .ndocrc',
  action: 'storeTrue'
});


if (-1 === process.argv.indexOf('--noenv')) {
  if (fs.existsSync('./.ndocrc')) {
    var rcflags = shellwords(fs.readFileSync('./.ndocrc', 'utf8'));
    [].splice.apply(process.argv, [2, 0].concat(rcflags));
  }
}


//
// preprocess plugins
//


NDoc.cli.parseKnownArgs().shift().use.forEach(function (pathname) {
  if (/^\./.test(pathname)) {
    pathname = path.resolve(process.cwd(), pathname);
  }

  try {
    NDoc.use(require(pathname));
  } catch (err) {
    exit('Failed add renderer: ' + pathname + '\n\n' + err.toString());
  }
});


//
// parse options
//


var options = NDoc.cli.parseArgs();


//
// Post-process some of the options
//

options.title = template(options.title || '', {'package': options.package});
options.index = options.index || '';

//
// collect sources, parse into ast, render
//

async.waterfall([
  function collect_files(next) {
    NDoc.cli.findFiles(options.paths, options.exclude, next);
  },
  function parse_files(files, next) {
    NDoc.parse(files, options, next);
  },
  function render_ast(ast, next) {
    NDoc.render(options.renderer, ast, options, next);
  }
], exit);

{'fraction_non_alphanumeric': 0.2637692769877829, 'fraction_numerical': 0.0052072902062888045, 'mean_word_length': 2.2855263157894736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5576928', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1633, 'n_words': 323}