This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
/
gulpfile.js
149 lines (123 loc) · 4.53 KB
/
gulpfile.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
var fs = require('fs');
var gulp = require('gulp');
var cleanCSS = require('gulp-clean-css');
var concat = require('gulp-concat');
var ejs = require('gulp-ejs');
var gls = require('gulp-live-server');
var gulpif = require('gulp-if');
var open = require('gulp-open');
var prettify = require('gulp-prettify');
var rename = require("gulp-rename");
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var del = require('del');
var highlight = require('highlight.js');
var marked = require('marked');
var yaml = require('js-yaml');
var renderer = new marked.Renderer();
var COMPRESS = true;
renderer.code = function (code, language) {
var highlighted = language ? highlight.highlight(language, code).value
: highlight.highlightAuto(code).value;
return '<pre class="highlight ' + language + '"><code>' + highlighted + '</code></pre>';
};
var readIndexYml = function() {
return yaml.safeLoad(fs.readFileSync('./source/index.yml', 'utf8'));
};
var getPageData = function() {
var config = readIndexYml();
var includes = config.includes
.map(function(include) { return './source/includes/' + include + '.md'; })
.map(function(include) { return fs.readFileSync(include, 'utf8'); })
.map(function(include) { return marked(include, { renderer: renderer }); });
return {
current_page: {
data: config
},
page_classes: '',
includes: includes,
image_tag: function(filename, alt, className) {
return '<img alt="' + alt + '" class="' + className + '" src="images/' + filename + '">';
},
javascript_include_tag: function(name) {
return '<script src="javascripts/' + name + '.js" type="text/javascript"></script>';
},
stylesheet_link_tag: function(name, media) {
return '<link href="stylesheets/' + name + '.css" rel="stylesheet" type="text/css" media="' + media + '" />';
},
langs: (config.language_tabs || []).map(function(lang) {
return typeof lang == 'string' ? lang : lang.keys.first;
})
};
};
gulp.task('clean', function () {
return del(['build/*']);
});
gulp.task('fonts', function() {
return gulp.src('./source/fonts/**/*').pipe(gulp.dest('build/fonts'));
});
gulp.task('images', function() {
return gulp.src('./source/images/**/*').pipe(gulp.dest('build/images'));
});
gulp.task('js', function() {
var config = readIndexYml();
var libs = [
'./source/javascripts/lib/_energize.js',
'./source/javascripts/lib/_jquery.js',
'./source/javascripts/lib/_jquery_ui.js',
'./source/javascripts/lib/_jquery.tocify.js',
'./source/javascripts/lib/_imagesloaded.min.js',
];
var scripts = [
'./source/javascripts/app/_lang.js',
'./source/javascripts/app/_toc.js',
];
if (config.search) {
libs.push('./source/javascripts/lib/_lunr.js');
libs.push('./source/javascripts/lib/_jquery.highlight.js');
libs.push('./source/javascripts/app/_search.js');
}
return gulp.src(libs.concat(scripts))
.pipe(concat('all.js'))
.pipe(gulpif(COMPRESS, uglify()))
.pipe(gulp.dest('./build/javascripts'));
});
gulp.task('sass', function () {
return gulp.src('./source/stylesheets/*.css.scss')
.pipe(sass().on('error', sass.logError))
.pipe(rename({ extname: ''}))
.pipe(gulpif(COMPRESS, cleanCSS()))
.pipe(gulp.dest('./build/stylesheets'));
});
gulp.task('highlightjs', function () {
var config = readIndexYml();
var path = './node_modules/highlight.js/styles/' + config.highlight_theme + '.css';
return gulp.src(path)
.pipe(rename({ prefix: 'highlight-'}))
.pipe(gulpif(COMPRESS, cleanCSS()))
.pipe(gulp.dest('./build/stylesheets'));
});
gulp.task('html', function () {
var data = getPageData();
return gulp.src('./source/*.html')
.pipe(ejs(data).on('error', gutil.log))
.pipe(gulpif(COMPRESS, prettify({indent_size: 2})))
.pipe(gulp.dest('./build'));
});
gulp.task('NO_COMPRESS', function() {
COMPRESS = false;
});
gulp.task('default', ['clean', 'fonts', 'images', 'highlightjs', 'js', 'sass', 'html']);
gulp.task('serve', ['NO_COMPRESS', 'default'], function() {
gulp.watch(['./source/*.html', './source/includes/**/*'], ['html']);
gulp.watch('./source/javascripts/**/*', ['js']);
gulp.watch('./source/stylesheets/**/*', ['sass']);
gulp.watch('./source/index.yml', ['highlightjs', 'js', 'html']);
var server = gls.static('build', 4567);
server.start();
gulp.watch(['build/**/*'], function (file) {
server.notify.apply(server, [file]);
});
gulp.src(__filename).pipe(open({uri: 'http://localhost:4567'}));
});