<commit_before>var fs = require('fs')
var path = require('path')
var url = require('url')

var electron = require('electron')
var _ = require('lodash')

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

var HTML_DPI = 96

var PDFExporter = function (input, output, argv) {
  var app = electron.app
  app.on('ready', appReady)
  app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })

  function appReady () {
    var customCss = argv.css

    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 markdown or html to pdf
   * @param {String} indexUrl The path to the HTML or url, or a markdown file
   *   with 'md' or 'markdown' extension.
   * @param output The name of the file to export to.  If the extension is
   *   '.png' then a PNG image will be generated instead of a PDF.
   */
  function render (indexUrl, output) {
    var requestedURL = url.parse(indexUrl)
    var win = launchBrowserWindow()
    setSessionCookies(argv.cookies, requestedURL, win)
    loadURL(win, indexUrl)
    win.webContents.on('did-finish-load', function () {
      setTimeout(
        generateOutput.bind(this, win, output),
        argv.outputWait)
    })
  }

  /**
   *
   * @param {String} cookies - ';' delimited cookies, '=' delimited name/value
   *   pairs
   * @param {URL} requestedURL - URL Object
   * @param {BrowserWindow} win - The electron browser window
   */
  function setSessionCookies (cookies, requestedURL, win) {
    if (cookies) {
      cookies.split(';').forEach(function (c) {
        var nameValue = c.split('=')
        var cookie = {
          url: requestedURL.protocol + '//' + requestedURL.host,
          name: nameValue[0],
          value: nameValue[1]
        }
        win.webContents.session.cookies.set(cookie, function (err) {
          if (err) {
            console.log(err)
          }
        })
      })
    }
  }

  /**
   *
   * @returns {electron.BrowserWindow}
   */
  function launchBrowserWindow () {
    function pdfToPixels (inches) {
      return Math.floor(inches * HTML_DPI)
    }

    var pageDimensions = {
      'A3': {x: pdfToPixels(11.7), y: pdfToPixels(16.5)},
      'A4': {x: pdfToPixels(8.3), y: pdfToPixels(11.7)},
      'A5': {x: pdfToPixels(5.8), y: pdfToPixels(8.3)},
      'Letter': {x: pdfToPixels(8.5), y: pdfToPixels(11)},
      'Legal': {x: pdfToPixels(8.5), y: pdfToPixels(14)},
      'Tabloid': {x: pdfToPixels(11), y: pdfToPixels(17)}
    }

    var pageDim = pageDimensions[argv.pageSize]
    pageDim = argv.landscape ? {x: pageDim.y, y: pageDim.x} : pageDim

    var defaultOpts = {
      width: pageDim.x,
      height: pageDim.y,
      enableLargerThanScreen: true,
      show: false
    }

    var browserConfig = _.extend(defaultOpts, JSON.parse(argv.browserConfig || {}))

    console.info('Opening a browser window', browserConfig.width, 'x', browserConfig.height)
    var win = new electron.BrowserWindow(browserConfig)<commit_msg>fixed bug with browserConfig default<commit_after>var fs = require('fs')
var path = require('path')
var url = require('url')

var electron = require('electron')
var _ = require('lodash')

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

var HTML_DPI = 96

var PDFExporter = function (input, output, argv) {
  var app = electron.app
  app.on('ready', appReady)
  app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })

  function appReady () {
    var customCss = argv.css

    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 markdown or html to pdf
   * @param {String} indexUrl The path to the HTML or url, or a markdown file
   *   with 'md' or 'markdown' extension.
   * @param output The name of the file to export to.  If the extension is
   *   '.png' then a PNG image will be generated instead of a PDF.
   */
  function render (indexUrl, output) {
    var requestedURL = url.parse(indexUrl)
    var win = launchBrowserWindow()
    setSessionCookies(argv.cookies, requestedURL, win)
    loadURL(win, indexUrl)
    win.webContents.on('did-finish-load', function () {
      setTimeout(
        generateOutput.bind(this, win, output),
        argv.outputWait)
    })
  }

  /**
   *
   * @param {String} cookies - ';' delimited cookies, '=' delimited name/value
   *   pairs
   * @param {URL} requestedURL - URL Object
   * @param {BrowserWindow} win - The electron browser window
   */
  function setSessionCookies (cookies, requestedURL, win) {
    if (cookies) {
      cookies.split(';').forEach(function (c) {
        var nameValue = c.split('=')
        var cookie = {
          url: requestedURL.protocol + '//' + requestedURL.host,
          name: nameValue[0],
          value: nameValue[1]
        }
        win.webContents.session.cookies.set(cookie, function (err) {
          if (err) {
            console.log(err)
          }
        })
      })
    }
  }

  /**
   *
   * @returns {electron.BrowserWindow}
   */
  function launchBrowserWindow () {
    function pdfToPixels (inches) {
      return Math.floor(inches * HTML_DPI)
    }

    var pageDimensions = {
      'A3': {x: pdfToPixels(11.7), y: pdfToPixels(16.5)},
      'A4': {x: pdfToPixels(8.3), y: pdfToPixels(11.7)},
      'A5': {x: pdfToPixels(5.8), y: pdfToPixels(8.3)},
      'Letter': {x: pdfToPixels(8.5), y: pdfToPixels(11)},
      'Legal': {x: pdfToPixels(8.5), y: pdfToPixels(14)},
      'Tabloid': {x: pdfToPixels(11), y: pdfToPixels(17)}
    }

    var pageDim = pageDimensions[argv.pageSize]
    pageDim = argv.landscape ? {x: pageDim.y, y: pageDim.x} : pageDim

    var defaultOpts = {
      width: pageDim.x,
      height: pageDim.y,
      enableLargerThanScreen: true,
      show: false
    }

    var browserConfig = _.extend(defaultOpts, JSON.parse(argv.browserConfig || '{}'))

    console.info('Opening a browser window', browserConfig.width, 'x', browserConfig.height)
    var win = new electron.BrowserWindow(browserConfig)
{'fraction_non_alphanumeric': 0.13725225832546853, 'fraction_numerical': 0.009707428879600917, 'mean_word_length': 2.568061568061568, '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': '5459864', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2419, 'n_words': 605}