<commit_before>'use strict';

var minify = require('html-minifier').minify;
var pathUtils = require('path');
var fs = require('fs');

module.exports = function() {
    Html2Js.prototype.brunchPlugin = true;
    Html2Js.prototype.type = 'template';
    Html2Js.prototype.extension = 'html';

    Html2Js.prototype.compile = function(content, path, callback) {
        var options = this.options;
        var moduleName = normalizePath(pathUtils.relative(options.base, path));
        this.moduleNames.push("'" + moduleName + "'");

        if (options.target === 'js') {
            return callback(null, compileTemplate(moduleName, content, options.quoteChar, options.indentString, options.useStrict, options.htmlmin));
        } else if (options.target === 'coffee') {
            return callback(null, compileCoffeeTemplate(moduleName, content, options.quoteChar, options.indentString, options.htmlmin));
        } else {
            return callback('Unknown target "' + options.target + '" specified');
        }
    };

    Html2Js.prototype.onCompile = function(generatedFiles) {
        var bundle = '';
        var options = this.options;
        var joinToKeys = Object.keys(this.joinTo);        

        for (var i = 0; i < joinToKeys.length; i++) {
            var path = this.publicPath + pathUtils.sep + joinToKeys[i];
            var targetModule = options.module || pathUtils.basename(path, '.js');
            bundle = "angular.module('" + targetModule + "', [" + this.moduleNames.join(', ') + "])";
            if (options.target === 'js') {
                bundle += ';';
            }

            bundle += '\n\n' + fs.readFileSync(path);

            fs.writeFile(path, bundle, function(err) {
                if (err) throw err;
            });
        }
    }

    function Html2Js(cfg) {        
        cfg = cfg || {};
        this.options = {
            base: 'src',
            module: '',
            quoteChar: '"',
            fileHeaderString: '',
            fileFooterString: '',
            indentString: '    ',
            target: 'js',
            htmlmin: {}
        };
        this.joinTo = cfg.files ? cfg.files.templates.joinTo : null;
        this.publicPath = cfg.paths ? cfg.paths.public : null;
        this.moduleNames = [];

        var config = cfg.plugins && cfg.plugins.html2js;
        if (config) {
            var options = config.options || {};

            for (var key in options) {
                if (options.hasOwnProperty(key)) {
                    this.options[key] = options[key];
                }
            }
        }
    };

    function escapeContent(content, quoteChar, indentString) {
        var bsRegexp = new RegExp('\\\\', 'g');
        var quoteRegexp = new RegExp('\\' + quoteChar, 'g');
        var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar;
        return content.replace(bsRegexp, '\\\\').replace(quoteRegexp, '\\' + quoteChar).replace(/\r?\n/g, nlReplace);
    }

    // convert Windows file separator URL path separator
    function normalizePath(p) {
        if (pathUtils.sep !== '/') {
            p = p.replace(/\\/g, '/');
        }
        return p;
    }

    function getContent(content, quoteChar, indentString, htmlmin) {
        if (Object.keys(htmlmin).length) {            
            content = minify(content, htmlmin);            
        }

        return escapeContent(content, quoteChar, indentString);
    }

    // compile a template to an angular module<commit_msg>Fixed constant prepending of angular.module when auto-reloads occur<commit_after>'use strict';

var minify = require('html-minifier').minify;
var pathUtils = require('path');
var fs = require('fs');

module.exports = function() {
    Html2Js.prototype.brunchPlugin = true;
    Html2Js.prototype.type = 'template';
    Html2Js.prototype.extension = 'html';

    Html2Js.prototype.compile = function(content, path, callback) {
        var options = this.options;
        var moduleName = normalizePath(pathUtils.relative(options.base, path));

        //Hack to not include any files not in the app dir since brunch passes in every file matching the extension
        if (moduleName.indexOf('..') == -1) {
            this.moduleNames.push("'" + moduleName + "'");

            if (options.target === 'js') {
                return callback(null, compileTemplate(moduleName, content, options.quoteChar, options.indentString, options.useStrict, options.htmlmin));
            } else if (options.target === 'coffee') {
                return callback(null, compileCoffeeTemplate(moduleName, content, options.quoteChar, options.indentString, options.htmlmin));
            } else {
                return callback('Unknown target "' + options.target + '" specified');
            }
        }

        return callback(null, null);
    };

    Html2Js.prototype.onCompile = function(generatedFiles) {
        var bundle = '';
        var options = this.options;
        var joinToKeys = Object.keys(this.joinTo);

        for (var i = 0; i < joinToKeys.length; i++) {
            var path = this.publicPath + pathUtils.sep + joinToKeys[i];
            var targetModule = options.module || pathUtils.basename(path, '.js');
            bundle = "angular.module('" + targetModule + "', [" + this.moduleNames.join(', ') + "])";
            if (options.target === 'js') {
                bundle += ';';
            }

            bundle += '\n\n';

            var fileContent = fs.readFileSync(path, {encoding: 'utf-8'});            

            if (fileContent.indexOf(bundle) == -1) {
                fs.writeFile(path, bundle.concat(fileContent), function(err) {
                    if (err) throw err;
                });
            }
        }
    }

    function Html2Js(cfg) {
        cfg = cfg || {};
        this.options = {
            base: 'src',
            module: '',
            quoteChar: '"',
            fileHeaderString: '',
            fileFooterString: '',
            indentString: '    ',
            target: 'js',
            htmlmin: {}
        };
        this.joinTo = cfg.files ? cfg.files.templates.joinTo : null;
        this.publicPath = cfg.paths ? cfg.paths.public : null;
        this.moduleNames = [];

        var config = cfg.plugins && cfg.plugins.html2js;
        if (config) {
            var options = config.options || {};

            for (var key in options) {
                if (options.hasOwnProperty(key)) {
                    this.options[key] = options[key];
                }
            }
        }
    };

    function escapeContent(content, quoteChar, indentString) {
        var bsRegexp = new RegExp('\\\\', 'g');
        var quoteRegexp = new RegExp('\\' + quoteChar, 'g');
        var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar;
        return content.replace(bsRegexp, '\\\\').replace(quoteRegexp, '\\' + quoteChar).replace(/\r?\n/g, nlReplace);
    }

    // convert Windows file separator URL path separator

    function normalizePath(p) {
        if (pathUtils.sep !== '/') {
            p = p.replace(/\\/g, '/');
        }
        return p;
    }

    function getContent(content, quoteChar, indentString, htmlmin) {
        if (Object.keys(htmlmin).length) {
            content = minify(content, htmlmin);
        }

        return escapeContent(content, quoteChar, indentString);
    }

    // compile a template to an angular module

{'fraction_non_alphanumeric': 0.14040037619239554, 'fraction_numerical': 0.00255273411258901, 'mean_word_length': 1.9657370517928288, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6379636', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2034, 'n_words': 461}