<commit_before>var es = require('event-stream');
var Handlebars = require('ember-handlebars');
var Emblem = require('emblem');
var path = require('path');
var extend = require('xtend');

// Return a declaration and namespace name for output
var getNSInfo = function(ns, omitLast) {
  var output = [];
  var curPath = 'this';
  if (ns !== 'this') {
    var nsParts = ns.split('.');
    nsParts.some(function(curPart, index) {
      if (curPart !== 'this') {
        curPath += '[' + JSON.stringify(curPart) + ']';

        // Ignore the last part of the namespace, it will be used for assignment
        if (omitLast && index === nsParts.length - 1) {
          return true;
        }
        else {
          output.push(curPath + ' = ' + curPath + ' || {};');
        }
      }
    });
  }

  return {
    namespace: curPath,
    pathParts: output,
    declaration: output.join('')
  };
};

// Default name processing function should give the filename without extension
var defaultProcessName = function(name) { return path.basename(name, path.extname(name)); };

module.exports = function(options) {
  options = extend({
    compilerOptions: {},
    declareNamespace: true,
    wrapped: true,
    outputType: 'browser', // browser, amd, commonjs, node, hybrid, bare
    processName: defaultProcessName,
    namespace: 'Ember.TEMPLATES',
    ember: true
  }, options);

  // Never declare namespaces if not provided
  if (!options.namespace) {
    options.declareNamespace = false;
  }

  var compileHandlebars = function(file, callback) {
    // Get the name of the template
    var name = options.processName(file.path);

    // Perform pre-compilation
    try {
      var compiled = Emblem.precompile(Handlebars, file.contents.toString(), options.compilerOptions);
    }
    catch(err) {
      return callback(err, file);
    }

    if (options.wrapped) {
      if (options.ember) {
        compiled = 'Ember.Handlebars.template('+compiled+')';
      }
      else {
        compiled = 'Handlebars.template('+compiled+')';
      }
    }

    // Handle different output times
    if (options.outputType === 'browser' || options.outputType === 'hybrid') {
      // Prepend namespace to name
      if (options.namespace !== false) {
        name = options.namespace+'.'+name;
      }

      // Get namespace information for the final template name
      var nameNSInfo = getNSInfo(name, true);

      if (options.outputType === 'hybrid') {
        var templateDef = compiled;

        compiled = "(function(g) {";
        compiled += "var Handlebars = g.Handlebars || require('handlebars');";

        if (options.declareNamespace) {
          compiled += nameNSInfo.declaration+compiled;
        }

        compiled += "var template = "+templateDef+";"
        compiled += "if (typeof exports === 'object' && exports) module.exports = template;";
        compiled += nameNSInfo.namespace+' = template;';
        compiled += "}(typeof window !== 'undefined' ? window : global));";
      }
      else {
        // Add assignment
        compiled = nameNSInfo.namespace+' = '+compiled+';';

        if (options.declareNamespace) {
          // Tack on namespace declaration, if necessary
          compiled = nameNSInfo.declaration+compiled;
        }
      }
    }
    else if (options.outputType === 'amd') {
      compiled = "define(['handlebars'], function(Handlebars) {return "+compiled+";});";
    }
    else if (options.outputType === 'commonjs') {
      compiled = "module.exports = function(Handlebars) {return "+compiled+";};";
    }
    else if (options.outputType === 'node') {
      compiled = "module.exports = "+compiled+";";
      compiled = "var Handlebars = global.Handlebars || require('handlebars');"+compiled;
    }
    else if (options.outputType !== 'bare') {
      callback(new Error('Invalid output type: '+options.outputType), file);
    }

    file.path = path.join(path.dirname(file.path), name+'.js');
    file.contents = new Buffer(compiled);

    callback(null, file);
  };

  return es.map(compileHandlebars);
};
<commit_msg>Namespace templates according to directory they're in<commit_after>var es = require('event-stream');
var Handlebars = require('ember-handlebars');
var Emblem = require('emblem');
var path = require('path');
var extend = require('xtend');

// Return a declaration and namespace name for output
var getNSInfo = function(ns, omitLast) {
  var output = [];
  var curPath = 'this';
  if (ns !== 'this') {
    var nsParts = ns.split('.');
    nsParts.some(function(curPart, index) {
      if (curPart !== 'this') {
        curPath += '[' + JSON.stringify(curPart) + ']';

        // Ignore the last part of the namespace, it will be used for assignment
        if (omitLast && index === nsParts.length - 1) {
          return true;
        }
        else {
          output.push(curPath + ' = ' + curPath + ' || {};');
        }
      }
    });
  }

  return {
    namespace: curPath,
    pathParts: output,
    declaration: output.join('')
  };
};

// Default name processing function should give the filename without extension
var defaultProcessName = function(name) { return path.basename(name, path.extname(name)); };

module.exports = function(options) {
  options = extend({
    compilerOptions: {},
    declareNamespace: true,
    wrapped: true,
    outputType: 'browser', // browser, amd, commonjs, node, hybrid, bare
    processName: defaultProcessName,
    namespace: 'Ember.TEMPLATES',
    ember: true
  }, options);

  // Never declare namespaces if not provided
  if (!options.namespace) {
    options.declareNamespace = false;
  }

  var compileHandlebars = function(file, callback) {
    // Get the name of the template
    // The name is namespaced to the directory that the file is in
    var name = path.relative(file.cwd, file.path)
      .slice(0, -path.extname(file.path).length)
      .split(path.sep);
    name = name.slice(2, name.length).join('/');

    // Perform pre-compilation
    try {
      var compiled = Emblem.precompile(Handlebars, file.contents.toString(), options.compilerOptions);
    }
    catch(err) {
      return callback(err, file);
    }

    if (options.wrapped) {
      if (options.ember) {
        compiled = 'Ember.Handlebars.template('+compiled+')';
      }
      else {
        compiled = 'Handlebars.template('+compiled+')';
      }
    }

    // Handle different output times
    if (options.outputType === 'browser' || options.outputType === 'hybrid') {
      // Prepend namespace to name
      if (options.namespace !== false) {
        name = options.namespace+'.'+name;
      }

      // Get namespace information for the final template name
      var nameNSInfo = getNSInfo(name, true);

      if (options.outputType === 'hybrid') {
        var templateDef = compiled;

        compiled = "(function(g) {";
        compiled += "var Handlebars = g.Handlebars || require('handlebars');";

        if (options.declareNamespace) {
          compiled += nameNSInfo.declaration+compiled;
        }

        compiled += "var template = "+templateDef+";"
        compiled += "if (typeof exports === 'object' && exports) module.exports = template;";
        compiled += nameNSInfo.namespace+' = template;';
        compiled += "}(typeof window !== 'undefined' ? window : global));";
      }
      else {
        // Add assignment
        compiled = nameNSInfo.namespace+' = '+compiled+';';

        if (options.declareNamespace) {
          // Tack on namespace declaration, if necessary
          compiled = nameNSInfo.declaration+compiled;
        }
      }
    }
    else if (options.outputType === 'amd') {
      compiled = "define(['handlebars'], function(Handlebars) {return "+compiled+";});";
    }
    else if (options.outputType === 'commonjs') {
      compiled = "module.exports = function(Handlebars) {return "+compiled+";};";
    }
    else if (options.outputType === 'node') {
      compiled = "module.exports = "+compiled+";";
      compiled = "var Handlebars = global.Handlebars || require('handlebars');"+compiled;
    }
    else if (options.outputType !== 'bare') {
      callback(new Error('Invalid output type: '+options.outputType), file);
    }

    file.path = path.join(path.dirname(file.path), name+'.js');
    file.contents = new Buffer(compiled);

    callback(null, file);
  };

  return es.map(compileHandlebars);
};

{'fraction_non_alphanumeric': 0.1481659074562455, 'fraction_numerical': 0.0004795013186286262, 'mean_word_length': 2.7751131221719456, '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': '6897011', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2344, 'n_words': 611}