-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgulpfile.js
238 lines (224 loc) · 7.39 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
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
import browserSync from 'browser-sync';
import gulp from 'gulp';
import gulpPlumber from 'gulp-plumber';
import pug from 'gulp-pug';
import replace from 'gulp-replace';
import prettier from 'gulp-prettier';
import gulpSitemap from 'gulp-sitemap';
import File from 'vinyl';
import collection from 'gulp-collection';
import matter from '@rojo2/gulp-gray-matter';
import sort from 'gulp-sort';
import fs from 'fs';
import rename from 'gulp-rename';
import through from'through2';
import engine from 'pug';
let pugPath = "";
let contextTags = [];
function loadTemplate(name) {
return new File({
path: name,
contents: fs.readFileSync(name)
});
}
function buffer() {
var files = [];
return through.obj(function(file, enc, next) {
files.push(file);
next(null);
}, function(cb) {
var stream = this;
files.forEach(function(file) {
stream.push(file);
});
cb();
});
}
const build_blog = () => {
var blogTemplate = loadTemplate('./_src/_components/templates/blog.pug');
var postTemplate = loadTemplate('./_src/_components/templates/post.pug');
return gulp.src('./_src/_blog/*.pug')
.pipe(matter({
excerpt: true,
excerpt_separator: '<!-- end -->'
}))
// sort by published date
.pipe(sort({
comparator: function(file1, file2) {
return new Date(file2.data.date) - new Date(file1.data.date);
}
}))
.pipe(collection(':tags/:tag/:pager.idx/index.html', {
item: blogTemplate,
paginate: {limit: 10},
groupFn: function(group) {
contextTags = Object.keys(group)
// configure a new pager (for all blog posts) via using a virtual "blog" tag
let blog = {}
for (let tag in group) {
for (let key in group[tag]) {
blog[group[tag][key].path] = group[tag][key]
}
}
group.blog = Object.values(blog)
group.blog.sort(function(file1, file2) {
return new Date(file2.data.date) - new Date(file1.data.date);
})
}
}))
// buffer files before rendering to ensure context has been created
.pipe(buffer())
.pipe(through.obj(function(file, enc, next) {
// configure a blog post template
if (file.data.items) {
file.data.items.sort(function(file1, file2) {
return new Date(file2.data.date) - new Date(file1.data.date);
})
}
else {
file.data.post = engine.compile(file.contents.toString())()
if (file.data.excerpt != undefined) {
file.data.more = true
file.data.excerpt = engine.compile(file.data.excerpt)()
}
else {
file.data.more = false
file.data.excerpt = file.data.post
}
file.contents = postTemplate.contents
file.data.context = contextTags
file.data.format_date = new Date(file.data.date).toLocaleDateString('en-US', {month: 'long', day: 'numeric', year: 'numeric'})
}
next(null, file);
}))
.pipe(pug({
pretty: false,
}))
.pipe(prettier({
singleQuote: true,
parser:"html",
tabWidth: 2,
useTabs: false,
htmlWhitespaceSensitivity:"css",
printWidth:240,
}))
.on('data', function(file){
console.log('▒ PUG→HTML: ' + file.path.replace(file.cwd, '') );
})
.pipe(rename(function(path) {
// configure blog folders' structure
path.dirname = path.dirname.replace('tags/', '');
path.dirname = path.dirname.replace('blog/0', '');
path.dirname = path.dirname.replace('blog/', '');
path.dirname = path.dirname.replace('/0', '');
}))
.pipe(gulp.dest('blog'));
}
//Compiler PUG -> HTML
const html = () => {
let pugTruePath = './_src/**/' + pugPath;
if(!pugPath || pugPath.includes("_")){
pugTruePath = ['./_src/**/*.pug', '!./_src/_*/**'];
}
return gulp.src(pugTruePath)
.pipe(gulpPlumber())
.pipe(pug({
pretty: false,
}))
.pipe(replace('"../img', '"/img'))
.pipe(prettier({
singleQuote: true,
parser:"html",
tabWidth: 2,
useTabs: false,
htmlWhitespaceSensitivity:"css",
printWidth:240,
}))
.on('data', function(file){
console.log('▒ PUG→HTML: ' + file.path.replace(file.cwd, '') );
})
.pipe(gulp.dest('./')).on('end', (e) => {
if(!pugPath || pugPath.includes("_blog") || pugPath.includes("_components")){
build_blog()
}
});
}
//Filewatcher and live-reload
export const watchpug = () => {
browserSync.init({
server: './',
ui: false,
injectChanges: true,
watch: false,
});
gulp.watch(['_src/**/*.pug', '_src/**/*.html'], {}).on('change', function(pathPug, stats){
pugPath = null;
console.log("Modified file: " + pathPug);
if(!pathPug.includes("_components")){
let pathArray = pathPug.split("\\");
let filename = pathArray[pathArray.length - 1];
pugPath = filename;
}
html();
});
gulp.watch([
'./css/**/*.css',
'./js/**/*.js',
'./*.html',
'./features/*.html',
'./arch/*.html',
'./use-cases/*.html',
], {}).on('change', function(path) {
browserSync.reload(path);
});
}
/**
* XML Sitemap Generate for site and /docs/latest folder
*/
export const sitemap = () => {
let srcForSitemap = [
'./**/*.html',
'!./_*/**',
'!./jcache/**',
'!./releases/**',
'!./docs/2.9.0/**',
'!./docs/2.9.1/**',
'!./docs/2.10.0/**',
'!./docs/2.11.0/**',
'!./docs/2.11.1/**',
'!./docs/2.12.0/**',
'!./docs/2.13.0/**',
'!./docs/2.14.0/**',
'!./docs/2.15.0/**',
'!./docs/3.0.0-beta/**',
'!./docs/extensions/**',
'!./node_modules/**'
];
return gulp.src(srcForSitemap, {
read: false
})
.pipe(gulpSitemap({
siteUrl: 'https://ignite.apache.org',
changefreq: 'monthly',
priority: function(siteUrl, loc, entry) {
// Give pages inside root path (i.e. no slashes) a higher priority
return loc.includes('docs') ? 0.7 : 1;
},
getLoc: function(siteUrl, loc, entry) {
if(loc.includes('/docs/')){
//Remove .html for all docs files
let newloc = loc.replace(/.html$/, "")
//Change version folders into "latest"
newloc = newloc.replace("2.16.0", "latest");
//console.log(newloc);
return newloc;
}
return loc;
}
}))
.pipe(gulp.dest('./'));
}
export const blog = gulp.series(build_blog);
export const build = gulp.series(html);
export const watch = gulp.series(watchpug);
export default gulp.series(html, watchpug);