-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgulpfile.js
171 lines (147 loc) · 5.25 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Include gulp
var gulp = require('gulp'),
p = require('./package.json'),
fs = require('fs');
// Include Our Plugins
var jshint = require('gulp-jshint'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
babel = require('gulp-babel'),
minifyCss = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
header = require('gulp-header'),
connect = require('gulp-connect'),
markdown = require('gulp-markdown-it'),
replace = require('gulp-replace'),
toc = require('gulp-doctoc'),
//file watch paths
paths = {
css: {
home: 'css/',
src: 'css/**/',
dest: 'dist/'
},
js: {
home: 'js/',
src: 'js/**/',
dest: 'dist/'
}
},
/*
date = new Date(),
datetime = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() +
' @ ' + date.getHours() + ':' + (date.getMinutes() < 10 ? '0' : '') + date.getMinutes() +
':' + (date.getSeconds() < 10 ? '0' : '') + date.getSeconds(),
*/
headerText = function(filename) {
return '/*! ' + p.name + ' v' + p.version + ': ' + filename +
' by ' + p.author + ' (' + p.license + ' license) */\n';
};
// Lint Task
gulp.task('lint', function() {
return gulp.src(paths.js.home + '*.js')
.pipe(jshint());
});
// Compile Our Sass
gulp.task('sass', function() {
return gulp.src(paths.css.home + '*.scss')
.pipe(sass({
outputStyle: 'expanded'
}))
.pipe(autoprefixer({
browsers: ['> 0.75%', 'not IE 7', 'not IE 8', 'not IE 9' /*, 'not OperaMobile'*/ ]
}))
.pipe(gulp.dest(paths.css.home));
});
// Inject readme and its toc into index.html
gulp.task('inject-markdown', ['markdown', 'toc'], function() {
return gulp.src('index.html')
// .pipe(inject(
// gulp.src(['demo-media/readme.html', 'demo-media/toc.html']), {
// transform: function(filePath, file) {
// return file.contents.toString('utf8')
// }
// }
// ))
.pipe(replace(/inject:toc -->[^]*?(?=<!-- endinject)/g,
'inject:toc -->\n' + fs.readFileSync('demo-media/toc.html', 'utf8')))
.pipe(replace(/inject:readme -->[^]*?(?=<!-- endinject)/g,
'inject:readme -->\n' + fs.readFileSync('demo-media/readme.html', 'utf8')))
.pipe(gulp.dest('.'));
});
//Compile readme to html
gulp.task('markdown', function() {
return gulp.src(['./README.md'])
.pipe(replace(/[^]*(?=## Usage)/gm, '')) //exclude unitl usage section
.pipe(replace(/## Motivation[^]*/gm, '')) //exclude anything after "Motivation" section
.pipe(markdown({
options: {
html: true
}
}))
.pipe(replace(/<h(\d)>(.*)?(?=<\/h\d>)/gm, function(match, p1, p2) {
// console.log(match,+"\n\n\n\n", p1,+"\n\n\n\n", p2);
return '<h' + p1 + ' id="' + p2.toLowerCase().replace(/[^\w\s]/g, '').replace(/\s/g, '-') + '">' + p2;
})) //add IDs to headings
.pipe(replace('<pre', '<pre class="rainbow"'))
.pipe(replace('class="language-', 'data-language="')) //change code language markup
.pipe(replace('</pre>', '</pre><br>')) //add linebreak after code snippets
.pipe(rename('readme.html'))
.pipe(gulp.dest('demo-media'));
});
//Get a toc of the readme
gulp.task('toc', function() {
return gulp.src(['./README.md'])
.pipe(replace(/^#(?=[^#]).*/gm, '')) //exclude h1
.pipe(toc({
title: ' ',
}))
.pipe(replace(/<!-- END[^]*/g, '')) //remove comments of toc plugin
.pipe(replace(/<!-- .*/g, ''))
.pipe(markdown())
.pipe(rename('toc.html'))
.pipe(gulp.dest('demo-media'));
});
// Minify JS
gulp.task('buildJS', ['lint'], function() {
return gulp.src(paths.js.home + 'ripple.js')
.pipe(babel())
.pipe(uglify({
preserveComments: 'some'
}))
.pipe(rename('ripple.min.js'))
.pipe(header(headerText('ripple.min.js')))
.pipe(gulp.dest(paths.js.dest));
});
// Minify CSS
gulp.task('buildCSS', ['sass'], function() {
return gulp.src(paths.css.home + 'ripple.css')
.pipe(minifyCss())
.pipe(rename('ripple.min.css'))
.pipe(header(headerText('ripple.min.css')))
.pipe(gulp.dest(paths.css.dest));
});
//webserver
gulp.task('connect', function() {
connect.server({
port: 8000,
livereload: true
});
});
// Watch Files For Changes
gulp.task('watch', function() {
gulp.watch('**/*.html').on('change', function() {
gulp.src('**/*.html').pipe(connect.reload());
});
gulp.watch('README.md', ['inject-markdown']);
gulp.watch(paths.js.src + '*.js', ['lint']).on('change', function() {
gulp.src(paths.js.src + '*.js').pipe(connect.reload());
});
gulp.watch(paths.css.src + '*.scss', ['sass']);
gulp.watch(paths.css.src + '*.css').on('change', function() {
gulp.src(paths.css.src + '*.css').pipe(connect.reload());
});
});
gulp.task('default', ['watch', 'connect']);
gulp.task('build', ['lint', 'buildCSS', 'buildJS', 'inject-markdown']);