<commit_before>/*
 * grunt-build-html
 * https://github.com/tonai/grunt-build-html
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */
 
'use strict';

module.exports = function(grunt) {
  var path      = require('path');
  var _         = grunt.util._;
  var debug     = grunt.log.debug;
  var backtrace = function(files) {
    var message = '[backtrace] : ';
    for (var i in files) {
      message += grunt.util.linefeed + files[i];
    }
    debug(grunt.util.normalizelf(message));
  };

  grunt.registerMultiTask('build_html', 'Build HTML templates recursively.', function() {
    var include          = null;
    var templates        = {};
    var globalData       = {};
    var templateSettings = {};

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      templates: []
    });

    // Manage template settings.
    if (options.interpolate !== undefined) {
      templateSettings.interpolate = options.interpolate;
    }
    if (options.escape !== undefined) {
      templateSettings.escape = options.escape;
    }
    if (options.evaluate !== undefined) {
      templateSettings.evaluate = options.evaluate;
    }

    // Read JSON data.
    if (options.data !== undefined) {
      globalData = grunt.file.readJSON(options.data);
    }

    // Include method to be used in HTML files.
    include = function(tplName, data) {
      var files, templateData, html = '';
      if (typeof data == 'undefined') {
        data = {};
      }
      data = _.extend(globalData, data);

      if (_.has(templates, tplName)) {
        files = _.clone(this.files);
        files.push(templates[tplName].filepath);
        templateData = {files: files};
        data.include = include.bind(templateData);
        try {
          html = _.template(templates[tplName].content, data, templateSettings);
        } catch (error) {
          grunt.log.error(error.message);
          backtrace(files);
        }
      } else {
        grunt.log.writeln('Template "' + tplName + '" does not exists.');
        backtrace(this.files);
      }
      
      return html;
    };

    // Process templates.
    _.each(grunt.file.expand(options.templates), function(tpl) {
      var tplName = path.basename(tpl, path.extname(tpl)).replace(/[\s]/g, '_');
      debug("loading template :", tplName);
      debug("  path is :", tpl);
      templates[tplName] = {
        content: grunt.file.read(tpl),
        filepath: tpl
      };
    });

    // Iterate over all specified file groups.
    this.files.forEach(function(file) {
      // Concat specified files.
      var src = file.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        var html = '';
        var templateData = {files: [filepath]};
        templateData.include = include.bind(templateData);
        options.filename = filepath;
        try {
          html = _.template(grunt.file.read(filepath), templateData, templateSettings);
        } catch (error) {
          grunt.log.error(error.message);
          backtrace(templateData.files);
        }
        return html;
      }).join(grunt.util.normalizelf(grunt.util.linefeed));

      // Write the destination file.
      grunt.file.write(file.dest, src);

      // Print a success message.
      grunt.log.ok('Build HTML file to "' + file.dest + '"');
    });
  });

};
<commit_msg>Global data available into files (not only in partials).<commit_after>/*
 * grunt-build-html
 * https://github.com/tonai/grunt-build-html
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */
 
'use strict';

module.exports = function(grunt) {
  var path      = require('path');
  var _         = grunt.util._;
  var debug     = grunt.log.debug;
  var backtrace = function(files) {
    var message = '[backtrace] : ';
    for (var i in files) {
      message += grunt.util.linefeed + files[i];
    }
    debug(grunt.util.normalizelf(message));
  };

  grunt.registerMultiTask('build_html', 'Build HTML templates recursively.', function() {
    var include          = null;
    var templates        = {};
    var globalData       = {};
    var templateSettings = {};

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      templates: []
    });

    // Manage template settings.
    if (options.interpolate !== undefined) {
      templateSettings.interpolate = options.interpolate;
    }
    if (options.escape !== undefined) {
      templateSettings.escape = options.escape;
    }
    if (options.evaluate !== undefined) {
      templateSettings.evaluate = options.evaluate;
    }

    // Read JSON data.
    if (options.data !== undefined) {
      globalData = grunt.file.readJSON(options.data);
    }

    // Include method to be used in HTML files.
    include = function(tplName, data) {
      var files, templateData, html = '';
      if (typeof data == 'undefined') {
        data = {};
      }
      data = _.extend(globalData, data);

      if (_.has(templates, tplName)) {
        files = _.clone(this.files);
        files.push(templates[tplName].filepath);
        templateData = {files: files};
        data.include = include.bind(templateData);
        try {
          html = _.template(templates[tplName].content, data, templateSettings);
        } catch (error) {
          grunt.log.error(error.message);
          backtrace(files);
        }
      } else {
        grunt.log.writeln('Template "' + tplName + '" does not exists.');
        backtrace(this.files);
      }
      
      return html;
    };

    // Process templates.
    _.each(grunt.file.expand(options.templates), function(tpl) {
      var tplName = path.basename(tpl, path.extname(tpl)).replace(/[\s]/g, '_');
      debug("loading template :", tplName);
      debug("  path is :", tpl);
      templates[tplName] = {
        content: grunt.file.read(tpl),
        filepath: tpl
      };
    });

    // Iterate over all specified file groups.
    this.files.forEach(function(file) {
      // Concat specified files.
      var src = file.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        var html = '';
        var templateData = _.extend(globalData, {files: [filepath]});
        templateData.include = include.bind(templateData);
        options.filename = filepath;
        try {
          html = _.template(grunt.file.read(filepath), templateData, templateSettings);
        } catch (error) {
          grunt.log.error(error.message);
          backtrace(templateData.files);
        }
        return html;
      }).join(grunt.util.normalizelf(grunt.util.linefeed));

      // Write the destination file.
      grunt.file.write(file.dest, src);

      // Print a success message.
      grunt.log.ok('Build HTML file to "' + file.dest + '"');
    });
  });

};

{'fraction_non_alphanumeric': 0.13492167961037252, 'fraction_numerical': 0.0010530472554955903, 'mean_word_length': 2.0055379746835444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1028084', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2172, 'n_words': 485}