<commit_before>var electron = require('electron')
var argv = require('minimist')(process.argv.slice(2))
var fs = require('fs')
var path = require('path')

var app = electron.app
var BrowserWindow = electron.BrowserWindow

var wargs = require('./lib/args')
var markdownToHTMLPath = require('./lib/markdown')

app.on('ready', appReady)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

function appReady () {
  var input = argv._[0] || argv.i || argv.input
  var output = argv._[1] || argv.o || argv.output
  var customCss = argv.c || argv.css

  if (!input || !output) {
    usage(0)
    app.quit()
  }

  if (argv.h || argv.help) {
    usage(0)
  }

  input = path.resolve(input)

  function isMarkdown (input) {
    var ext = path.extname(input)
    return ext.indexOf('md') > 0 || ext.indexOf('markdown') > 0
  }

  if (isMarkdown(input)) {
    var opts = {}

    if (customCss) {
      opts.customCss = customCss
    }

    // if given a markdown, render it into HTML and return the path of the HTML
    input = markdownToHTMLPath(input, opts, function (err, tmpHTMLPath) {
      if (err) {
        console.error('Parse markdown file error', err)
        app.quit()
      }

      var indexUrl = wargs.urlWithArgs(tmpHTMLPath, {})
      render(indexUrl, output)
    })
  } else {
    var indexUrl = wargs.urlWithArgs(input, {})
    render(indexUrl, output)
  }
}

/**
 * render file to pdf
 * @param  {String} indexUrl The path to the HTML or url
 */
function render (indexUrl, output) {
  var win = new BrowserWindow({ width: 0, height: 0, show: false })
  win.on('closed', function () { win = null })
  win.loadURL(indexUrl)

  // print to pdf args
  var opts = {
    marginType: argv.m || argv.marginType || 0,
    printBackground: argv.p || argv.printBackground || true,
    printSelectionOnly: argv.s || argv.printSelectionOnly || false,
    landscape: argv.l || argv.landscape || false
  }

  win.webContents.on('did-finish-load', function () {
    win.webContents.printToPDF(opts, function (err, data) {
      if (err) {
        console.error(err)
      }

      fs.writeFile(path.resolve(output), data, function (err) {
        if (err) {
          console.error(err)
        }
        app.quit()
      })
    })
  })
}

function usage(code) {
  var rs = fs.createReadStream(__dirname + '/usage.txt')
  rs.pipe(process.stdout)
  rs.on('close', function() {
    if (code) process.exit(code)
  })
}<commit_msg>fix code style<commit_after>var electron = require('electron')
var argv = require('minimist')(process.argv.slice(2))
var fs = require('fs')
var path = require('path')

var app = electron.app
var BrowserWindow = electron.BrowserWindow

var wargs = require('./lib/args')
var markdownToHTMLPath = require('./lib/markdown')

app.on('ready', appReady)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

function appReady () {
  var input = argv._[0] || argv.i || argv.input
  var output = argv._[1] || argv.o || argv.output
  var customCss = argv.c || argv.css

  if (!input || !output) {
    usage(0)
    app.quit()
  }

  if (argv.h || argv.help) {
    usage(0)
  }

  input = path.resolve(input)

  function isMarkdown (input) {
    var ext = path.extname(input)
    return ext.indexOf('md') > 0 || ext.indexOf('markdown') > 0
  }

  if (isMarkdown(input)) {
    var opts = {}

    if (customCss) {
      opts.customCss = customCss
    }

    // if given a markdown, render it into HTML and return the path of the HTML
    input = markdownToHTMLPath(input, opts, function (err, tmpHTMLPath) {
      if (err) {
        console.error('Parse markdown file error', err)
        app.quit()
      }

      var indexUrl = wargs.urlWithArgs(tmpHTMLPath, {})
      render(indexUrl, output)
    })
  } else {
    var indexUrl = wargs.urlWithArgs(input, {})
    render(indexUrl, output)
  }
}

/**
 * render file to pdf
 * @param  {String} indexUrl The path to the HTML or url
 */
function render (indexUrl, output) {
  var win = new BrowserWindow({ width: 0, height: 0, show: false })
  win.on('closed', function () { win = null })
  win.loadURL(indexUrl)

  // print to pdf args
  var opts = {
    marginType: argv.m || argv.marginType || 0,
    printBackground: argv.p || argv.printBackground || true,
    printSelectionOnly: argv.s || argv.printSelectionOnly || false,
    landscape: argv.l || argv.landscape || false
  }

  win.webContents.on('did-finish-load', function () {
    win.webContents.printToPDF(opts, function (err, data) {
      if (err) {
        console.error(err)
      }

      fs.writeFile(path.resolve(output), data, function (err) {
        if (err) {
          console.error(err)
        }
        app.quit()
      })
    })
  })
}

function usage (code) {
  var rs = fs.createReadStream(__dirname + '/usage.txt')
  rs.pipe(process.stdout)
  rs.on('close', function () {
    if (code) process.exit(code)
  })
}
{'fraction_non_alphanumeric': 0.1535043425570592, 'fraction_numerical': 0.004039587962027873, 'mean_word_length': 2.8477078477078477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4677659', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1645, 'n_words': 407}