Fri Jan 19 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
function gulphtml() {
	//Get the "gulpconfig*" file
	var cfgobj = JSON.parse( fs.readFileSync(__dirname + path.sep + configsrc) );
	console.log('processing: ', configsrc);
	var htmlguides = cfgobj.html;
	var contentdata = cfgobj['content' + (args['loc'] ? '-' + args['loc'] : '')];
	var tagsource = {langval: (args['loc'] ? args['loc'] : 'en')};
	var cfgsource = [];
	var indexbasename = "";
	var destext = cfgobj.dest;
	var moduleguides = cfgobj.modules;

	var modules = !moduleguides ? [] : (moduleguides.filter(function(v){
			//filter modules that are not targeted to this market
			return v.match(/::/) ? (v.split('::')[1] === args['loc']) : 1;
		}).map(function(modulename){
			//get modules from names
			console.log('Adding Module: ' + modulename);
			var modret = require(__dirname + path.sep + 'build' + path.sep + 'modules' + path.sep + modulename.split('::')[0] + '.js' );
			modret.gulpname = modulename;
			return modret;
		})
	);

	while (htmlguides.length) {
		(function preparetemplates(_t) {
			tagsource[_t[1]] = fs.readFileSync(__dirname + path.sep + _t[0]);
			cfgsource.push(_t[0]);
			if(_t[1] === "index")
			{
				indexbasename = _t[0].split('/').pop();
			}
		})(htmlguides.pop());
	}

	//apply content variables to tagsource
	!!contentdata && ( tagsource = lo_.defaults(tagsource, contentdata) );

	//apply business logic using the getbusinessdata function (which should use the same source as Landing Pages, to maintain a single point of truth.)
	!!contentdata && ( tagsource = lo_.defaults(tagsource, {"getbusinessdata": getbusinessdata(contentdata && contentdata.content_langcode)}) );
	tagsource = lo_.defaults(tagsource, {"crosssell": getbusinessdata()});
	//Custom to expose to the templater:
	//gettoplvl: can be called in template to query the "top" level product for each type
	tagsource = lo_.defaults(tagsource, {gettoplvl: function(o) {return (getbusinessdata(contentdata && contentdata.content_langcode))(o).sort(function(a,b){return a.lvl === b.lvl ? 0 : a.lvl<b.lvl ? 1 : -1})[0];}});
	//args: give template access to compilation arguments
	tagsource = lo_.defaults(tagsource, args);
	//fs,__dirname,path: template access to file system
	tagsource = lo_.defaults(tagsource, {fs: fs, __dirname: __dirname, path: path});


	return gulp.src(cfgsource.length ? cfgsource : 'build/*.html')
		.pipe($.template( tagsource ))
		.pipe($.template( tagsource )) /* 2nd pass to allow for compound templating */
		//.pipe($.htmlhint())
		//.pipe($.htmlhint.reporter())
		.pipe((typeof moduleguides != 'undefined' && moduleguides.length) ? dom(function _chaintransforms(){
			//chain each of the transform functions in module
			modules.reduce(function gettransform(_this, mod, i){
				mod.transforms.reduce(function runtransform(_t, f, j){
					//run the transform and feed it's return value (nominally _this_) back into the
					args['verbose'] && console.log('Applying Module:' + (mod.gulpname + 1) + '::Transform#' + (j + 1));
					return f.call(_t);
				} ,_this);
				return _this;
			}, this);

			return this;
		}) : $.util.noop())
		.pipe(args.size ? $.size() : $.util.noop())
		.pipe($.htmlmin({collapseWhitespace: true, removeComments: true, keepClosingSlash:true, minifyJS: true}))
		.pipe(args.size ? $.size() : $.util.noop())
		.pipe($.rename(function renamehtml(path){
			if ( indexbasename == (path.basename +  path.extname) )
			{
				path.basename = "index";
			}
		}))
		.pipe(gulpFilter(file => /index/.test(file.path) )) // avoid generating unnecessary html files
		.pipe(gulp.dest('deploy' + (destext ? destext : '')))
}