<commit_before>var Plugin = require('./Plugin');
var path = require('path');
var extend = require('extend');
var HOOKS = require('../hooks');
var TemplateCompiler = require('../modules/TemplateCompiler');
var interpolateName = require('../utils/interpolateName');
var emitFile = require('../utils/emitFile');
var DocPage = require('../data/Page');
var Promise = require('bluebird');


var defaultOptions = {
  template: null,
  filename: 'docs/[path][name].[ext]/index.html',
  context: {}
};

var PagePlugin = Plugin.create(defaultOptions);

PagePlugin.prototype.apply = function (compiler) {
  var plugin = this;

  if (!this.options.template)
    throw new Error('Template not defined');

  compiler.plugin('compilation', function (compilation) {

    // Skip child compilations
    if (compilation.compiler.hasOwnProperty('parentCompilation'))
      return;

    compilation.plugin(HOOKS.EMIT_RESULTS, function(context, done) {
      var results = context.results;

      var promises = results.filter(function (result) {
        return result instanceof DocPage;
      })
      .map(function(page) {
        var filename = interpolateName(plugin.options.filename, {
          path: page.source.absolutePath,
          context: compilation.compiler.context
        });

        page.path = filename;

        return page;
      })
      .map(function (page) {
        var compiler = new TemplateCompiler(compilation, plugin.options.template, true);

        return compiler.run().then(function (template) {
          var templateContext = extend(true, {}, plugin.options.context, {
            page: page.serialize(),
            pages: context.plugin.docs.map(function(doc) {
              return doc.serialize()
            })
          });

          var result = template(templateContext);
          emitFile(compilation, page.path, result);

          return true;
        });
      });

      Promise.all(promises)
        .then(function () {
          done(null, context);
        })
        .catch(function (err) {
          done(err);
        });

    });
  });

};

module.exports = PagePlugin;<commit_msg>Add ability to pass callback as template context in PagePlugin<commit_after>var Plugin = require('./Plugin');
var path = require('path');
var extend = require('extend');
var HOOKS = require('../hooks');
var TemplateCompiler = require('../modules/TemplateCompiler');
var interpolateName = require('../utils/interpolateName');
var emitFile = require('../utils/emitFile');
var DocPage = require('../data/Page');
var Promise = require('bluebird');


var defaultOptions = {
  template: null,
  filename: 'docs/[path][name].[ext]/index.html',
  context: {}
};

var PagePlugin = Plugin.create(defaultOptions);

PagePlugin.prototype.apply = function (compiler) {
  var plugin = this;

  if (!this.options.template)
    throw new Error('Template not defined');

  compiler.plugin('compilation', function (compilation) {

    // Skip child compilations
    if (compilation.compiler.hasOwnProperty('parentCompilation'))
      return;

    compilation.plugin(HOOKS.EMIT_RESULTS, function(context, done) {
      var results = context.results;

      var promises = results.filter(function (result) {
        return result instanceof DocPage;
      })
      .map(function(page) {
        var filename = interpolateName(plugin.options.filename, {
          path: page.source.absolutePath,
          context: compilation.compiler.context
        });

        page.path = filename;

        return page;
      })
      .map(function (page) {
        var compiler = new TemplateCompiler(compilation, plugin.options.template, true);
        var templateContext = plugin.options.context;

        Object.keys(templateContext).forEach(function(key) {
          if (typeof templateContext[key] == 'function') {
            templateContext[key] = templateContext[key].call(null, context.plugin.docs);
          }
        });

        templateContext = extend(true, {}, templateContext, {
          page: page.serialize()
        });

        return compiler.run().then(function (template) {
          var result = template(templateContext);
          emitFile(compilation, page.path, result);
          return true;
        });
      });

      Promise.all(promises)
        .then(function () {
          done(null, context);
        })
        .catch(function (err) {
          done(err);
        });

    });
  });

};

module.exports = PagePlugin;
{'fraction_non_alphanumeric': 0.1373959973015516, 'fraction_numerical': 0.0, 'mean_word_length': 2.7315436241610738, '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': '631014', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1176, 'n_words': 260}