/
glog.js
405 lines (318 loc) · 9.44 KB
/
glog.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Load required modules
*/
var path = require('path'),
fs = require('fs'),
marked = require('marked'),
jade = require('jade'),
exec = require('child_process').exec,
rss = require('rss');
/*
* Setup module
*/
module.exports = new Glog();
function Glog() {
/*
* Object variable to track pages
*/
this.pages = {};
};
/*
* Function to handle loading of configs
*/
Glog.prototype.load_configs = function(cb) {
// Set defaults
var options = {
blog_title : 'Glog Blog',
articles_per_page : 10,
show_author : false
},
fn = this,
key;
fs.readFile(path.join('glog_config.json'), 'UTF-8', function(err, data) {
if(err) {
fn.handle_error('Could not load configuration file. This should be named glog_config.json. Err: ' + err);
}
var configs = JSON.parse(data);
// Check mandatory configs
if(!configs.port) {
fn.handle_error('port is not defined in glog_config.json. This should be set to the port number you wish the blog to run on');
process.exit();
}
if(!configs.blog_repository) {
fn.handle_error('blog_repository is not defined in glog_config.json. This should point to the git repository of the blog content');
process.exit();
}
if(!configs.author) {
fn.handle_error('author is not defined in glog_config.json. This should be the default blog author');
process.exit();
}
for(key in configs) {
options[key] = configs[key];
}
cb(options);
});
}
/*
* Load the articles from disc and add them to an array
*/
Glog.prototype.load_articles = function(options, cb) {
var articles = [],
article_path = path.join('blog_repo', '/articles'),
fn = this;
console.log('Loading articles from ' + article_path);
fs.readdir(article_path, function(err, files) {
if(err) {
fn.handle_error('Could not read articles directory. Please make sure paths are set up correctly. Err: ' + err);
}
// Remove incorrect extensions
for(var i=0; i<files.length; i++) {
if(path.extname(files[i]) !== '.txt' && path.extname(files[i]) !== '.md') {
console.log('Skipping file ' + files[i] + ' Incorrect extension');
files.splice(i,1);
}
}
for(var i=0; i<files.length; i++) {
console.log('Reading file ' + files[i]);
fs.readFile(path.join(article_path, files[i]), 'UTF-8', function(err, data) {
if(err) {
fn.handle_error('Error reading article. Err: ' + err);
}
// Extract the header
var parts = data.split('\n\n');
var header = JSON.parse(parts[0]);
parts.splice(0,1);
var body = marked(parts.join('\n\n'));
console.log('Read articles with headers: ');
console.log(header);
// Check mandatory fields
if(!header.title) {
fn.handle_error('Articles must have a title');
cb(false);
return;
}
if(!header.date) {
fn.handle_error('Articles must have a date');
cb(false);
return;
}
// Extract date
var date = new Date(header.date),
year = date.getFullYear(),
month = date.getMonth() + 1; // Month is + 1 as starts at 0
// If month < 10 prepend a 0
if(month < 10) {
month = ['0', month].join('');
}
articles.push({
'title' : header.title,
'author' : header.author || options.author,
'date' : date,
'date_str' : Glog.formatDate(date),
'year' : year,
'month' : month,
'body' : body,
'url' : [year, month, (header.url || header.title.replace(/\s/g, '-'))].join('/')
});
// If that was the last article then trigger the callback
if(files.length === articles.length) {
cb(articles.reverse());
}
});
}
});
};
/*
* Create the RSS feed
*/
Glog.prototype.render_rss = function(options, articles, cb) {
var feed = new rss({
title : options.blog_title,
feed_url : (options.base_url || '') + '/rss.xml',
site_url : (options.base_url || ''),
author : options.author
}),
len = articles.length, i;
for(i=0;i<len;i++) {
feed.item({
title : articles[i].title,
description : articles[i].body,
url : articles[i].url,
author : articles[i].author,
date : articles[i].date
});
}
this.pages['rss'] = feed.xml();
cb();
};
/*
* Render the entire blog and store pages in cache
*/
Glog.prototype.render_blog = function(options, articles, cb) {
var fn = this;
fs.readFile(path.join('blog_repo', 'layout.jade'), 'UTF-8', function(err, data) {
if(err) {
fn.handle_error('Error loading layout. Err: ' + err);
}
console.log('Compiling template for home page');
var ja = jade.compile(data),
per_page = options.articles_per_page,
i, len = Math.floor((articles.length / per_page) + 1),
start, end;
for(i = 0; i < len; i++) {
start = (per_page * i);
end = articles.length < (per_page * (i + 1)) ? articles.length : (per_page * (i + 1));
fn.pages['_page' + (i+1)] = ja({
'title' : options.blog_title,
'analytics_code' : options.analytics_code || null,
'base_url' : options.base_url || '',
'show_author' : options.show_author,
'articles' : articles.slice(start, end),
'page_num' : end === articles.length ? -1 : (i+1) // If we are on the last page, pass -1 so we know there are no more pages
});
// pages[/] === pages[_page1]
fn.pages['/'] = fn.pages['_page1'];
}
console.log('Compiling template for individual pages');
// Loop through articles and compile individual pages
for(var i=0; i< articles.length; i++) {
fn.pages[articles[i].url] = ja({
'title' : articles[i].title,
'articles' : [articles[i]],
'disqus_id' : options.disqus_id || -1,
'analytics_code' : options.analytics_code || null,
'base_url' : options.base_url || '',
'show_author' : options.show_author,
'single' : true
});
}
cb();
});
};
/*
* Handle a request for the home page
*/
Glog.prototype.req_home = function(req, res, next, options) {
console.log('Home page requested');
var pagenum = req.params.pagenum || 1;
console.log('Rendering page ' + pagenum);
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
if(typeof this.pages['_page' + pagenum] !== 'undefined') {
res.end(this.pages['_page' + pagenum]);
} else {
res.end(this.pages['/']);
}
};
/*
* Handle a request for the RSS feed
*/
Glog.prototype.req_rss = function(req, res, next, options) {
console.log('RSS feed requested');
// Set headers
res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8');
res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
res.end(this.pages['rss']);
};
/*
* Handle a request for an individual article
*/
Glog.prototype.req_article = function(req, res, next, options) {
var fn = this,
url = [req.params.year, req.params.month, req.params.article].join('/');
console.log('Requesting ' + url);
if(typeof this.pages[url] === 'undefined') {
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
res.end(this.pages['/']);
} else {
// Set headers
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Cache-Control', options.cache_time || 'no-cache');
res.setHeader('Server', 'Glog (NodeJS)');
res.end(this.pages[url]);
}
};
/*
* Generic error handler
*/
Glog.prototype.handle_error = function(err) {
console.error(err);
}
/*
* Function to trigger a build of the entire site
*/
Glog.prototype.rebuild = function(cb) {
var fn = this,
next;
fn.load_configs(function(options) {
fn.update_repo(options, function(err) {
fn.load_articles(options, function(articles) {
articles = articles.sort(function(a,b) {
return b.date - a.date;
});
fn.render_rss(options, articles, function() {
fn.render_blog(options, articles, cb);
});
});
});
});
};
/*
* Clone a blog git repository
*/
Glog.prototype.get_repo = function(repo, cb) {
var fn = this;
// Attempt to clone the git repo
console.log('Cloning git repository ' + repo);
exec('git clone ' + repo + ' blog_repo', function(error, stdout, stderr) {
if(error) {
fn.handle_error('Could not clone git repository: ' + error);
cb(false);
}
console.log('Stdout: ' + stdout);
console.log('Stderr: ' + stderr);
cb(true);
});
};
/*
* Update git repository
*/
Glog.prototype.update_repo = function(options, cb) {
var fn = this;
// If repo does not exist, clone it
fs.stat('blog_repo', function(err, stats) {
if(err) {
fn.handle_error(err);
}
if(err || !stats.isDirectory()) {
fn.get_repo(options.blog_repository, cb);
} else {
console.log('Updating blog git repo');
exec('cd blog_repo; git pull origin master', function(error, stdout, stderr) {
if(err) {
fn.handle_error('Could not update blog repo: ' + error);
cb(false);
}
console.log('Stdout: ' + stdout);
console.log('Stderr: ' + stderr);
cb(true);
});
}
});
};
/*
* Format a date
*/
Glog.formatDate = function(date) {
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
months = ['January' , 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
str;
str = [days[date.getDay()-1], ', ', date.getDate(), ' ', months[date.getMonth()], ', ', date.getFullYear()].join('');
return str;
};