-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgulpfile.js
286 lines (239 loc) · 7.92 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
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
var gulp = require('gulp');
var _ = require('underscore');
var fs = require('fs');
var serve = require('gulp-serve');
var slug = require('slug');
var Handlebars = require('handlebars');
var Sitemap = require('sitemap');
// Wiki Bot
var bot = require('nodemw');
var client = new bot({
server: 'wiki.nuitdebout.fr',
// path: 'api.php',
debug: true
});
function getCities(callback)
{
var cities = [];
client.getArticle('Villes', function(err, article) {
var matches = article.match(/\[\[([^\]]+)/g).forEach(function(city) {
city = city.replace('[[', '');
var pieces = city.split('|')
, wiki_uri = pieces[0]
, label = pieces[1];
try {
cities.push({
slug: slug(label, {lower: true}),
uri: '/ville/' + slug(label, {lower: true}),
wiki_uri: wiki_uri,
wiki_url: 'https://wiki.nuitdebout.fr/wiki/'+wiki_uri,
links: [],
label: label,
name: label
});
} catch (e) {
console.log(e)
}
});
var agendaRegex = /== Calendrier ==([^=]+)/g;
var linkRegex = /(https?:\/\/[^ }=\r\n]+)/g;
function getCityDetails(city, cb) {
client.getArticle(city.wiki_uri, function(err, cityArticle) {
var matches;
if (matches = linkRegex.exec(cityArticle)) {
matches.forEach(function(link) {
city.links.push(link);
});
}
city.links = _.uniq(city.links);
cb(city);
});
}
function eachAsync(arr, func, cb) {
var doneCounter = 0,
results = [];
arr.forEach(function (item) {
func(item, function (res) {
doneCounter += 1;
results.push(res);
if (doneCounter === arr.length) {
cb(results);
}
});
});
}
eachAsync(cities, getCityDetails, function(results) {
callback.apply(undefined, [results]);
});
});
}
function getLastReports(callback)
{
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
client.getRecentChanges(yesterday.getTime(), function(err, changes) {
var titles = []
, matches = [];
changes.forEach(function(change) {
if (change.type == 'new' && (change.title.match(/Villes\/.*\/CR/g) || change.title.match(/Villes\/.*\/AG/g))) {
var label, city;
if (matches = /^Villes\/([^\/]+)/g.exec(change.title)) {
city = matches[1];
}
if (matches = /.*\/(.*)$/g.exec(change.title)) {
label = matches[1];
}
if (city && label) {
titles.push({
url: 'https://wiki.nuitdebout.fr/wiki/'+change.title,
city: city,
label: label
});
}
}
});
callback.apply(undefined, _.uniq([titles]));
})
}
/**
* Retrieves the list of cities from the wiki and creates a JSON file.
*/
gulp.task('import:cities', function(cb) {
getCities(function(newCities) {
var currentCities = JSON.parse(fs.readFileSync('data/cities.json').toString());
// Merge links
var cities = newCities.map(function(newCity) {
var currentCity = _.find(currentCities, function(currentCity) {
return currentCity.slug === newCity.slug;
})
if (currentCity) {
newCity.links = _.uniq(currentCity.links.concat(newCity.links));
}
return newCity;
});
fs.writeFile('data/cities.json', JSON.stringify(cities, null, 2), function(err) {
if (err) {
return console.log(err);
}
cb();
});
});
});
gulp.task('import:reports', function() {
getLastReports(function(titles) {
fs.writeFile('data/reports.json', JSON.stringify(titles, null, 2), function(err) {
if (err) {
return console.log(err);
}
});
});
});
gulp.task('import', ['import:cities', 'import:reports']);
var callSlugs = {
"ca": "crida-internacional-de-nuit-debout",
"el": slug("Διεθνές-Κάλεσμα-του-κινήματος-nuit-debout", {lower: true}),
"en": "international-call-by-nuit-debout",
"es": "llamada-internacional-de-nuit-debout",
"it": "appello-internazionale-di-nuit-debout",
"fr": "appel-international-de-nuit-debout",
"hr": "Pozivamo-na-GLOBALDEBOUT",
"rs": "meunarodni-poziv-nuitdebout",
"ro": "APEL-INTERNAȚIONAL-PENTRU-NUITDEBOUT",
"sq": "THIRRJE-NDERKOMBETARE-PER-NUITDEBOUT",
"de": "internationaler-Aufruf-von-NuitDebout"
}
gulp.task('sitemap', function() {
var cities = JSON.parse(fs.readFileSync('data/cities.json').toString());
var urls = [
{url: '/', changefreq: 'daily'}
];
_.each(callSlugs, function(slug, language) {
var uri = ((language === 'fr' ? '/'+slug : '/'+language+'/'+slug)+'.html')
urls.push({url: uri, changefreq: 'daily'});
})
var citiesURLs = cities.map(function(city) {
return {
url: '/ville/' + city.slug,
changefreq: 'daily'
}
});
urls = urls.concat(citiesURLs);
var sitemap = Sitemap.createSitemap ({
hostname: 'http://www.nuitdebout.fr',
urls: urls
});
fs.writeFileSync('./sitemap.xml', sitemap.toString());
});
/**
* Generates the index.html file using Handlebars.
*/
gulp.task('website', ['website:international'], function() {
Handlebars.registerPartial('header', fs.readFileSync('templates/partials/header.hbs').toString());
Handlebars.registerPartial('footer', fs.readFileSync('templates/partials/footer.hbs').toString());
var tplIndex = Handlebars.compile(fs.readFileSync('templates/index.hbs').toString());
var tplCity = Handlebars.compile(fs.readFileSync('templates/city.hbs').toString());
var cities = JSON.parse(fs.readFileSync('data/cities.json').toString());
var reports = JSON.parse(fs.readFileSync('data/reports.json').toString());
var websites = JSON.parse(fs.readFileSync('data/websites.json').toString());
// Create an array of arrays of 2 cities
var citiesTwoPerLine = [];
var arrayOfTwo = [];
cities.forEach(function(city) {
if (arrayOfTwo.length < 2) {
return arrayOfTwo.push(city);
}
citiesTwoPerLine.push(arrayOfTwo);
arrayOfTwo = [];
arrayOfTwo.push(city);
});
var data = {
citiesTwoPerLine: citiesTwoPerLine,
cities: cities,
reports: reports,
callSlugs: callSlugs
};
fs.writeFileSync('index.html', tplIndex(data));
cities.forEach(function(city) {
var dir = './ville/' + slug(city.label, {lower: true});
var file = dir + '/index.html';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
var hasOwnWebsite = websites.hasOwnProperty(city.slug)
, websiteURL = hasOwnWebsite ? websites[city.slug] : undefined;
var tpl = tplCity;
if (fs.existsSync('./templates/cities/'+city.slug+'.hbs')) {
tpl = Handlebars.compile(fs.readFileSync('./templates/cities/'+city.slug+'.hbs').toString());
}
city = _.extend(city, {
hasOwnWebsite: hasOwnWebsite,
websiteURL: websiteURL
});
fs.writeFileSync(file, tpl({
city: city
}));
})
});
gulp.task('website:international', function() {
Handlebars.registerPartial('header', fs.readFileSync('templates/partials/header.hbs').toString());
Handlebars.registerPartial('footer', fs.readFileSync('templates/partials/footer.hbs').toString());
var files = fs.readdirSync('./templates/international').forEach(function(filename) {
var tplLanguage = Handlebars.compile(fs.readFileSync('./templates/international/'+filename).toString());
var language = filename.replace('.hbs', '');
if (!fs.existsSync(language)) {
fs.mkdirSync(language);
}
if (callSlugs.hasOwnProperty(language)) {
var slug = callSlugs[language];
if (language === 'fr') {
fs.writeFileSync('./'+slug+'.html', tplLanguage({}));
} else {
fs.writeFileSync('./'+language+'/'+slug+'.html', tplLanguage({}));
}
}
});
});
gulp.task('watch', function() {
gulp.watch(['templates/*.hbs', 'templates/partials/*.hbs', 'templates/international/*.hbs', 'templates/cities/*.hbs'], ['website'])
});
gulp.task('serve', ['watch'], serve('./'));