-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
466 lines (423 loc) · 13.7 KB
/
index.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
'use strict';
var _ = require('lodash');
var fs = require('fs');
var es = require('event-stream');
var gulp = require('gulp');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var through = require('through2');
var slate = require('./src/slate');
var Promise = require('promise');
var gutil = require('gulp-util');
var rename = require("gulp-rename");
var add = require("gulp-add");
var del = require("del");
var path = require("path");
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-slate';
var ROOT = __dirname + '/';
/**
* set the current file in given path to name
*
* @param srcPath
* @param name
*
* @returns {string}
*/
function changeFile (srcPath, name) {
var path = require('path');
var parts = srcPath.split(path.sep);
parts.pop();
parts.push(name);
return parts.join(path.sep);
}
/**
* get the path of the module
*
* @param name
*
* @returns {string}
*/
function getModulePath(name) {
if (name == 'slate') {
return ROOT+'node_modules/slate';
}
return path.dirname(require.resolve(name));
}
/**
* change the name to the override if provided else just remove .md file ending
*
* @param name
* @param override
*
* @returns {string}
*/
function fixFilename (name, override) {
if (override) {
return override;
}
return name.split('/').pop().replace(/\.md/, "");
}
/**
* @param level
* @internal message
*/
function log(level) {
var args;
if(arguments) {
args = Array.prototype.slice.call(arguments);
args.shift();
} else {
args.push('message missing');
}
args.unshift('['+gutil.colors.blue(PLUGIN_NAME)+']');
if (level >= log.level) {
gutil.log.apply(this, args);
}
}
log.DEBUG = 0;
log.INFO = 10;
log.WARN = 20;
log.ERROR = 30;
/**
* prepare scss files, slate is not perfect...
*
* @returns Promise
*/
function preprocessScss (opts) {
log(log.DEBUG, 'call preprocessSlate');
return new Promise(function (resolve) {
var variablesScss = [getModulePath('slate')+'/source/stylesheets/_variables.scss'];
if (opts.variables) {
if(!path.isAbsolute(opts.variables)) {
opts.variables = path.join(path.dirname(module.parent.filename), opts.variables);
}
variablesScss.push(opts.variables);
}
es.concat(
gulp
.src(variablesScss)
.pipe(add('fix.scss', '@function font-url($url){ @return url($url) }'))
.pipe(concat("_variables.scss"))
.pipe(rename({dirname: '', basename: '_variables', extname: '.scss'}))
.pipe(gulp.dest('slate.tmp/')),
gulp
.src([
getModulePath('slate')+'/source/stylesheets/_icon-font.scss',
getModulePath('slate')+'/source/stylesheets/_normalize.scss',
getModulePath('slate')+'/source/stylesheets/print.css.scss',
getModulePath('slate')+'/source/stylesheets/screen.css.scss',
getModulePath('highlight.js')+'/../styles/solarized-light.css',
getModulePath('highlight.js')+'/../styles/'+opts.style+'.css'
])
.pipe(gulp.dest('slate.tmp/'))
)
.on('end', resolve);
});
}
/**
* build css files
*
* @returns Promise
*/
function processScss () {
log(log.INFO, 'call processScss');
return new Promise(function (resolve) {
es.concat(
gulp.src(['slate.tmp/screen.css.scss'])
.pipe(sass())
.pipe(concat("screen.css"))
.pipe(gulp.dest('slate.tmp/')),
gulp.src(['slate.tmp/print.css.scss'])
.pipe(sass())
.pipe(concat("print.css"))
.pipe(gulp.dest('slate.tmp/'))
)
.on('end', resolve);
});
}
/**
* concat stuff to one css file
*
* @returns Promise
*/
function buildCss (opts) {
log(log.INFO, 'call buildCss');
return new Promise(function (resolve) {
var rawScss = [];
rawScss.push('@media screen { @import "slate.tmp/screen"; }');
rawScss.push('@media screen { .highlight._{ @import "slate.tmp/solarized-light"; } }');
rawScss.push('@media screen { .highlight{ @import "slate.tmp/'+opts.style+'"; } }');
rawScss.push('@media print { @import "slate.tmp/print"; }');
var files = [];
es.concat(
gulp
.src([opts.scss], {base: '.'})
.pipe(add('raw.scss', rawScss.join("\n"), true))
.pipe(concat("app.scss"))
.pipe(sass())
.pipe(concat("app.css"))
.pipe(rename({dirname: 'build', basename: 'app', extname: '.css'}))
.pipe(gutil.buffer(function(err, result) {
_.forEach(result, function (file) {
log(log.DEBUG, "build asset "+file.path);
files.push(file);
});
}))
)
.on('end', function () {
resolve(files);
});
});
}
/**
* build app js file and uglify it
*
* @returns Promise
*/
function buildJsApp () {
log(log.DEBUG, 'call buildJsApp');
return new Promise(function(resolve) {
var files = [];
es.concat(
gulp
.src(getModulePath('slate')+'/source/javascripts/app/*.js', {base: '.'})
.pipe(concat("app.js"))
.pipe(uglify())
.pipe(rename({dirname: 'build', basename: 'app', extname: '.js'}))
.pipe(gutil.buffer(function(err, result) {
_.forEach(result, function (file) {
log(log.DEBUG, "build asset "+file.path);
files.push(file);
});
}))
)
.on('end', function () {
resolve(files);
});
});
}
/**
* build lib js file and uglify it
*
* @returns Promise
*/
function buildJsLibs () {
log(log.DEBUG, 'call buildJsLibs');
return new Promise(function(resolve) {
var files = [];
es.concat(
gulp
.src(
[
getModulePath('slate')+'/source/javascripts/lib/_jquery.js',
getModulePath('slate')+'/source/javascripts/lib/_jquery_ui.js',
getModulePath('slate')+'/source/javascripts/lib/*.js'
], {base: '.'}
)
.pipe(concat("libs.js"))
.pipe(uglify())
.pipe(rename({dirname: 'build', basename: 'libs', extname: '.js'}))
.pipe(gutil.buffer(function(err, result) {
_.forEach(result, function (file) {
log(log.DEBUG, "build asset "+file.path);
files.push(file);
});
}))
)
.on('end', function () {
resolve(files);
});
});
}
/**
* prepare static files
*
* @returns Promise
*/
function buildStatic (opts) {
log(log.DEBUG, 'call buildStatic');
return new Promise(function(resolve) {
var files = [];
es.concat(
gulp
.src(
[
getModulePath('slate')+'/source/fonts/*',
getModulePath('slate')+'/source/images/navbar.png'
], {base: '.'}
)
.pipe(rename(function (path) {
path.dirname = 'build';
}))
.pipe(gutil.buffer(function(err, result) {
_.forEach(result, function (file) {
log(log.DEBUG, "build asset "+file.path);
files.push(file);
});
})),
gulp
.src(opts.logo, {base: '.'})
.pipe(rename({dirname: 'images', basename: 'logo', extname: '.png'}))
.pipe(gutil.buffer(function(err, result) {
_.forEach(result, function (file) {
log(log.DEBUG, "build asset "+file.path);
files.push(file);
});
}))
)
.on('end', function () {
resolve(files);
});
});
}
/**
* build assets
*
* @param opts
*
* @returns Promise
*/
function buildAssets (opts) {
log(log.DEBUG, 'call buildAssets');
return new Promise(function (resolve) {
preprocessScss(opts)
.then(function () {
return processScss();
})
.then(function () {
Promise.all([
buildCss(opts),
buildJsApp(),
buildJsLibs(),
buildStatic(opts)
]).then(function (result) {
log(log.DEBUG, 'finishing up assets');
var files = [];
_.forEach(result, function (group) {
_.forEach(group, function (file) {
files.push(file);
})
});
resolve(files);
}).finally(function() {
// cleanup tmp folder
del([
'slate.tmp'
])
});
})
});
}
module.exports = function (opts) {
opts = _.extend({
log: 'WARN',
assets: true,
filename: false,
style: 'solarized-dark',
template: ROOT+'src/layout.html',
scss: ROOT+'src/app.scss',
variables: null,
logo: getModulePath('slate')+'/source/images/logo.png',
includeLoader: function (name, mainFile) {
return new Promise(function (resolve) {
var includeFile = changeFile(mainFile, "includes/_"+name+".md");
log(log.INFO, "include markup", includeFile);
fs.readFile(
includeFile,
'utf-8',
function (err, source) {
if (err) {
throw new PluginError(
PLUGIN_NAME,
{
name: 'FileOpenError',
message: 'could not open include includes/_'+name+'.md'
}
);
} else {
resolve(source);
}
}
);
});
}
}, opts);
if (typeof log[opts.log] === "number") {
log.level = log[opts.log];
} else {
log.level = 20;
}
return through.obj(
function (file, enc, cb) {
// file coming in :D
var context = this;
var promises = [];
if (opts.assets) {
promises.push(buildAssets(opts));
}
promises.push(new Promise(function (resolve, reject) {
fs.readFile(opts.template, 'utf8', function (err, source) {
if (err) {
reject(new PluginError(
PLUGIN_NAME,
{
name: 'FileOpenError',
message: 'failed to load template: '+opts.template
}
));
return;
}
slate(
file.contents.toString(),
source,
function (name) {
// call internal file loader
return opts.includeLoader(name, file.path);
}
).then(
function(result) {
var main = new gutil.File({
contents: new Buffer(result),
path: fixFilename(file.path, opts.filename)
});
resolve([main]);
},
function (err) {
reject(new PluginError(
PLUGIN_NAME,
{
name: 'SlateError',
message: err
}
));
}
);
});
}));
Promise
.all(promises)
.then(
function (groups) {
log(log.DEBUG, "push files to pipe");
_.forEach(groups, function(files) {
_.forEach(files, function (file) {
if (!file.path) {
return;
}
log(log.DEBUG, "push markdown", gutil.colors.dim(file.path));
context.push(file);
})
});
log(log.INFO, gutil.colors.green('finished building'));
cb();
},
function (error) {
log(log.ERROR, gutil.colors.red(error.message));
cb(error);
}
);
}
);
};