/*
 *  Process:
 *    * Scan the templates directory and make list all .temp or .part files.
 *    * Divide them in templates and partials based on extention.
 *    * Create new Handlebars engine.
 *    * Load and register all partials (name from file).
 *    * Load and compile all templates (name from file).
 *    * Look at the `expand` map, it maps {template name => data spec}.
 *
 *    Data Spec:
 *      The data spcecifiction is an object with the following required fields:
 *        * type: the type of the specification.
 *        * dest: the output file for the expanded template.
 *
 *      Other fields depend on the type.
 *
 *      * "raw" type:
 *        The raw type gets the data from the `data` attribute of the
 *        specification itself.
 *        `dest` must be a file path.
 */


module.exports = function(grunt) {
  var Handelbars = require("handlebars");
  var load       = require("./helpers/loaders");
  var utils      = require("./helpers/utils");

  // Map of expand types to expand function.
  var file = require("./processors/file");
  var raw  = require("./processors/raw");
  var EXPAND_TYPES = {
    file: file.single,
    raw:  raw.single,

    "multi-file": file.multi,
    "multi-raw":  raw.multi
  };


  grunt.registerMultiTask(
      "handlebars-expand",
      "A grunt task to compile and run handlebars templates offline.",
      function() {
    var helpers   = utils.scanDirectory("helpers",   "js");
    var partials  = utils.scanDirectory("partials",  "part");
    var plugins   = utils.scanDirectory("plugins",   "js");
    var templates = utils.scanDirectory("templates", "temp");

    var handlebars = Handelbars.create();
    handlebars.templates = {};

    load.helpers(helpers, handlebars, 8);
    load.plugins(plugins, handlebars);

    load.partials(partials, handlebars, 9);
    load.templates(templates, handlebars, 10);

    var data = this.data || {};
    var map  = data.expand || data;

    for (var name in map) {
      if (!map.hasOwnProperty(name)) { continue; }

      var spec = map[name];
      EXPAND_TYPES[spec.type](grunt, name, spec, handlebars);
    }
  });

};

@@ -1379,24 +1379,531 @@
 unction() %7B%0A
+    var options = this.options(%7B%0A      helpers_dir:   %22helpers%22,%0A      partials_dir:  %22partials%22,%0A      partials_ext:  %22part%22,%0A      plugins_dir:   %22plugins%22,%0A      templates_dir: %22templates%22,%0A      templates_ext: %22temp%22%0A    %7D);%0A%0A    var helpers_dir   = options.helpers_dir;%0A    var partials_dir  = options.partials_dir;%0A    var partials_ext  = options.partials_ext;%0A    var plugins_dir   = options.plugins_dir;%0A    var templates_dir = options.templates_dir;%0A    var templates_ext = options.templates_ext;%0A%0A
     var help
@@ -1930,25 +1930,27 @@
 rectory(
-%22
 helpers
-%22
+_dir
 ,   %22js%22
@@ -1992,25 +1992,24 @@
 rectory(
-%22
 partials
 %22,  %22par
@@ -2004,18 +2004,27 @@
 ials
-%22
+_dir
 ,  
-%22
 part
-%22
+ials_ext
 );%0A 
@@ -2062,25 +2062,27 @@
 rectory(
-%22
 plugins
-%22
+_dir
 ,   %22js%22
@@ -2124,17 +2124,16 @@
 rectory(
-%22
 template
@@ -2137,17 +2137,27 @@
 ates
-%22
+_dir
 , 
-%22
 temp
-%22
+lates_ext
 );%0A%0A
@@ -2272,9 +2272,30 @@
 rs, 
-8
+helpers_dir.length + 1
 );%0A 
@@ -2360,16 +2360,18 @@
 artials,
+  
  handleb
@@ -2379,9 +2379,31 @@
 rs, 
-9
+partials_dir.length + 1
 );%0A 
@@ -2447,10 +2447,32 @@
 rs, 
+templates_dir.length + 
 1
-0
 );%0A%0A

Support for custom data paths.