|
| 1 | +// Définition du LazyPipe pour utiliser Handlebars |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var path = require('path'); |
| 5 | +var _ = require('underscore'); |
| 6 | +var lazypipe = require('lazypipe'); |
| 7 | +var gulp = require('gulp'); |
| 8 | +var gutil = require('gulp-util'); |
| 9 | +var hbs = require('gulp-hbs'); |
| 10 | +var data = require('gulp-data'); |
| 11 | +var dir = require('require-dir'); |
| 12 | +var ENV = require('../../tools/env'); |
| 13 | + |
| 14 | +var DEST = path.resolve(ENV.html['dest-dir']); |
| 15 | +var DEST_URL = DEST.replace(path.resolve(ENV.connect.baseDir), '') |
| 16 | + .replace(path.sep, '/'); |
| 17 | + |
| 18 | +var genericDataFile = path.resolve(path.join(ENV.html['data-dir'], 'data.json')); |
| 19 | + |
| 20 | +var layouts = path.join(ENV.html['src-dir'], 'layout', '**', '*.hbs'); |
| 21 | + |
| 22 | +// HBS HELPERS |
| 23 | +// ---------------------------------------------------------------------------- |
| 24 | +var helpers = dir('../../tools/handlebars/helpers'); |
| 25 | + |
| 26 | +hbs.registerHelper(helpers); |
| 27 | + |
| 28 | +// UTILS |
| 29 | +// ---------------------------------------------------------------------------- |
| 30 | +function processData(file) { |
| 31 | + var base = file.path.replace(ENV.html['src-dir'], ENV.html['data-dir']); |
| 32 | + var specificDataFile = base.replace('.hbs', '.json'); |
| 33 | + var gData = {}; |
| 34 | + var sData = {}; |
| 35 | + |
| 36 | + try { |
| 37 | + gData = require(genericDataFile); |
| 38 | + } catch (e) { |
| 39 | + gutil.log(gutil.colors.yellow('WARN:'), |
| 40 | + 'Unable to find data from', |
| 41 | + genericDataFile.replace(path.resolve('.'), '').slice(1) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + sData = require(specificDataFile); |
| 47 | + } catch (e) { |
| 48 | + gutil.log(gutil.colors.yellow('WARN:'), |
| 49 | + 'Unable to find data from', |
| 50 | + specificDataFile.replace(path.resolve('.'), '').slice(1) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + var data = _.extend({}, gData, sData); |
| 55 | + data.url = file.path.replace(ENV.html['src-dir'], DEST_URL).replace('.hbs', '.html'); |
| 56 | + data.main = hbs.handlebars.compile(file.contents.toString())(data); |
| 57 | + |
| 58 | + return data; |
| 59 | +} |
| 60 | + |
| 61 | +module.exports = function () { |
| 62 | + var lazystream = lazypipe() |
| 63 | + .pipe(data, processData) |
| 64 | + .pipe(hbs, gulp.src(layouts), { |
| 65 | + dataSource: 'data', |
| 66 | + defaultTemplate: ENV.html.layout || '_default.hbs' |
| 67 | + }); |
| 68 | + |
| 69 | + return lazystream(); |
| 70 | +}; |
0 commit comments