<commit_before>/*
 * grunt-require
 * https://github.com/asciidisco/grunt-requirejs
 *
 * Copyright (c) 2012 asciidisco
 * Licensed under the MIT license.
 */

module.exports = function (grunt) {
  'use strict';

  // Grunt utilities.
  var task = grunt.task;
  var file = grunt.file;
  var log = grunt.log;
  var verbose = grunt.verbose;
  var fail = grunt.fail;
  var config = grunt.config;
  var utils = typeof grunt.utils !== 'undefined' ? grunt.utils : grunt.util;
  var _ = utils._;

  // shortcuts
  var isArray = utils._.isArray;
  var isFunction = utils._.isFunction;
  var clone = utils._.clone;
  
  // lib dependencies
  var rjs = require('requirejs'); 
  var $ = require('cheerio');
  var fs = require('fs');

  // ==========================================================================
  // PRIVATE HELPER FUNCTIONS
  // ==========================================================================

  // runs the require js optimizer
  var optimize = function (config, done, cb) {
    return function (chgConfig) {
        grunt.helper('optimize', {
            config: typeof chgConfig === 'undefined' ? config : chgConfig,
            done: done,
            cb: cb
        });      
    };
  };

  // runs the almond js html file replacement
  var replaceAlmond = function (config, done, cb) {
    return function (chgConfig) {
        grunt.helper('replaceRequireWithAlmond', {
            config: typeof chgConfig === 'undefined' ? config : chgConfig,
            done: done,
            cb: cb
        });
    };
  };

  // ==========================================================================
  // TASKS
  // ==========================================================================

  grunt.registerTask('requirejs', 'Runs requirejs optimizer', function(mode) {
    var done = this.async(),
        rqConfig = config.get('requirejs');

    rqConfig = mode && rqConfig[mode] || rqConfig.js || rqConfig.css || rqConfig;

    // log process start
    log.ok('RequireJS optimizer started');

    // execute clear target helper
    grunt.helper('almond', {
      config: rqConfig,
      done: done,
      cb: optimize(rqConfig, done, replaceAlmond(rqConfig, done))
    });
    
    return done;
  });

  // ==========================================================================
  // HELPERS
  // ==========================================================================

  // helper to execute requirejs optimizer function
  grunt.registerHelper('optimize', function (options) {
    // call rjs optimizer 
    rjs.optimize(options.config, function (result) {
      // check if verbose flag is set, then log result
      grunt.verbose.ok(result);

      // log process end
      log.ok('RequireJS optimizer finished');

      // display sizes of modules
      if (isArray(options.config.modules) && options.config.dir) {
        options.config.modules.forEach(function (module) {
          try {
            // Query the entry
            var stats = fs.lstatSync(module._buildPath);

            // Is it a file
            if (stats.isFile()) {
                grunt.helper('require_size_info', module.name, options.config.optimize, grunt.file.read(module._buildPath, 'utf-8'));
            }
          }
          catch (e) {
            log.warn('Stats not available for module: ' + module.name);
          }

        });
      }

      // check for callback, else mark as done
      if (isFunction(options.cb)) {
        options.cb();
      } else {
        options.done();
      }

    });
  });

  // helper to execute requirejs optimizer function
  grunt.registerHelper('replaceRequireWithAlmond', function (options) {
    // check if we should replace require with almond in html files
    if (options.config.almond === true && isArray(options.config.replaceRequireScript)) {
      var $ = require('jQuery').create();

      // iterate over all modules that are configured for replacement
      options.config.replaceRequireScript.forEach(function (entry, idx) {
        var files = grunt.file.expand(entry.files);
        // log almond including
        log.ok('Replacing require script calls, with almond module files');

        // iterate over found html files
        files.forEach(function (file, index) {
          // load file contents
          var contents = String(grunt.file.read(file, 'utf-8'));
          // iterate over content nodes to find the correct script tags
          $(contents).each(function (idx, elm) {

            // check for require js like script tags
            if (String(elm.tagName).toLowerCase() === 'script' && $(elm).attr('data-main') !== '') {
              // replace the attributes of requires script tag
              // with the 'almonded' version of the module
              var $newElm = $(elm).clone(), newElm = $newElm[0].outerHTML;
              var insertScript = _.isUndefined(entry.modulePath) !== true ? entry.modulePath : $newElm.attr('data-main');
              contents = contents.replace($.trim(newElm.replace('<script', '').replace('></script>', '')), 'src="' + insertScript + '.js"');
            }

          });

          // write out newly created file contents
          grunt.file.write(file, contents, 'utf-8');
        });
      });<commit_msg>Removed jQuery dependency<commit_after>/*
 * grunt-require
 * https://github.com/asciidisco/grunt-requirejs
 *
 * Copyright (c) 2012 asciidisco
 * Licensed under the MIT license.
 */

module.exports = function (grunt) {
  'use strict';

  // Grunt utilities.
  var task = grunt.task;
  var file = grunt.file;
  var log = grunt.log;
  var verbose = grunt.verbose;
  var fail = grunt.fail;
  var config = grunt.config;
  var utils = typeof grunt.utils !== 'undefined' ? grunt.utils : grunt.util;
  var _ = utils._;

  // shortcuts
  var isArray = utils._.isArray;
  var isFunction = utils._.isFunction;
  var clone = utils._.clone;
  
  // lib dependencies
  var rjs = require('requirejs'); 
  var $ = require('cheerio');
  var fs = require('fs');

  // ==========================================================================
  // PRIVATE HELPER FUNCTIONS
  // ==========================================================================

  // runs the require js optimizer
  var optimize = function (config, done, cb) {
    return function (chgConfig) {
        grunt.helper('optimize', {
            config: typeof chgConfig === 'undefined' ? config : chgConfig,
            done: done,
            cb: cb
        });      
    };
  };

  // runs the almond js html file replacement
  var replaceAlmond = function (config, done, cb) {
    return function (chgConfig) {
        grunt.helper('replaceRequireWithAlmond', {
            config: typeof chgConfig === 'undefined' ? config : chgConfig,
            done: done,
            cb: cb
        });
    };
  };

  // ==========================================================================
  // TASKS
  // ==========================================================================

  grunt.registerTask('requirejs', 'Runs requirejs optimizer', function(mode) {
    var done = this.async(),
        rqConfig = config.get('requirejs');

    rqConfig = mode && rqConfig[mode] || rqConfig.js || rqConfig.css || rqConfig;

    // log process start
    log.ok('RequireJS optimizer started');

    // execute clear target helper
    grunt.helper('almond', {
      config: rqConfig,
      done: done,
      cb: optimize(rqConfig, done, replaceAlmond(rqConfig, done))
    });
    
    return done;
  });

  // ==========================================================================
  // HELPERS
  // ==========================================================================

  // helper to execute requirejs optimizer function
  grunt.registerHelper('optimize', function (options) {
    // call rjs optimizer 
    rjs.optimize(options.config, function (result) {
      // check if verbose flag is set, then log result
      grunt.verbose.ok(result);

      // log process end
      log.ok('RequireJS optimizer finished');

      // display sizes of modules
      if (isArray(options.config.modules) && options.config.dir) {
        options.config.modules.forEach(function (module) {
          try {
            // Query the entry
            var stats = fs.lstatSync(module._buildPath);

            // Is it a file
            if (stats.isFile()) {
                grunt.helper('require_size_info', module.name, options.config.optimize, grunt.file.read(module._buildPath, 'utf-8'));
            }
          }
          catch (e) {
            log.warn('Stats not available for module: ' + module.name);
          }

        });
      }

      // check for callback, else mark as done
      if (isFunction(options.cb)) {
        options.cb();
      } else {
        options.done();
      }

    });
  });

  // helper to execute requirejs optimizer function
  grunt.registerHelper('replaceRequireWithAlmond', function (options) {
    // check if we should replace require with almond in html files
    if (options.config.almond === true && isArray(options.config.replaceRequireScript)) {
      var $ = require('cheerio');

      // iterate over all modules that are configured for replacement
      options.config.replaceRequireScript.forEach(function (entry, idx) {
        var files = grunt.file.expand(entry.files);
        // log almond including
        log.ok('Replacing require script calls, with almond module files');

        // iterate over found html files
        files.forEach(function (file, index) {
          // load file contents
          var contents = String(grunt.file.read(file, 'utf-8'));
          // iterate over content nodes to find the correct script tags
          $(contents).each(function (idx, elm) {

            // check for require js like script tags
            if (String(elm.tagName).toLowerCase() === 'script' && $(elm).attr('data-main') !== '') {
              // replace the attributes of requires script tag
              // with the 'almonded' version of the module
              var $newElm = $(elm).clone(), newElm = $newElm[0].outerHTML;
              var insertScript = _.isUndefined(entry.modulePath) !== true ? entry.modulePath : $newElm.attr('data-main');
              contents = contents.replace($.trim(newElm.replace('<script', '').replace('></script>', '')), 'src="' + insertScript + '.js"');
            }

          });

          // write out newly created file contents
          grunt.file.write(file, contents, 'utf-8');
        });
      });
{'fraction_non_alphanumeric': 0.20462768996381642, 'fraction_numerical': 0.001523519329651495, 'mean_word_length': 2.5150602409638556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6110480', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2765, 'n_words': 742}