'use strict';

var fs = require('fs'),
    path = require('path');

var Q = require('q'),
    through = require('through2');

var renderJade = require('../../../app/functions/render-jade'),
    renderMarkdown = require('../../../app/functions/render-markdown');

var layoutCache = {};

module.exports = function(options) {

  return through.obj(transform);

  function transform(file, enc, cb) {
    if ((/\.(md|markdown)$/i).test(file.history[0])) {
      file.contents = new Buffer(renderMarkdown(String(file.contents)));
    }
    if ((/\.jade$/i).test(file.history[0])) {
      options.jade.filename = file.history[0];
      file.contents = new Buffer(renderJade(String(file.contents), options.jade, file.data));
    }
    if (!file.data.hasOwnProperty('layout')) {
      file.data.layout = options.layout.default;
    }
    if (file.data.layout) applyLayout(file, cb);
    else cb(null, file);
  }

  function applyLayout(file, cb) {
    getLayout(file.data.layout).done(function(layout) {
      if (layout) {
        file.data.contents = String(file.contents);
        options.jade.filename = layout.path;
        file.contents = new Buffer(renderJade(layout.contents, options.jade, file.data));
      }
      cb(null, file);
    }, cb);
  }

  function getLayout(layout) {
    return Q.Promise(function(resolve, reject) {
      var layoutPath = path.join(options.layout.path, layout);
      if (layoutCache[layoutPath]) {
        resolve(layoutCache[layoutPath]);
      } else {
        Q.nfapply(fs.readFile, [layoutPath, 'utf8']).done(function(layoutContents) {
          if (typeof layoutContents === 'string' && layoutContents.length > 0) {
            layoutCache[layoutPath] = {
              contents: layoutContents,
              path: layoutPath
            };
          }
          resolve(layoutCache[layoutPath]);
        }, reject);
      }
    });
  }

};

@@ -296,24 +296,48 @@
 ports = 
-function
+renderSite;%0A%0Afunction renderSite
 (options
@@ -407,18 +407,20 @@
 e, enc, 
-cb
+done
 ) %7B%0A    
@@ -431,21 +431,12 @@
 (/%5C.
-(md%7Cmarkdown)
+jade
 $/i)
@@ -457,24 +457,71 @@
 tory%5B0%5D)) %7B%0A
+      options.jade.filename = file.history%5B0%5D;%0A
       file.c
@@ -547,24 +547,20 @@
 r(render
-Markdown
+Jade
 (String(
@@ -573,16 +573,41 @@
 ontents)
+, options.jade, file.data
 ));%0A    
@@ -603,28 +603,29 @@
 ata));%0A    %7D
-%0A   
+ else
  if ((/%5C.jad
@@ -613,36 +613,45 @@
  %7D else if ((/%5C.
-jade
+(md%7Cmarkdown)
 $/i).test(file.h
@@ -668,55 +668,8 @@
 ) %7B%0A
-      options.jade.filename = file.history%5B0%5D;%0A
     
@@ -695,36 +695,40 @@
 ew Buffer(render
-Jade
+Markdown
 (String(file.con
@@ -737,33 +737,8 @@
 nts)
-, options.jade, file.data
 ));%0A
@@ -889,18 +889,20 @@
 t(file, 
-cb
+done
 );%0A    e
@@ -905,18 +905,20 @@
    else 
-cb
+done
 (null, f
@@ -957,18 +957,20 @@
 t(file, 
-cb
+done
 ) %7B%0A    
@@ -1242,18 +1242,20 @@
 %7D%0A      
-cb
+done
 (null, f
@@ -1271,10 +1271,12 @@
  %7D, 
-cb
+done
 );%0A 
@@ -1906,10 +1906,9 @@
 ;%0A  %7D%0A%0A%7D
-;
 %0A

refactor render gulp module