<commit_before>'use strict';

var asynEach = require('async-foreach').forEach,
    chalk = require('chalk'),
    config = require('./config'),
    fs = require('fs'),
    path = require('path'),
    PDF = require('pdfkit'),
    Q = require('q'),
    sizeOf = require('image-size'),
    urlParse = require('url-parse'),
    utils = require('./utils'),
    Website = require('./website');


var pdfExclusions = ['pdf'];


function checkFileExistsSync(filepath){
  let flag = true;
  try{
    fs.accessSync(filepath, fs.F_OK);
  }catch(e){
    flag = false;
  }
  return flag;
}





function getDate() {

    var d = new Date(),
        date =
        d.getUTCFullYear() + '-' +
        (d.getUTCMonth() + 1) + '-' +
        d.getUTCDate();
    return date;
}

/**
 * Write text to PDF object
 * @author <NAME>
 * @date   2016-04-25
 * @param  {Object}   pdf        pdf-kit object
 * @param  {String}   title      title header for field
 * @param  {String}   field      field that's part of the cotent collection
 * @param  {String}   fieldColor font color for the field parameter
 */
function writeTextPDF(pdf, title, field, fieldColor) {

    fieldColor = fieldColor || 'black';
    field =  field !== undefined ? field : '';

    pdf.fontSize(30)
        .fillColor('black')
        .text(title);

    pdf.fontSize(20)
        .fillColor(fieldColor)
        .text(field);

    pdf.moveDown();
}


/**
 * Generates a PDF containing the passed array of images along with metadata collected from Siteshooter
 * @author <NAME>
 * @date   2016-05-10
 * @param  {Object}   pdfShooter
 * @param  {Array}    arrImages
 * @param  {[type]}   dimensions
 * @param  {String}   domainName
 * @param  {String}   outputPath
 * @param  {[type]}   viewport
 * @param  {Function} done
 * @return {Function} callback
 */
function createPDF(pdfShooter, arrImages, dimensions, domainName, outputPath, viewport, done) {

    utils.log.verbose('createPDF', arguments);

    utils.log.log(chalk.green.bold('   ✔︎ '), chalk.green(viewport));

    var date = getDate(),
        page,
        pageEvent,
        pdfPath = path.join(outputPath, utils.urlToDir(domainName) + '-' + viewport + '-' + date + '.pdf'),
        tempURL,
        writeStream;

    utils.log.verbose('pdfPath', pdfPath);

    var pdf = new PDF({
        layout: 'portrait',
        margins: {
            top: 72,
            left: 60,
            right: 72,
            bottom: 20
        }
    });

    writeStream = fs.createWriteStream(pdfPath);

    pdf.pipe(writeStream);

    pdf.pipe(fs.createWriteStream(pdfPath));

    arrImages.forEach(function(png, i) {

        if ( checkFileExistsSync(png) === false ){
            return;
        }

        pageEvent = '';
        page = png.replace('screenshots/', '');

        // check for page events
        if( page.indexOf('event') !== -1 ){
            pageEvent = page.substr(page.indexOf('event'), page.indexOf('.png'));
            pageEvent = pageEvent.substr(0, pageEvent.indexOf('.png')).replace('event-','pevent=');
        }

        // remove from last slash and remaining chars (e.g., /1600.png)
        page = page.substr(0, page.lastIndexOf('/'));

        if( pageEvent !== '' ){
            page += '?' + pageEvent;
        }

        if( pdfShooter.contentCollection ){

            var pageURI, pageURL;

            // get page information stored in website collection
            page = pdfShooter.contentCollection.pages.filter(function(item) {

                pageURL = item.loc;
                pageURI = new urlParse(pageURL, true);

                if (pageURL.indexOf('visual.force') > -1) {
                    pageURL = pageURI.hostname + '/' + pageURI.query.name + pageURI.hash;
                }

                if ( pageURI.query.pevent ){
                    pageURL = pageURL + '?pevent=' + pageURI.query.pevent;
                }

                // remove protocol and last slash
                tempURL = pageURL
                    .replace(/^(http|https):\/\//, '')
                    .replace(/\/$/, '');

                if(tempURL === page){
                    pdfShooter.contentCollection.pages.shift();
                }

                return (tempURL === page);
            });

        }

        // add site-level information to cover page
        if (i === 0) {

            writeTextPDF(pdf, 'Date', date);

            writeTextPDF(pdf, 'Website', domainName, 'blue');

            writeTextPDF(pdf, 'Viewport', viewport + ' (' + dimensions.width + 'x' + dimensions.height + ')');

            //writeTextPDF(pdf, 'Google Analytics Version', page[0].meta.gaVersion);

            writeTextPDF(pdf, 'Number of webpages', arrImages.length);

        }


        // do we have page specific data?
        if (Array.isArray(page) && page.length > 0) {

            // check page object returned from content collection<commit_msg>fix(PDF): Ensure working directory is accounted for when looking page content in the collection<commit_after>'use strict';

var asynEach = require('async-foreach').forEach,
    chalk = require('chalk'),
    config = require('./config'),
    fs = require('fs'),
    path = require('path'),
    PDF = require('pdfkit'),
    Q = require('q'),
    sizeOf = require('image-size'),
    urlParse = require('url-parse'),
    utils = require('./utils'),
    Website = require('./website');


var pdfExclusions = ['pdf'];


function checkFileExistsSync(filepath){
  let flag = true;
  try{
    fs.accessSync(filepath, fs.F_OK);
  }catch(e){
    flag = false;
  }
  return flag;
}





function getDate() {

    var d = new Date(),
        date =
        d.getUTCFullYear() + '-' +
        (d.getUTCMonth() + 1) + '-' +
        d.getUTCDate();
    return date;
}

/**
 * Write text to PDF object
 * @author <NAME>
 * @date   2016-04-25
 * @param  {Object}   pdf        pdf-kit object
 * @param  {String}   title      title header for field
 * @param  {String}   field      field that's part of the cotent collection
 * @param  {String}   fieldColor font color for the field parameter
 */
function writeTextPDF(pdf, title, field, fieldColor) {

    fieldColor = fieldColor || 'black';
    field =  field !== undefined ? field : '';

    pdf.fontSize(30)
        .fillColor('black')
        .text(title);

    pdf.fontSize(20)
        .fillColor(fieldColor)
        .text(field);

    pdf.moveDown();
}


/**
 * Generates a PDF containing the passed array of images along with metadata collected from Siteshooter
 * @author <NAME>
 * @date   2016-05-10
 * @param  {Object}   pdfShooter
 * @param  {Array}    arrImages
 * @param  {[type]}   dimensions
 * @param  {String}   domainName
 * @param  {String}   outputPath
 * @param  {[type]}   viewport
 * @param  {Function} done
 * @return {Function} callback
 */
function createPDF(pdfShooter, arrImages, dimensions, domainName, outputPath, viewport, done) {

    utils.log.verbose('createPDF', arguments);

    utils.log.log(chalk.green.bold('   ✔︎ '), chalk.green(viewport));

    var date = getDate(),
        page,
        pageEvent,
        pdfPath = path.join(outputPath, utils.urlToDir(domainName) + '-' + viewport + '-' + date + '.pdf'),
        tempURL,
        writeStream;

    utils.log.verbose('pdfPath', pdfPath);

    var pdf = new PDF({
        layout: 'portrait',
        margins: {
            top: 72,
            left: 60,
            right: 72,
            bottom: 20
        }
    });

    writeStream = fs.createWriteStream(pdfPath);

    pdf.pipe(writeStream);

    pdf.pipe(fs.createWriteStream(pdfPath));

    arrImages.forEach(function(png, i) {

        if ( checkFileExistsSync(png) === false ){
            return;
        }



        pageEvent = '';
        page = png.replace(config.process.get('working_directory') + '/screenshots/', '');

        // check for page events
        if( page.indexOf('event') !== -1 ){
            pageEvent = page.substr(page.indexOf('event'), page.indexOf('.png'));
            pageEvent = pageEvent.substr(0, pageEvent.indexOf('.png')).replace('event-','pevent=');
        }

        // remove from last slash and remaining chars (e.g., /1600.png)
        page = page.substr(0, page.lastIndexOf('/'));

        if( pageEvent !== '' ){
            page += '?' + pageEvent;
        }


        if( pdfShooter.contentCollection ){

            var pageURI, pageURL;

            // get page information stored in website collection
            page = pdfShooter.contentCollection.pages.filter(function(item) {

                pageURL = item.loc;
                pageURI = new urlParse(pageURL, true);

                if (pageURL.indexOf('visual.force') > -1) {
                    pageURL = pageURI.hostname + '/' + pageURI.query.name + pageURI.hash;
                }

                if ( pageURI.query.pevent ){
                    pageURL = pageURL + '?pevent=' + pageURI.query.pevent;
                }

                // remove protocol and last slash
                tempURL = pageURL
                    .replace(/^(http|https):\/\//, '')
                    .replace(/\/$/, '');


                if(tempURL === page){
                    pdfShooter.contentCollection.pages.shift();
                }

                return (tempURL === page);
            });

        }




        // add site-level information to cover page
        if (i === 0) {

            writeTextPDF(pdf, 'Date', date);

            writeTextPDF(pdf, 'Website', domainName, 'blue');

            writeTextPDF(pdf, 'Viewport', viewport + ' (' + dimensions.width + 'x' + dimensions.height + ')');

            //writeTextPDF(pdf, 'Google Analytics Version', page[0].meta.gaVersion);

            writeTextPDF(pdf, 'Number of webpages', arrImages.length);

        }

        // do we have page specific data?
        if (Array.isArray(page) && page.length > 0) {



            // check page object returned from content collection
{'fraction_non_alphanumeric': 0.14392485607514394, 'fraction_numerical': 0.00807999192000808, 'mean_word_length': 2.0179823224626636, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '763283', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2833, 'n_words': 689}