blogs
Version:
A simple blog generator.
68 lines (62 loc) • 1.72 kB
JavaScript
var fs = require('fs');
var path = require('path');
var async = require('async');
var bMods = require('blogs_modules');
var forEachKey = bMods.forEachKey;
var Cache = bMods.Cache;
var leftpad = bMods.leftpad;
class Post {
constructor(blog, file) {
this.blog = blog;
this.file = file;
this.cache = new Cache();
}
load(callback) {
this.cache.get('load', (callback) => {
async.waterfall([
(callback) => {
async.parallel([
(callback) => {
fs.readFile(this.file, callback);
},
(callback) => {
this.blog.plugins('processor', callback);
}
], callback);
},
(results, callback) => {
var [buffer, processors] = results;
var type = path.extname(this.file).replace(/\./, '').toLowerCase();
var processor = processors[type];
if (processor) {
processor.call(this, buffer, callback);
} else {
callback(new Error('There is no ' + type + ' processor'));
}
},
(data, callback) => {
forEachKey(data, (value, key) => {
this[key] = value;
});
this.Y = this.date.getFullYear();
this.YY = leftpad(this.Y % 100);
this.YYYY = leftpad(this.Y, 4);
this.M = this.date.getMonth() + 1;
this.MM = leftpad(this.M);
this.D = this.date.getDate();
this.DD = leftpad(this.D);
this.T = this.title
.toLowerCase()
.replace(/^[^0-9a-z-]*|[^0-9a-z-]*$/g, '')
.replace(/[^0-9a-z-]+/g, '-');
this.blog.render(this.blog.permalink, null, this, callback);
},
(link, callback) => {
this.permalink = ('/' + this.blog.root + link).replace(/\/+/g, '/');
callback(null, this);
}
], callback);
}, callback);
}
}
module.exports = Post;