<commit_before>'use strict';

module.exports = function (inputFile, outputFile, options) {
    var fs = require('fs');
    var _ = require('underscore');
    var colorObj = require('color');

    function sortColors(colors) {
        colors.sort(function (a, b) {
            return colorObj(a).hue() - colorObj(b).hue();
        });
    }

    function toHtml(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);
        var template = fs.readFileSync(__dirname+'/templates/html.tpl', 'utf8');
        var render = _.template(template);

        sortColors(colors);

        return render({ colors: colors, colorObj: colorObj });
    }

    function toList(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);

        sortColors(colors);

        return colors.join('\n');
    }

    function toJson(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);

        sortColors(colors);

        return JSON.stringify(colors);
    }

    function toCss(data) {
        var postcss = require('postcss');
        var colorsOnly = require('postcss-colors-only');
        var css = postcss(colorsOnly(options)).process(data).css;

        return css.toString();
    }

    function processInput(input) {
        var output;

        if (outputFile && typeof options.format === 'undefined') {
            var path = require('path');
            var extension = path.extname(outputFile);

            switch (extension) {
                case '.css':
                case '.sass':
                case '.scss':
                case '.less':
                    options.format = 'css';
                    break;
                case '.json':
                case '.js':
                    options.format = 'json';
                    break;
                case '.html':
                case '.htm':
                    options.format = 'html';
                    break;
                default:
                    break;
            }
        }

        switch (options.format) {
            case 'json':
                output = toJson(input);
                break;
            case 'css':
                output = toCss(input);
                break;
            case 'html':
                output = toHtml(input);
                break;
            default:
                output = toList(input);
                break;
        }

        if (outputFile) {
            fs.writeFileSync(outputFile, output);
        } else {
            console.log(output);
        }
    }

    function processStdin() {
        var data = '';

        process.stdin.setEncoding('utf8');

        process.stdin.on('readable', function () {
            var chunk = process.stdin.read();

            if (chunk !== null) {
                data += chunk;
            }
        });

        process.stdin.on('end', function () {
            if (!data) {
                throw new Error('No input specified.');
            }

            processInput(data);
        });
    }

    function processInputFile() {
        if (!inputFile) {
            throw new Error('No input specified.');
        }

        fs.readFile(inputFile, 'utf8', function (err, data) {
            if (err) {
                throw new Error(err);
            }

            processInput(data);
        });
    }

    if (!process.stdin.isTTY) {
        processStdin();
    } else {
        processInputFile();
    }
};
<commit_msg>Use path.join instead of string concatenation<commit_after>'use strict';

module.exports = function (inputFile, outputFile, options) {
    var fs = require('fs');
    var path = require('path');
    var _ = require('underscore');
    var colorObj = require('color');

    function sortColors(colors) {
        colors.sort(function (a, b) {
            return colorObj(a).hue() - colorObj(b).hue();
        });
    }

    function toHtml(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);
        var filePath = path.join(__dirname, '/templates/html.tpl');
        var template = fs.readFileSync(filePath, 'utf8');
        var render = _.template(template);

        sortColors(colors);

        return render({ colors: colors, colorObj: colorObj });
    }

    function toList(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);

        sortColors(colors);

        return colors.join('\n');
    }

    function toJson(data) {
        var extractor = require('css-color-extractor');
        var colors = extractor.fromCss(data, options);

        sortColors(colors);

        return JSON.stringify(colors);
    }

    function toCss(data) {
        var postcss = require('postcss');
        var colorsOnly = require('postcss-colors-only');
        var css = postcss(colorsOnly(options)).process(data).css;

        return css.toString();
    }

    function processInput(input) {
        var output;

        if (outputFile && typeof options.format === 'undefined') {
            var extension = path.extname(outputFile);

            switch (extension) {
                case '.css':
                case '.sass':
                case '.scss':
                case '.less':
                    options.format = 'css';
                    break;
                case '.json':
                case '.js':
                    options.format = 'json';
                    break;
                case '.html':
                case '.htm':
                    options.format = 'html';
                    break;
                default:
                    break;
            }
        }

        switch (options.format) {
            case 'json':
                output = toJson(input);
                break;
            case 'css':
                output = toCss(input);
                break;
            case 'html':
                output = toHtml(input);
                break;
            default:
                output = toList(input);
                break;
        }

        if (outputFile) {
            fs.writeFileSync(outputFile, output);
        } else {
            console.log(output);
        }
    }

    function processStdin() {
        var data = '';

        process.stdin.setEncoding('utf8');

        process.stdin.on('readable', function () {
            var chunk = process.stdin.read();

            if (chunk !== null) {
                data += chunk;
            }
        });

        process.stdin.on('end', function () {
            if (!data) {
                throw new Error('No input specified.');
            }

            processInput(data);
        });
    }

    function processInputFile() {
        if (!inputFile) {
            throw new Error('No input specified.');
        }

        fs.readFile(inputFile, 'utf8', function (err, data) {
            if (err) {
                throw new Error(err);
            }

            processInput(data);
        });
    }

    if (!process.stdin.isTTY) {
        processStdin();
    } else {
        processInputFile();
    }
};

{'fraction_non_alphanumeric': 0.12436092303440652, 'fraction_numerical': 0.0008290728202293769, 'mean_word_length': 1.4150817484150817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5535800', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1867, 'n_words': 417}