var gulp         = require('gulp'),
    autoprefixer = require('gulp-autoprefixer'),
    browserSync  = require('browser-sync').create(),
    htmlmin      = require('gulp-htmlmin'),
    nunjucks     = require('gulp-nunjucks-render'),
    sass         = require('gulp-sass'),
    sourcemaps   = require('gulp-sourcemaps'),
    projectData  = require('./source/data/projects.json');

gulp.task('build-html', function() {
  return gulp.src('source/html/*.html')
    .pipe(nunjucks({
      data: projectData
    }))
    .pipe(htmlmin({
      collapseBooleanAttributes: true,
      collapseWhitespace: true,
      conservativeCollapse: true, // required for browser-sync to work
      decodeEntities: true,
      includeAutoGeneratedTags: false,
      minifyCSS: true,
      minifyJS: true,
      processConditionalComments: true,
      processScripts: ['text/html'],
      quoteCharacter: '"',
      removeComments: true,
      removeEmptyAttributes: true,
      removeRedundantAttributes: true,
      removeScriptTypeAttributes: true,
      removeStyleLinkTypeAttributes: true,
      useShortDoctype: true
    }))
    .pipe(gulp.dest('public'));
});

gulp.task('build-css', function() {
  return gulp.src('source/scss/*.scss')
    .pipe(sourcemaps.init())
    .pipe(sass({
      outputStyle: 'compressed'
    }).on('error', sass.logError))
    .pipe(autoprefixer())
    .pipe(sourcemaps.write('./sourcemaps'))
    .pipe(gulp.dest('public/assets/stylesheets'));
});

gulp.task('sync-html', ['build-html'], function() {
  browserSync.reload();
});

gulp.task('sync-css', ['build-css'], function() {
  browserSync.reload();
});

gulp.task('watch', function() {
  browserSync.init({
    server: {
      baseDir: 'public'
    }
  });

  gulp.watch('source/scss/*.scss', ['sync-css']);
  gulp.watch(['source/html/*.html', 'source/templates/*.html',
    'source/data/*.json'], ['sync-html']);
});

gulp.task('build', ['build-html', 'build-css']);
gulp.task('default', ['build', 'watch']);

@@ -131,16 +131,50 @@
 eate(),%0A
+    fs           = require('fs'),%0A
     html
@@ -347,21 +347,61 @@
 cemaps')
-,%0A   
+;%0A%0Agulp.task('build-html', function() %7B%0A  var
  project
@@ -405,26 +405,44 @@
 ectData 
- 
 = 
-require
+JSON.parse(fs.readFileSync
 ('./sour
@@ -468,47 +468,10 @@
 on')
+)
 ;
-%0A%0Agulp.task('build-html', function() %7B
 %0A  r
@@ -527,22 +527,16 @@
 njucks(%7B
-%0A     
  data: p
@@ -545,20 +545,16 @@
 jectData
-%0A   
  %7D))%0A   

Allow live reloading of projects data