<commit_before>/*
 * grunt-favicons
 * https://github.com/gleero/grunt-favicons
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

var path = require('path');
var fs = require('fs');
var execSync = require("execSync");

module.exports = function(grunt) {

    // Tasks
    grunt.registerMultiTask('favicons', 'Generate favicon.ico and icons for iOS, Android, WP8 and Firefox (OS)', function() {

        var target = this.target;

        // Default options
        var options = this.options({
            debug: false,
            trueColor: false,
            precomposed: true,
            HTMLPrefix: "",
            appleTouchBackgroundColor: "auto", // none, auto, #color
            appleTouchPadding: 15,
            windowsTile: true,
            coast: false,
            sharp: 0,
            tileBlackWhite: true,
            tileColor: "auto", // none, auto, #color
            firefox: false,
            apple: true,
            regular: true,
            firefoxRound: false,
            firefoxManifest: "",
            androidHomescreen: false
        });

        // Execute external command
        var execute = function(cmd) {
            if (options.debug) {
                console.log("\n\033[37m" + cmd + "\033[0m");
            }
            return execSync.exec(cmd);
        };

        // Convert image with imagemagick
        var convert = function(args) {
            args.unshift("convert");
            var ret = execute(args.join(" "));
            if (ret.code === 127) {
                return grunt.warn(
                    'You need to have ImageMagick installed in your PATH for this task to work.'
                );
            }
        };

        // Generate background color for apple touch icons
        var generateColor = function(src) {
            var ret = execute("convert " + src + " -polaroid 180 -resize 1x1 -colors 1 -alpha off -unique-colors txt:- | grep -v ImageMagick | sed -n 's/.*\\(#[0-9A-F]*\\).*/\\1/p'");
            return ret.stdout.trim();
        };

        // Generate background color for windows 8 tile
        var generateTileColor = function(src) {
            var ret = execute("convert " + src + " +dither -colors 1 -alpha off -unique-colors txt:- | grep -v ImageMagick | sed -n 's/.*\\(#[0-9A-F]*\\).*/\\1/p'");
            return ret.stdout.trim();
        };

        var combine = function(src, dest, size, fname, additionalOpts, padding) {
            var out = [
                src,
                "-resize",
                size
            ].concat(additionalOpts);
            if (options.sharp > 0) {
                out = out.concat([
                    "-adaptive-sharpen",
                    options.sharp + "x" + options.sharp
                ]);
            }
            // icon padding
            if (typeof(padding)==='number' && padding >= 0 && padding < 100) {
                var thumb = Math.round((100 - padding) * parseInt(size.split("x")[0], 10) / 100);
                out = out.concat([
                    "-gravity",
                    "center",
                    "-thumbnail",
                    "\"" + thumb + "x" + thumb + ">" + "\"",
                    "-extent",
                    size
                ]);
            }
            out.push(path.join(dest, fname));
            return out;
        };



        // Append all icons to HTML as meta tags (needs cheerio)
        var needHTML = options.html !== undefined && options.html !== "";

        if (needHTML) {
            var cheerio = require("cheerio");
            var contents = (grunt.file.exists(options.html)) ? grunt.file.read(options.html) : "";
            var $ = cheerio.load(contents);
            // Removing exists favicon from HTML
            $('link[rel="shortcut icon"]').remove();
            $('link[rel="icon"]').remove();
            $('link[rel="apple-touch-icon"]').remove();
            $('link[rel="apple-touch-icon-precomposed"]').remove();
            $('meta').each(function(i, elem) {
                var name = $(this).attr('name');
                if(name && (name === 'msapplication-TileImage' ||
                            name === 'msapplication-TileColor' ||
                            name.indexOf('msapplication-square') >= 0)) {
                    $(this).remove();
                }
            });
            var html = $.html().replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
            if(html === '') {
                $ = cheerio.load('');
            }
        }

        // Iterate over all specified file groups.
        this.files.forEach(function(f) {

            if (f.src.length === 0) {
                return grunt.warn ('Source file not found.');
            }

            if (!grunt.file.isDir(f.dest)) {
                grunt.file.mkdir(f.dest);
                grunt.log.ok('Created output folder at "', f.dest, '"');
            }

            // Iterate source files
            f.src.forEach(function(source) {

                var resolmap = {};

                // Create resized version of source image
                // 16x16: desktop browsers, address bar, tabs
                // 32x32: safari reading list, non-retina iPhone, windows 7+ taskbar
                // 48x48: windows desktop

                var files = [];
                var ext = path.extname(source);
                var basename = path.basename(source, ext);
                var dirname = path.dirname(source);
                var prefix = options.precomposed ? "-precomposed" : "";
                var additionalOpts = options.appleTouchBackgroundColor !== "none" ?
                    [ "-background", '"' + options.appleTouchBackgroundColor + '"', "-flatten"] : [];
                grunt.log.write('Resizing images for "' + source + '"... ');

                if (options.regular) {

                    // regular png
                    ['16x16', '32x32', '48x48'].forEach(function(size) {
                        var p = path.join(dirname, basename + "." + size + ext);
                        var saveTo = path.join(f.dest, size + '.png');
                        var src = source;
                        if (fs.existsSync(p)) {
                            src = p;
                        }
                        convert([src, '-resize', size, saveTo]);
                        files.push(saveTo);
                    });
                    grunt.log.ok();

                    // favicon.ico
                    grunt.log.write('favicon.ico... ');
                    convert(files.concat([
                        "-alpha on",
                        "-background none",
                        options.trueColor ? "" : "-bordercolor white -border 0 -colors 64",
                        path.join(f.dest, 'favicon.ico')
                    ]));
                    grunt.log.ok();

                    // 64x64 favicon.png higher priority than .ico
                    grunt.log.write('favicon.png... ');
                    convert([source, '-resize', "64x64", path.join(f.dest, 'favicon.png')]);
                    grunt.log.ok();
                }

                ////// PNG's for iOS and Android icons

                if (options.apple) {
                    // Convert options for transparent and flatten
                    if (options.appleTouchBackgroundColor === "auto") {
                        options.appleTouchBackgroundColor = generateColor(source);
                    }

                    // 57x57: iPhone non-retina, Android 2.1+
                    grunt.log.write('apple-touch-icon.png... ');
                    convert(combine(source, f.dest, "57x57", "apple-touch-icon.png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();

                    if (options.precomposed) {
                        grunt.log.write('apple-touch-icon' + prefix + '.png... ');
                        convert(combine(source, f.dest, "57x57", "apple-touch-icon" + prefix + ".png", additionalOpts, options.appleTouchPadding));
                        grunt.log.ok();
                    }

                    // 60x60: iPhone iOS 7 without size
                    grunt.log.write('apple-touch-icon-60x60-precomposed.png... ');
                    convert(combine(source, f.dest, "60x60", "apple-touch-icon-60x60-precomposed.png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();

                    // 72x72: iPad non-retina, iOS 6 and lower
                    grunt.log.write('apple-touch-icon-72x72' + prefix + '.png... ');
                    convert(combine(source, f.dest, "72x72", "apple-touch-icon-72x72" + prefix + ".png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();<commit_msg>generateColor for appleTouchBackgroundColor called too late<commit_after>/*
 * grunt-favicons
 * https://github.com/gleero/grunt-favicons
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

var path = require('path');
var fs = require('fs');
var execSync = require("execSync");

module.exports = function(grunt) {

    // Tasks
    grunt.registerMultiTask('favicons', 'Generate favicon.ico and icons for iOS, Android, WP8 and Firefox (OS)', function() {

        var target = this.target;

        // Default options
        var options = this.options({
            debug: false,
            trueColor: false,
            precomposed: true,
            HTMLPrefix: "",
            appleTouchBackgroundColor: "auto", // none, auto, #color
            appleTouchPadding: 15,
            windowsTile: true,
            coast: false,
            sharp: 0,
            tileBlackWhite: true,
            tileColor: "auto", // none, auto, #color
            firefox: false,
            apple: true,
            regular: true,
            firefoxRound: false,
            firefoxManifest: "",
            androidHomescreen: false
        });

        // Execute external command
        var execute = function(cmd) {
            if (options.debug) {
                console.log("\n\033[37m" + cmd + "\033[0m");
            }
            return execSync.exec(cmd);
        };

        // Convert image with imagemagick
        var convert = function(args) {
            args.unshift("convert");
            var ret = execute(args.join(" "));
            if (ret.code === 127) {
                return grunt.warn(
                    'You need to have ImageMagick installed in your PATH for this task to work.'
                );
            }
        };

        // Generate background color for apple touch icons
        var generateColor = function(src) {
            var ret = execute("convert " + src + " -polaroid 180 -resize 1x1 -colors 1 -alpha off -unique-colors txt:- | grep -v ImageMagick | sed -n 's/.*\\(#[0-9A-F]*\\).*/\\1/p'");
            return ret.stdout.trim();
        };

        // Generate background color for windows 8 tile
        var generateTileColor = function(src) {
            var ret = execute("convert " + src + " +dither -colors 1 -alpha off -unique-colors txt:- | grep -v ImageMagick | sed -n 's/.*\\(#[0-9A-F]*\\).*/\\1/p'");
            return ret.stdout.trim();
        };

        var combine = function(src, dest, size, fname, additionalOpts, padding) {
            var out = [
                src,
                "-resize",
                size
            ].concat(additionalOpts);
            if (options.sharp > 0) {
                out = out.concat([
                    "-adaptive-sharpen",
                    options.sharp + "x" + options.sharp
                ]);
            }
            // icon padding
            if (typeof(padding)==='number' && padding >= 0 && padding < 100) {
                var thumb = Math.round((100 - padding) * parseInt(size.split("x")[0], 10) / 100);
                out = out.concat([
                    "-gravity",
                    "center",
                    "-thumbnail",
                    "\"" + thumb + "x" + thumb + ">" + "\"",
                    "-extent",
                    size
                ]);
            }
            out.push(path.join(dest, fname));
            return out;
        };



        // Append all icons to HTML as meta tags (needs cheerio)
        var needHTML = options.html !== undefined && options.html !== "";

        if (needHTML) {
            var cheerio = require("cheerio");
            var contents = (grunt.file.exists(options.html)) ? grunt.file.read(options.html) : "";
            var $ = cheerio.load(contents);
            // Removing exists favicon from HTML
            $('link[rel="shortcut icon"]').remove();
            $('link[rel="icon"]').remove();
            $('link[rel="apple-touch-icon"]').remove();
            $('link[rel="apple-touch-icon-precomposed"]').remove();
            $('meta').each(function(i, elem) {
                var name = $(this).attr('name');
                if(name && (name === 'msapplication-TileImage' ||
                            name === 'msapplication-TileColor' ||
                            name.indexOf('msapplication-square') >= 0)) {
                    $(this).remove();
                }
            });
            var html = $.html().replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');
            if(html === '') {
                $ = cheerio.load('');
            }
        }

        // Iterate over all specified file groups.
        this.files.forEach(function(f) {

            if (f.src.length === 0) {
                return grunt.warn ('Source file not found.');
            }

            if (!grunt.file.isDir(f.dest)) {
                grunt.file.mkdir(f.dest);
                grunt.log.ok('Created output folder at "', f.dest, '"');
            }

            // Iterate source files
            f.src.forEach(function(source) {

                var resolmap = {};

                // Create resized version of source image
                // 16x16: desktop browsers, address bar, tabs
                // 32x32: safari reading list, non-retina iPhone, windows 7+ taskbar
                // 48x48: windows desktop

                var files = [];
                var ext = path.extname(source);
                var basename = path.basename(source, ext);
                var dirname = path.dirname(source);
                var prefix = options.precomposed ? "-precomposed" : "";

                // Convert options for transparent and flatten
                if (options.appleTouchBackgroundColor === "auto") {
                    options.appleTouchBackgroundColor = generateColor(source);
                }

                var additionalOpts = options.appleTouchBackgroundColor !== "none" ?
                    [ "-background", '"' + options.appleTouchBackgroundColor + '"', "-flatten"] : [];
                grunt.log.write('Resizing images for "' + source + '"... ');

                if (options.regular) {

                    // regular png
                    ['16x16', '32x32', '48x48'].forEach(function(size) {
                        var p = path.join(dirname, basename + "." + size + ext);
                        var saveTo = path.join(f.dest, size + '.png');
                        var src = source;
                        if (fs.existsSync(p)) {
                            src = p;
                        }
                        convert([src, '-resize', size, saveTo]);
                        files.push(saveTo);
                    });
                    grunt.log.ok();

                    // favicon.ico
                    grunt.log.write('favicon.ico... ');
                    convert(files.concat([
                        "-alpha on",
                        "-background none",
                        options.trueColor ? "" : "-bordercolor white -border 0 -colors 64",
                        path.join(f.dest, 'favicon.ico')
                    ]));
                    grunt.log.ok();

                    // 64x64 favicon.png higher priority than .ico
                    grunt.log.write('favicon.png... ');
                    convert([source, '-resize', "64x64", path.join(f.dest, 'favicon.png')]);
                    grunt.log.ok();
                }

                ////// PNG's for iOS and Android icons

                if (options.apple) {
                    // 57x57: iPhone non-retina, Android 2.1+
                    grunt.log.write('apple-touch-icon.png... ');
                    convert(combine(source, f.dest, "57x57", "apple-touch-icon.png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();

                    if (options.precomposed) {
                        grunt.log.write('apple-touch-icon' + prefix + '.png... ');
                        convert(combine(source, f.dest, "57x57", "apple-touch-icon" + prefix + ".png", additionalOpts, options.appleTouchPadding));
                        grunt.log.ok();
                    }

                    // 60x60: iPhone iOS 7 without size
                    grunt.log.write('apple-touch-icon-60x60-precomposed.png... ');
                    convert(combine(source, f.dest, "60x60", "apple-touch-icon-60x60-precomposed.png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();

                    // 72x72: iPad non-retina, iOS 6 and lower
                    grunt.log.write('apple-touch-icon-72x72' + prefix + '.png... ');
                    convert(combine(source, f.dest, "72x72", "apple-touch-icon-72x72" + prefix + ".png", additionalOpts, options.appleTouchPadding));
                    grunt.log.ok();
{'fraction_non_alphanumeric': 0.14172780526645054, 'fraction_numerical': 0.015241995108912018, 'mean_word_length': 1.3268492788143442, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '3663285', 'n_tokens_mistral': 4965, 'n_tokens_neox': 4513, 'n_words': 1032}