'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var Handlebars = require('handlebars');
var yaml = require('js-yaml');
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var ejs = require('ejs');

// 组合模板和数据生成网页
// 模板支持: Handlebars, ejs
// 数据支持: yaml
// 参数:
//   opts.partials: Handlebars 参数
//   opts.helpers: Handlebars 参数
module.exports.html = function (opts) {

  var options = opts || {};

  //Go through a partials object
  if(options.partials){
    for(var p in options.partials){
      Handlebars.registerPartial(p, options.partials[p]);
    }
  }
  //Go through a helpers object
  if(options.helpers){
    for(var h in options.helpers){
      Handlebars.registerHelper(h, options.helpers[h]);
    }
  }

  return through.obj(function (file, enc, cb) {
    if (file.isNull()) {
      this.push(file);
      return cb();
    }

    if (file.isStream()) {
      this.emit('error', new gutil.PluginError('gulp-tpl.html', 'Streaming not supported'));
      return cb();
    }

    var filepath = file.path;
    var dirname = path.dirname(filepath);
    var basename = path.basename(filepath);
    var name = path.basename(filepath, path.extname(basename));

    var hbsfile = path.join(dirname, name + ".hbs");
    var ejsfile = path.join(dirname, name + ".ejs");
    var datafile = path.join(dirname, name + ".yaml");
    var htmlfile = path.join(dirname, name + ".html");

    var errmsg = "";
    try {
      var data = {};
      if (fs.existsSync(datafile)) {
        data = yaml.safeLoad(fs.readFileSync(datafile, 'utf8'));
      }
    } catch (err) {
      errmsg = "[gulp-tpl.html error: " + datafile + "]  " + err;
    }

    if (errmsg==="") {
      var tplname = name;
      try {
        if (fs.existsSync(hbsfile)) {
          tplname += ".hbs";
          var tpl = fs.readFileSync(hbsfile, 'utf8');
          file.contents = new Buffer(Handlebars.compile(tpl)(data));
        } else if (fs.existsSync(ejsfile)) {
          tplname += ".ejs";
          var tpl = fs.readFileSync(ejsfile, 'utf8');
          data.filename = ejsfile;
          file.contents = new Buffer(ejs.render(tpl, data));
        }
      } catch (err) {
        errmsg = "[gulp-tpl.html error: " + path.join(dirname,tplname) + "]  " + err;
      }
    }

    if (errmsg!="") {
      if (options.ignoreErr) {
        gutil.log(errmsg);
      } else {
        this.emit('error', new gutil.PluginError('gulp-tpl.html', errmsg));
      }
    }

    file.path = htmlfile;
    this.push(file);
    cb();
  });
};

@@ -1333,20 +1333,20 @@
     var 
-data
+yaml
 file = p
@@ -1376,24 +1376,79 @@
 + %22.yaml%22);%0A
+    var jsonfile = path.join(dirname, name + %22.json%22);%0A
     var html
@@ -1487,24 +1487,43 @@
  %22.html%22);%0A%0A
+    var data = %7B%7D;%0A
     var errm
@@ -1531,28 +1531,16 @@
 g = %22%22;%0A
-    try %7B%0A  
     var 
@@ -1539,31 +1539,45 @@
     var data
+file
  = 
-%7B%7D
+%22%22
 ;%0A
+    try %7B%0A
       if (fs
@@ -1592,20 +1592,49 @@
 ync(
-datafile)) %7B
+yamlfile)) %7B%0A        datafile = yamlfile;
 %0A   
@@ -1675,20 +1675,161 @@
 ileSync(
-data
+yamlfile, 'utf8'));%0A      %7D else if (fs.existsSync(jsonfile)) %7B%0A        datafile = jsonfile;%0A        data = JSON.$.parseJSON(fs.readFileSync(json
 file, 'u

support json