/**
 * gulpfile.js
 *
 * All the yummy build stuff!
 *
 * @author Fredrik Frodlund (contact@frippz.se)
 */

// Requirements
var gulp             = require('gulp'),
    gulpif           = require('gulp-if'),
    gutil            = require('gulp-util'),
    plumber          = require('gulp-plumber'),
    sourcemaps       = require('gulp-sourcemaps'),
    autoprefixer     = require('gulp-autoprefixer'),
    minifyCss        = require('gulp-minify-css'),
    concat           = require('gulp-concat'),
    uglify           = require('gulp-uglify'),
    postcss          = require('gulp-postcss'),
    watch            = require('gulp-watch'),
    customProperties = require('postcss-custom-properties'),
    ghPages          = require('gh-pages'),
    path             = require('path'),
    eslint           = require('gulp-eslint'),
    htmlhint         = require('gulp-htmlhint'),
    w3cjs            = require('gulp-w3cjs'),
    dirSync          = require('gulp-directory-sync'),
    webserver        = require('gulp-webserver'),
    svgSprite        = require('gulp-svg-sprite');

// Configure paths
var paths = {

  // Inputs
  js: ['./src/js/**/*.js'],
  css: ['./src/css/**/*.css'],

  // Static assets
  images: './src/i/',

  // SVG icons
  svg: ['./src/svg/**/*.svg'],

  // Outputs
  jsOutput: 'main.js',
  cssOutput: 'styles.css',

  // Destinations
  jsDest: './gui/js/',
  cssDest: './gui/css/',
  imgDest: './gui/i/',
  svgDest: './_includes/'

};

// Error handler
var onError = function (err) {
  gutil.beep();
  console.log(err);
};

// Check for production
var isProduction = process.env.NODE_ENV === 'production';

// Process stylesheets
gulp.task('css', function () {
  return gulp.src(paths.css)
    .pipe(plumber({
      errorHandler: onError
    }))
    .pipe(sourcemaps.init())
    .pipe(autoprefixer({
      browsers: ['last 2 versions'],
      cascade: false
    }))
    .pipe(postcss([customProperties()]))
    .pipe(concat(paths.cssOutput))
    .pipe(minifyCss({compatibility: 'ie8'}))
    .pipe(gulpif(!isProduction, sourcemaps.write('.')))
    .pipe(gulp.dest(paths.cssDest));
});

// Concatenate and minify JavaScript
gulp.task('js', function () {
  return gulp.src(paths.js)
    .pipe(plumber({
      errorHandler: onError
    }))
    .pipe(sourcemaps.init())
    .pipe(concat(paths.jsOutput))
    .pipe(uglify())
    .pipe(gulpif(!isProduction, sourcemaps.write('.')))
    .pipe(gulp.dest(paths.jsDest));
});

// SVG sprite generation
svgConfig = {
  svg : {
    doctypeDeclaration: false,
    xmlDeclaration: false
  },
  mode: {
    symbol: {
      inline: true,
      dest: '.',
      sprite: 'sprite-symbol.svg'
    },
    css: {
      bust: false,
      layout: 'vertical',
      dest: '.',
      sprite: 'sprite-css.svg'
    }
  }
}

gulp.task('svg-sprite', function ( ){
  return gulp.src(paths.svg)
    .pipe(svgSprite(svgConfig))
    .pipe(gulp.dest(paths.svgDest))
    .pipe(gulp.dest(paths.imgDest));
});

// eslint
gulp.task('eslint', function () {
  return gulp.src(paths.js)
    .pipe(eslint())
    .pipe(eslint.format());
});

// Validation
gulp.task('validate', function () {
  // return gulp.src(paths.templates)
  //   .pipe(w3cjs());
});

// Linting task
gulp.task('lint', ['htmlhint', 'eslint', 'validate']);

// Watch for changes
gulp.task('watch', function() {
  watch(paths.css, function() {
    gulp.start(['css']);
  });
  watch(paths.js, function() {
    gulp.start(['js', 'eslint']);
  });
  watch(paths.images, function() {
    gulp.start(['images']);
  });
  watch(paths.svg, function() {
    gulp.start(['svg-sprite']);
  });
});

// Copy image assets into /gui
gulp.task('images', function(){
  gulp.src(paths.images)
    .pipe(dirSync(paths.images, paths.imgDest));
});

// Deploy to gh-pages
gulp.task('deploy', function (cb) {
  ghPages.publish(path.join(process.cwd(), paths.tplDest), cb);
});

// Build
gulp.task('build', ['css', 'js', 'images', 'svg-sprite']);

// Default
gulp.task('default', ['watch', 'css', 'js', 'images', 'svg-sprite', 'lint']);

@@ -832,57 +832,8 @@
 '),%0A
-    htmlhint         = require('gulp-htmlhint'),%0A
     
@@ -1404,16 +1404,78 @@
 cludes/'
+,%0A%0A  // Jekyll build files%0A  jekyllHTML: %5B'./_site/**/*.html'%5D
 %0A%0A%7D;%0A%0A//
@@ -3135,19 +3135,16 @@
 n () %7B%0A 
- //
  return 
@@ -3162,23 +3162,21 @@
 ths.
-templates)%0A  //
+jekyllHTML)%0A 
    .
@@ -3234,20 +3234,8 @@
 ', %5B
-'htmlhint', 
 'esl
@@ -3581,16 +3581,91 @@
 ;%0A  %7D);%0A
+  watch(paths.jekyllHTML, function() %7B%0A    gulp.start(%5B'validate'%5D);%0A  %7D);%0A
 %7D);%0A%0A// 

Remove htmlhint and validate generated HTML files