'use strict';
import gulp         from 'gulp';
import plugins      from 'gulp-load-plugins';
import _            from 'lodash';
import fs           from 'fs';
import yaml         from 'js-yaml';
import async        from 'async';
import panini       from 'panini';

// Load all Gulp plugins into one variable
const $ = plugins();

const { COMPATIBILITY, PORT, UNCSS_OPTIONS, PATHS } = loadConfig();

function loadConfig() {
  let ymlFile = fs.readFileSync('config.yml', 'utf8');
  return yaml.load(ymlFile);
}

// From http://stackoverflow.com/questions/23230569/how-do-you-create-a-file-from-a-string-in-gulp
function stringSrc(categories, cb) {
  async.eachOf(categories, (category, name, callback) => {
    var str = "---\n" + yaml.safeDump(category) + "---\n"
    fs.writeFile(PATHS.build + "/" + name + ".html", str, callback)
  }, cb)
}

function buildingBlocksCategoryStarters(cb) {
  var categories = JSON.parse(fs.readFileSync(PATHS.build + '/data/categories.json', 'utf8'));
  fs.mkdir(PATHS.build, () => {stringSrc(categories, cb)})
}

function buildingBlocksCategoryPages() {
  return gulp.src(PATHS.build + '/*.html')
    .pipe(panini({
      root: '_build/',
      layouts: 'src/layouts/building-blocks/index',
      partials: 'src/partials/',
      data: 'src/data/',
      helpers: 'src/panini-helpers/'
    }))
    .pipe(gulp.dest(PATHS.dist));
}

gulp.task('building-blocks-categories', buildingBlocksCategoryPages);
gulp.task('building-block-indices',
  gulp.series(buildingBlocksCategoryStarters, buildingBlocksCategoryPages));

@@ -698,16 +698,335 @@
 k) =%3E %7B%0A
+    var numPages = Math.ceil(category.total / 12);%0A    var objs = %5B%5D%0A    for(var i = 0; i %3C numPages; i++) %7B%0A      var obj = %7Btotal: category.total, page: i + 1, numPages: numPages%7D;%0A      obj.blocks = category.blocks.slice(i * numPages, 12);%0A      objs.push(obj)%0A    %7D%0A    async.each(objs, (obj, innerCallback) =%3E %7B%0A  
     var 
@@ -1055,24 +1055,19 @@
 afeDump(
-category
+obj
 ) + %22---
@@ -1070,16 +1070,18 @@
 %22---%5Cn%22%0A
+  
     fs.w
@@ -1129,16 +1129,38 @@
 l%22, str,
+ innerCallback)%0A    %7D,
  callbac
@@ -1161,16 +1161,17 @@
 allback)
+;
 %0A  %7D, cb

Fix pagination data