<commit_before>'use strict';

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }

/* eslint camelcase: 0, no-shadow: 0 */

var path = require('path'),
    fs = require('fs'),
    _ = require('underscore'),
    color = require('tinycolor2'),
    cheerio = require('cheerio'),
    colors = require('colors'),
    jsonxml = require('jsontoxml'),
    sizeOf = require('image-size'),
    async = require('async'),
    mkdirp = require('mkdirp'),
    Jimp = require('jimp'),
    File = require('vinyl'),
    Reflect = require('harmony-reflect'),
    NRC = require('node-rest-client').Client;

(function () {
    'use strict';

    var xmlconfig = { prettyPrint: true, xmlHeader: true, indent: '  ' },
        client = new NRC(),
        HEX_MAX = 255,
        NON_EXISTANT = -1,
        ROTATE_DEGREES = 90,
        HTTP_SUCCESS = 200;

    client.setMaxListeners(0);

    function helpers(options) {

        function contains(array, element) {
            return array.indexOf(element.toLowerCase()) > NON_EXISTANT;
        }

        function relative(directory) {
            return path.join(options.path, directory).replace(/\\/g, '/');
        }

        function absolute(directory) {
            return path.join(options.url, options.path, directory).replace(/\\/g, '/');
        }

        function print(context, message) {
            var newMessage = '';

            if (options.logging && message) {
                _.each(message.split(' '), function (item) {
                    newMessage += ' ' + (/^\d+x\d+$/gm.test(item) ? colors.magenta(item) : item);
                });
                console.log(colors.green('[Favicons]') + ' ' + context.yellow + ':' + newMessage + '...');
            }
        }

        function readFile(filepath, callback) {
            fs.readFile(filepath, function (error, buffer) {
                return callback(error, buffer);
            });
        }

        function updateDocument(document, code, tags, next) {
            var $ = cheerio.load(document, { decodeEntities: false }),
                target = $('head').length > 0 ? $('head') : $.root(),
                newCode = cheerio.load(code.join('\n'), { decodeEntities: false });

            async.each(tags, function (platform, callback) {
                async.forEachOf(platform, function (tag, selector, cb) {
                    if (options.replace) {
                        $(selector).remove();
                    } else if ($(selector).length) {
                        newCode(selector).remove();
                    }
                    return cb(null);
                }, function (error) {
                    return callback(error);
                });
            }, function (error) {
                target.append(newCode.html());
                return next(error, $.html().replace(/^\s*$[\n\r]{1,}/gm, ''));
            });
        }

        return {

            General: {
                background: function background(hex) {
                    print('General:background', 'Parsing colour ' + hex);
                    var rgba = color(hex).toRgb();

                    return Jimp.rgbaToInt(rgba.r, rgba.g, rgba.b, rgba.a * HEX_MAX);
                },
                source: function source(_source, callback) {
                    var sourceset = [];

                    print('General:source', 'Source type is ' + (typeof _source === 'undefined' ? 'undefined' : _typeof(_source)));
                    if (!_source || !_source.length) {
                        return callback('No source provided');
                    } else if (Buffer.isBuffer(_source)) {
                        sourceset = [{ size: sizeOf(_source), file: _source }];
                        return callback(sourceset.length ? null : 'Favicons source is invalid', sourceset);
                    } else if ((typeof _source === 'undefined' ? 'undefined' : _typeof(_source)) === 'object') {
                        async.each(_source, function (file, size, cb) {
                            return readFile(file, function (error, buffer) {
                                sourceset.push({
                                    size: { width: size, height: size, type: 'png' },
                                    file: buffer
                                });
                                return cb(error);
                            });
                        }, function (error) {
                            return callback(error || sourceset.length ? null : 'Favicons source is invalid');
                        }, sourceset);
                    } else if (typeof _source === 'string') {
                        readFile(_source, function (error, buffer) {
                            sourceset = [{ size: sizeOf(buffer), file: buffer }];
                            return callback(error || (sourceset.length ? null : 'Favicons source is invalid'), sourceset);
                        });
                    } else {
                        return callback('Invalid source type provided');
                    }
                },

                vinyl: function vinyl(object) {
                    return new File({
                        path: object.name,
                        contents: Buffer.isBuffer(object.contents) ? object.contents : new Buffer(object.contents)
                    });
                }
            },

            HTML: {
                parse: function parse(html, callback) {
                    print('HTML:parse', 'HTML found, parsing and modifying source');
                    var $ = cheerio.load(html),
                        link = $('*').is('link'),
                        attribute = link ? 'href' : 'content',
                        value = $('*').first().attr(attribute);

                    if (path.extname(value)) {
                        if (html.indexOf('og:image') !== NON_EXISTANT || html.indexOf('twitter:image') !== NON_EXISTANT) {
                            $('*').first().attr(attribute, absolute(value));
                        } else {
                            $('*').first().attr(attribute, relative(value));
                        }
                    } else if (value.slice(0, 1) === '#') {
                        $('*').first().attr(attribute, options.background);
                    } else if (html.indexOf('application-name') !== NON_EXISTANT || html.indexOf('apple-mobile-web-app-title') !== NON_EXISTANT) {
                        $('*').first().attr(attribute, options.appName);
                    }
                    return callback(null, $.html());
                },
                update: function update(document, code, tags, callback) {
                    var encoding = { encoding: 'utf8' };

                    async.waterfall([function (cb) {
                        return mkdirp(path.dirname(document), function (error) {
                            return cb(error);
                        });
                    }, function (cb) {
                        return fs.readFile(document, encoding, function (error, data) {
                            return cb(null, error ? null : data);
                        });
                    }, function (data, cb) {
                        if (data) {
                            updateDocument(data, code, tags, function (error, html) {
                                return cb(error, html);
                            });
                        } else {
                            return cb(null, code.join('\n'));
                        }
                    }, function (html, cb) {
                        return fs.writeFile(document, html, options, function (error) {
                            return cb(error);
                        });
                    }], function (error) {
                        return callback(error);
                    });
                }
            },

            Files: {
                create: function create(properties, name, callback) {
                    print('Files:create', 'Creating file: ' + name);
                    if (name === 'manifest.json') {
                        properties.name = options.appName;
                        properties.display = options.display;
                        properties.orientation = options.orientation;
                        _.map(properties.icons, function (icon) {
                            return icon.src = relative(icon.src);
                        });
                        properties = JSON.stringify(properties, null, 2);
                    } else if (name === 'manifest.webapp') {
                        properties.version = options.version;
                        properties.name = options.appName;
                        properties.description = options.appDescription;
                        properties.developer.name = options.developerName;
                        properties.developer.url = options.developerURL;
                        _.map(properties.icons, function (property) {
                            return property = relative(property);
                        });
                        properties = JSON.stringify(properties, null, 2);<commit_msg>update short_name to appName<commit_after>'use strict';

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }

/* eslint camelcase: 0, no-shadow: 0 */

var path = require('path'),
    fs = require('fs'),
    _ = require('underscore'),
    color = require('tinycolor2'),
    cheerio = require('cheerio'),
    colors = require('colors'),
    jsonxml = require('jsontoxml'),
    sizeOf = require('image-size'),
    async = require('async'),
    mkdirp = require('mkdirp'),
    Jimp = require('jimp'),
    File = require('vinyl'),
    Reflect = require('harmony-reflect'),
    NRC = require('node-rest-client').Client;

(function () {
    'use strict';

    var xmlconfig = { prettyPrint: true, xmlHeader: true, indent: '  ' },
        client = new NRC(),
        HEX_MAX = 255,
        NON_EXISTANT = -1,
        ROTATE_DEGREES = 90,
        HTTP_SUCCESS = 200;

    client.setMaxListeners(0);

    function helpers(options) {

        function contains(array, element) {
            return array.indexOf(element.toLowerCase()) > NON_EXISTANT;
        }

        function relative(directory) {
            return path.join(options.path, directory).replace(/\\/g, '/');
        }

        function absolute(directory) {
            return path.join(options.url, options.path, directory).replace(/\\/g, '/');
        }

        function print(context, message) {
            var newMessage = '';

            if (options.logging && message) {
                _.each(message.split(' '), function (item) {
                    newMessage += ' ' + (/^\d+x\d+$/gm.test(item) ? colors.magenta(item) : item);
                });
                console.log(colors.green('[Favicons]') + ' ' + context.yellow + ':' + newMessage + '...');
            }
        }

        function readFile(filepath, callback) {
            fs.readFile(filepath, function (error, buffer) {
                return callback(error, buffer);
            });
        }

        function updateDocument(document, code, tags, next) {
            var $ = cheerio.load(document, { decodeEntities: false }),
                target = $('head').length > 0 ? $('head') : $.root(),
                newCode = cheerio.load(code.join('\n'), { decodeEntities: false });

            async.each(tags, function (platform, callback) {
                async.forEachOf(platform, function (tag, selector, cb) {
                    if (options.replace) {
                        $(selector).remove();
                    } else if ($(selector).length) {
                        newCode(selector).remove();
                    }
                    return cb(null);
                }, function (error) {
                    return callback(error);
                });
            }, function (error) {
                target.append(newCode.html());
                return next(error, $.html().replace(/^\s*$[\n\r]{1,}/gm, ''));
            });
        }

        return {

            General: {
                background: function background(hex) {
                    print('General:background', 'Parsing colour ' + hex);
                    var rgba = color(hex).toRgb();

                    return Jimp.rgbaToInt(rgba.r, rgba.g, rgba.b, rgba.a * HEX_MAX);
                },
                source: function source(_source, callback) {
                    var sourceset = [];

                    print('General:source', 'Source type is ' + (typeof _source === 'undefined' ? 'undefined' : _typeof(_source)));
                    if (!_source || !_source.length) {
                        return callback('No source provided');
                    } else if (Buffer.isBuffer(_source)) {
                        sourceset = [{ size: sizeOf(_source), file: _source }];
                        return callback(sourceset.length ? null : 'Favicons source is invalid', sourceset);
                    } else if ((typeof _source === 'undefined' ? 'undefined' : _typeof(_source)) === 'object') {
                        async.each(_source, function (file, size, cb) {
                            return readFile(file, function (error, buffer) {
                                sourceset.push({
                                    size: { width: size, height: size, type: 'png' },
                                    file: buffer
                                });
                                return cb(error);
                            });
                        }, function (error) {
                            return callback(error || sourceset.length ? null : 'Favicons source is invalid');
                        }, sourceset);
                    } else if (typeof _source === 'string') {
                        readFile(_source, function (error, buffer) {
                            sourceset = [{ size: sizeOf(buffer), file: buffer }];
                            return callback(error || (sourceset.length ? null : 'Favicons source is invalid'), sourceset);
                        });
                    } else {
                        return callback('Invalid source type provided');
                    }
                },

                vinyl: function vinyl(object) {
                    return new File({
                        path: object.name,
                        contents: Buffer.isBuffer(object.contents) ? object.contents : new Buffer(object.contents)
                    });
                }
            },

            HTML: {
                parse: function parse(html, callback) {
                    print('HTML:parse', 'HTML found, parsing and modifying source');
                    var $ = cheerio.load(html),
                        link = $('*').is('link'),
                        attribute = link ? 'href' : 'content',
                        value = $('*').first().attr(attribute);

                    if (path.extname(value)) {
                        if (html.indexOf('og:image') !== NON_EXISTANT || html.indexOf('twitter:image') !== NON_EXISTANT) {
                            $('*').first().attr(attribute, absolute(value));
                        } else {
                            $('*').first().attr(attribute, relative(value));
                        }
                    } else if (value.slice(0, 1) === '#') {
                        $('*').first().attr(attribute, options.background);
                    } else if (html.indexOf('application-name') !== NON_EXISTANT || html.indexOf('apple-mobile-web-app-title') !== NON_EXISTANT) {
                        $('*').first().attr(attribute, options.appName);
                    }
                    return callback(null, $.html());
                },
                update: function update(document, code, tags, callback) {
                    var encoding = { encoding: 'utf8' };

                    async.waterfall([function (cb) {
                        return mkdirp(path.dirname(document), function (error) {
                            return cb(error);
                        });
                    }, function (cb) {
                        return fs.readFile(document, encoding, function (error, data) {
                            return cb(null, error ? null : data);
                        });
                    }, function (data, cb) {
                        if (data) {
                            updateDocument(data, code, tags, function (error, html) {
                                return cb(error, html);
                            });
                        } else {
                            return cb(null, code.join('\n'));
                        }
                    }, function (html, cb) {
                        return fs.writeFile(document, html, options, function (error) {
                            return cb(error);
                        });
                    }], function (error) {
                        return callback(error);
                    });
                }
            },

            Files: {
                create: function create(properties, name, callback) {
                    print('Files:create', 'Creating file: ' + name);
                    if (name === 'manifest.json') {
                        properties.name = options.appName;
                        properties.short_name = options.appName;
                        properties.display = options.display;
                        properties.orientation = options.orientation;
                        _.map(properties.icons, function (icon) {
                            return icon.src = relative(icon.src);
                        });
                        properties = JSON.stringify(properties, null, 2);
                    } else if (name === 'manifest.webapp') {
                        properties.version = options.version;
                        properties.name = options.appName;
                        properties.description = options.appDescription;
                        properties.developer.name = options.developerName;
                        properties.developer.url = options.developerURL;
                        _.map(properties.icons, function (property) {
                            return property = relative(property);
                        });
                        properties = JSON.stringify(properties, null, 2);
{'fraction_non_alphanumeric': 0.12782927355033413, 'fraction_numerical': 0.002155636990730761, 'mean_word_length': 1.2677502138579984, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3774285', 'n_tokens_mistral': 4545, 'n_tokens_neox': 4193, 'n_words': 942}