/**
 * Copyright 2014 aixigo AG
 * Released under the MIT license.
 * http://laxarjs.org/license
 */
'use strict';

var grunt = require( 'grunt' );
var fs = require( 'fs' );
var mdCodeStream = require( 'md-code-stream' );

function noop() {}

var logger = {
   header: noop,
   debug: noop,
   wordlist: noop,
   ok: noop
};

function setupTaskWithConfig( task, config, done ) {
   grunt.log = logger;
   grunt.loadTasks( 'tasks' );
   grunt.task.options( {
      error: function( err ) {
         done( err );
      },
      done: done
   } );
   grunt.initConfig( {} );
   grunt.config( task, config );
}

function runTaskWithConfig( task, config, done ) {
   setupTaskWithConfig( task, config, done );
   grunt.task.run( task );
   grunt.task.start( { asyncDone: true } );
}

function runMultiTaskWithConfig( task, config, done ) {
   setupTaskWithConfig( task, {
      default: config
   }, done );
   grunt.task.run( task + ':default' );
   grunt.task.start( { asyncDone: true } );
}

function runTaskWithConfigFromMarkdown( task, file, section, done ) {
  fs.createReadStream( file )
    .pipe( mdCodeStream() )
    .on( 'entry', function( entry ) {
      if( section === entry.section[entry.section.length-1] ) {
         return;
      }
      evalStream( entry );
    });

  function evalStream( stream ) {
    var data = [];
    stream.on('data', data.push.bind(data));
    stream.on('error', done );
    stream.on('end', function() {
      try {
        var evaluator = new Function( 'grunt', Buffer.concat(data) );
        evaluator( {
          initConfig: function(config) {
            setupTaskWithConfig( task, config, done );
            grunt.task.run( task );
            grunt.task.start( { asyncDone: true } );
          }
        } );
      }
      catch( err ) {
        done( err );
      }
    } );
  }
}

module.exports = {
   runTaskWithConfig: runTaskWithConfig,
   runMultiTaskWithConfig: runMultiTaskWithConfig,
   runTaskWithConfigFromMarkdown: runTaskWithConfigFromMarkdown
};

@@ -1055,16 +1055,17 @@
 e ) %7B%0A  
+ 
 fs.creat
@@ -1084,16 +1084,18 @@
  file )%0A
+  
     .pip
@@ -1118,16 +1118,18 @@
 ) )%0A    
+  
 .on( 'en
@@ -1160,16 +1160,19 @@
 %7B%0A      
+ 
+  
 if( sect
@@ -1217,24 +1217,27 @@
 ngth-1%5D ) %7B%0A
+   
          ret
@@ -1243,26 +1243,32 @@
 turn;%0A      
-%7D%0A
+   %7D%0A   
       evalSt
@@ -1290,13 +1290,16 @@
     
+  
 %7D);%0A%0A
+ 
   fu
@@ -1324,24 +1324,26 @@
  stream ) %7B%0A
+  
     var data
@@ -1349,24 +1349,26 @@
 a = %5B%5D;%0A    
+  
 stream.on('d
@@ -1396,24 +1396,26 @@
 data));%0A    
+  
 stream.on('e
@@ -1425,24 +1425,26 @@
 r', done );%0A
+  
     stream.o
@@ -1475,14 +1475,106 @@
     
+   
 try %7B%0A
+            // Trust me, JSHint, I know what I'm doing.%0A            /*jshint -W054*/%0A    
     
@@ -1643,24 +1643,28 @@
  );%0A        
+    
 evaluator( %7B
@@ -1660,24 +1660,29 @@
 valuator( %7B%0A
+     
           in
@@ -1722,16 +1722,22 @@
         
+      
 setupTas
@@ -1775,32 +1775,38 @@
  );%0A            
+     
+ 
 grunt.task.run( 
@@ -1809,24 +1809,30 @@
 un( task );%0A
+      
             
@@ -1874,34 +1874,43 @@
  %7D );%0A          
+    
+ 
 %7D%0A
+    
         %7D );%0A   
@@ -1908,26 +1908,32 @@
  %7D );%0A      
-%7D%0A
+   %7D%0A   
       catch(
@@ -1941,32 +1941,36 @@
 err ) %7B%0A        
+    
 done( err );%0A   
@@ -1968,26 +1968,31 @@
 rr );%0A      
+ 
-%7D%0A
+  %7D%0A  
     %7D );%0A  %7D
@@ -1988,16 +1988,17 @@
    %7D );%0A
+ 
   %7D%0A%7D%0A%0Am

fix jshint, indent