<commit_before>var _ = require('lodash');
var path = require('canonical-path');
var Q = require('q');
var nunjucks = require('nunjucks');
var log = require('winston');
var writer = require('./utils/doc-writer');
var templateFinderFactory = require('./utils/template-finder');

module.exports = function docRendererFactory(config) {

  if ( !config || !config.rendering || !config.rendering.templateFolder) {
    throw new Error('Invalid configuration: ' + config);
  }

  var templateFinder = templateFinderFactory(config);

  // Lets use {$ $} for nunjucks interpolation rather than {{ }}, which conflicts with AngularJS
  var env = nunjucks.configure(config.rendering.templateFolder, {
    tags: {
      variableStart: '{$',
      variableEnd: '$}'
    }
  });


  // Configure nunjucks with the custom filters
  _.forEach(config.rendering.filters, function(filter) {
    env.addFilter(filter.name, filter.process);
  });


  // Configure nunjucks with the custom tags
  _.forEach(config.rendering.tags, function(tag) {
    env.addExtension(tag.tags[0], tag);
  });

  // Extract any extra helper functions/data from the config
  var helpers = _.defaults({}, config.helpers);

  /**
   * Render the set of documents to the output folder and extra data, using the templates found in the given folder
   * @param  {object} doc          The document to render
   * @param  {object} extraData        Extra data to make available to the rendering
   * @returns {promise}            A promise to each of the output paths, resolved when all the docs have
   *                               been rendered and output
   */
  return function render(docs, extraData) {
    var fileWritePromises = [];
    
    _.forEach(docs, function(doc) {
      var data, res, outputPath, err;
      try {
        data = _.defaults({}, { doc: doc, docs: docs }, extraData, helpers);
        outputPath = path.resolve(config.rendering.outputPath, doc.outputPath);
        var templateFile = templateFinder(data.doc);
        res = nunjucks.render(templateFile, data);

        log.info('outputPath', doc.outputPath, outputPath);

        fileWritePromises.push(writer.writeFile(outputPath, res).then(function() {
          log.debug('Rendered doc', outputPath);
          return outputPath;
        }));
      } catch(ex) {
        throw new Error('Failed to render doc "' + doc.id + '"from file "' + doc.file + '" line ' + doc.startingLine + '\n Error Message follows:\n' + ex.stack);
      }
      
    });

    return Q.all(fileWritePromises);
  };

};<commit_msg>fix(doc-renderer): write outputPath on for debug<commit_after>var _ = require('lodash');
var path = require('canonical-path');
var Q = require('q');
var nunjucks = require('nunjucks');
var log = require('winston');
var writer = require('./utils/doc-writer');
var templateFinderFactory = require('./utils/template-finder');

module.exports = function docRendererFactory(config) {

  if ( !config || !config.rendering || !config.rendering.templateFolder) {
    throw new Error('Invalid configuration: ' + config);
  }

  var templateFinder = templateFinderFactory(config);

  // Lets use {$ $} for nunjucks interpolation rather than {{ }}, which conflicts with AngularJS
  var env = nunjucks.configure(config.rendering.templateFolder, {
    tags: {
      variableStart: '{$',
      variableEnd: '$}'
    }
  });


  // Configure nunjucks with the custom filters
  _.forEach(config.rendering.filters, function(filter) {
    env.addFilter(filter.name, filter.process);
  });


  // Configure nunjucks with the custom tags
  _.forEach(config.rendering.tags, function(tag) {
    env.addExtension(tag.tags[0], tag);
  });

  // Extract any extra helper functions/data from the config
  var helpers = _.defaults({}, config.helpers);

  /**
   * Render the set of documents to the output folder and extra data, using the templates found in the given folder
   * @param  {object} doc          The document to render
   * @param  {object} extraData        Extra data to make available to the rendering
   * @returns {promise}            A promise to each of the output paths, resolved when all the docs have
   *                               been rendered and output
   */
  return function render(docs, extraData) {
    var fileWritePromises = [];
    
    _.forEach(docs, function(doc) {
      var data, res, outputPath, err;
      try {
        data = _.defaults({}, { doc: doc, docs: docs }, extraData, helpers);
        outputPath = path.resolve(config.rendering.outputPath, doc.outputPath);
        var templateFile = templateFinder(data.doc);
        res = nunjucks.render(templateFile, data);

        log.debug('outputPath', doc.outputPath, outputPath);

        fileWritePromises.push(writer.writeFile(outputPath, res).then(function() {
          log.debug('Rendered doc', outputPath);
          return outputPath;
        }));
      } catch(ex) {
        throw new Error('Failed to render doc "' + doc.id + '"from file "' + doc.file + '" line ' + doc.startingLine + '\n Error Message follows:\n' + ex.stack);
      }
      
    });

    return Q.all(fileWritePromises);
  };

};
{'fraction_non_alphanumeric': 0.12729402577118312, 'fraction_numerical': 0.0003904724716907458, 'mean_word_length': 3.037037037037037, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2805362', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1436, 'n_words': 411}