-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
96 lines (82 loc) · 2.75 KB
/
gulpfile.js
File metadata and controls
96 lines (82 loc) · 2.75 KB
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
/* globals require */
var path = require('path');
var del = require('del');
var glob = require('glob');
var gulp = require('gulp');
var merge = require('merge-stream');
var rename = require('gulp-rename');
var cleanCss = require('gulp-clean-css');
var less = require('gulp-less');
var textTransformation = require('gulp-text-simple');
var htinliner = require('htinliner');
var h5smplDir = './node_modules/h5smpl/';
var tempDir = './tmp/';
var h5styleDir = path.join(h5smplDir, 'dist', 'css');
var styleSourcePattern = path.join(h5styleDir, 'style.*.mini.css');
var extractStyleFromFilename = function (fileName) {
'use strict';
return fileName.replace(/^.*\.([^\.]*?)\.mini\.css$/, '$1');
};
var findStyles = function () {
'use strict';
return glob.sync(styleSourcePattern)
.map(extractStyleFromFilename);
};
var setTemplateStyle = function (html, opts) {
'use strict';
return html.replace('style.default.css', 'style.' + opts.style + '.mini.css');
};
var insertLangAttribute = function (html) {
'use strict';
return html.replace('<html>', '<html$$if(lang)$$ lang="$$lang$$"$$endif$$>');
};
var insertNoFrameClass = function (html) {
'use strict';
return html.replace('<div id="frame">', '<div id="frame"$$if(toc)$$$$else$$ class="no-toc"$$endif$$>');
}
gulp.task('clean', function () {
'use strict';
return del([tempDir + '**']);
});
gulp.task('copy_template', function () {
'use strict';
return gulp.src('./src/template.html')
.pipe(gulp.dest(tempDir));
});
gulp.task('copy_h5smpl_styles', function () {
'use strict';
return gulp.src(styleSourcePattern, {base: h5styleDir})
.pipe(gulp.dest(path.join(tempDir, 'css/')));
});
gulp.task('compile_mdproc_styles', function () {
'use strict';
return gulp.src('./src/*.less')
.pipe(less())
.pipe(cleanCss())
.pipe(gulp.dest(path.join(tempDir, 'css/')));
});
var preparations = gulp.parallel(
'copy_template',
'compile_mdproc_styles',
'copy_h5smpl_styles'
);
gulp.task('preparations', preparations);
gulp.task('build_html_template', function () {
'use strict';
return merge(findStyles().map(function (style) {
var styleSetter = textTransformation(setTemplateStyle, { style: style });
var langInserter = textTransformation(insertLangAttribute);
var noFrameInserter = textTransformation(insertNoFrameClass);
return gulp.src('template.html', { cwd: tempDir })
.pipe(styleSetter())
.pipe(htinliner())
.pipe(langInserter())
.pipe(noFrameInserter())
.pipe(rename('template.' + style + '.html'))
.pipe(gulp.dest('assets/'));
}));
});
gulp.task('default', gulp.series(
'preparations',
'build_html_template'
));