Initial framework for creating post indexes is in place. Functionality to build all unbuilt posts does not protect against re-building indexes and should be re-factored for efficiency
This commit is contained in:
parent
23265dce80
commit
11fd6d9aa2
6 changed files with 166 additions and 38 deletions
152
post.js
152
post.js
|
@ -26,6 +26,10 @@ Post.prototype.set = function (name, value) {
|
|||
this.data[name] = value;
|
||||
}
|
||||
|
||||
Post.prototype.toString = function () {
|
||||
return 'Post: ' + this.get('title') + ' - ' + this.getShortDate();
|
||||
}
|
||||
|
||||
Post.prototype.sanitize = function (data) {
|
||||
data = data || {};
|
||||
schema = schemas.post;
|
||||
|
@ -45,6 +49,7 @@ Post.prototype.save = function (date, callback) {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Prototype Functions
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -152,23 +157,42 @@ Post.prototype.makeSlug = function () {
|
|||
// Arguments: Callback function
|
||||
// Returns: err
|
||||
Post.prototype.build = function (callback) {
|
||||
console.log('Rendering ' + this);
|
||||
|
||||
year = moment(this.get("postDate")).format("YYYY");
|
||||
month = moment(this.get("postDate")).format("MM");
|
||||
|
||||
// Post.buildYearIndex(year, function (err) {
|
||||
// if (err) console.log(err);
|
||||
// });
|
||||
Post.buildMonthIndex(year, month, function (err) {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
|
||||
var options = {
|
||||
pretty: true,
|
||||
post: this
|
||||
};
|
||||
|
||||
console.log('Rendering post: '+this.get("title"));
|
||||
var jadeOut = jade.renderFile('views/render-post.jade', options);
|
||||
|
||||
self = this;
|
||||
|
||||
mkdirp(self.getDirectory(), function (err) {
|
||||
fs.writeFile(self.getFilePath(), jadeOut, 'utf-8', function (err) {
|
||||
self.set('lastBuildDate', new Date());
|
||||
self.save(self.get('lastBuildDate'), function (err) {
|
||||
callback(err);
|
||||
if (!err) {
|
||||
fs.writeFile(self.getFilePath(), jadeOut, 'utf-8', function (err) {
|
||||
if (!err) {
|
||||
self.set('lastBuildDate', new Date());
|
||||
self.save(self.get('lastBuildDate'), function (err) {
|
||||
callback(err);
|
||||
});
|
||||
} else {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -190,7 +214,7 @@ Post.getLastBuildDate = function (callback) {
|
|||
};
|
||||
|
||||
// Function to find the most recent post upload date.
|
||||
// Returns a date object
|
||||
// Returns: err, date object
|
||||
Post.getLastUploadDate = function (callback) {
|
||||
db.collection("posts").findOne({}, {lastUploadDate: 1}, {sort: {lastUploadDate: -1}},
|
||||
function (err, doc) {
|
||||
|
@ -201,15 +225,17 @@ Post.getLastUploadDate = function (callback) {
|
|||
|
||||
|
||||
// Get a specific post by it's UUID
|
||||
// Returns post object
|
||||
// Returns: err, post object
|
||||
Post.getByUUID = function (uuid, callback) {
|
||||
db.collection("posts").findOne({uuid: uuid}, function(err, doc) {
|
||||
callback(err, new Post(doc));
|
||||
post = new Post(doc);
|
||||
console.log('Retrieved ' + post);
|
||||
callback(err, post);
|
||||
});
|
||||
}
|
||||
|
||||
// Function to get a count of current posts
|
||||
// Returns count of posts
|
||||
// Returns: err, count of posts
|
||||
Post.countPosts = function(callback) {
|
||||
db.collection("posts").find({}).count(function (err, count) {
|
||||
if (err) console.log(err);
|
||||
|
@ -219,7 +245,7 @@ Post.countPosts = function(callback) {
|
|||
|
||||
// Function to get a list of posts of a certain count and starting at an offset
|
||||
// Count and start are optional, when start is specified count must be specified.
|
||||
// Returns a list of post objects
|
||||
// Returns: err, list of post objects
|
||||
Post.getPosts = function(count, start, callback) {
|
||||
if (typeof callback === undefined) {
|
||||
if (typeof start === undefined) {
|
||||
|
@ -239,18 +265,22 @@ Post.getPosts = function(count, start, callback) {
|
|||
options.sort = [['postDate', 'desc'],['title', 'asc']];
|
||||
|
||||
db.collection("posts").find({},options).toArray( function(err, docs) {
|
||||
console.log(err);
|
||||
posts = [];
|
||||
for (i=0; i<docs.length; i++) {
|
||||
posts.push(new Post(docs[i]));
|
||||
if (!err) {
|
||||
posts = [];
|
||||
for (i=0; i<docs.length; i++) {
|
||||
posts.push(new Post(docs[i]));
|
||||
}
|
||||
callback(null, posts);
|
||||
} else {
|
||||
console.log(err);
|
||||
callback(err, null);
|
||||
}
|
||||
callback(null, posts);
|
||||
});
|
||||
}
|
||||
|
||||
// Function to find posts that need to be built
|
||||
// Inputs: Callback function
|
||||
// Returns: list of Post objects
|
||||
// Returns: err, list of Post objects
|
||||
Post.getNeedsBuild = function (callback) {
|
||||
db.collection("posts").find({
|
||||
$where: "(this.lastBuildDate < this.updatedDate) && this.published"
|
||||
|
@ -268,7 +298,7 @@ Post.getNeedsBuild = function (callback) {
|
|||
|
||||
// Function to find posts that need to be uploaded
|
||||
// Inputs: Callback function
|
||||
// Returns: List of Post objects
|
||||
// Returns: err, List of Post objects
|
||||
Post.getNeedsUpload = function (callback) {
|
||||
db.collection("posts").find({
|
||||
$where: '(this.lastUploadDate < this.updatedDate) && \
|
||||
|
@ -289,5 +319,91 @@ Post.getNeedsUpload = function (callback) {
|
|||
);
|
||||
}
|
||||
|
||||
// Function to build an index for a specific month
|
||||
// Inputs: Year, Month, callback
|
||||
// Returns: err
|
||||
Post.buildMonthIndex = function (year, month, callback) {
|
||||
console.log('Rendering month index: '+year+'/'+month);
|
||||
|
||||
directory = config.buildDir
|
||||
directory += "/blog/";
|
||||
directory += year + '/' + month + '/';
|
||||
|
||||
console.log('Finding posts from '+year+'/'+month);
|
||||
mkdirp(directory, function (err) {
|
||||
if (!err) {
|
||||
db.collection('posts').find({
|
||||
$where: '(this.postDate >= new Date('+year+', '+month+'-1, 1, 0, 0, 0)) \
|
||||
&& (this.postDate <= new Date('+year+', '+month+', 0, 23, 59, 59))'
|
||||
}, {sort: [['postDate', 'desc'],['title', 'asc']]}
|
||||
).toArray(function (err, docs) {
|
||||
if (!err) {
|
||||
console.log('Found '+docs.length+' posts from '+year+'/'+month);
|
||||
posts = []
|
||||
for (i=0; i<docs.length; i++) {
|
||||
posts.push(new Post(docs[i]));
|
||||
}
|
||||
|
||||
var jadeOut = jade.renderFile('views/render-post-index.jade', {
|
||||
pretty: true,
|
||||
posts: posts,
|
||||
title: 'Posts from '+year+'/'+month
|
||||
});
|
||||
|
||||
file = path.join(directory, 'index.html');
|
||||
|
||||
fs.writeFile(file, jadeOut, 'utf-8', function (err) {
|
||||
callback(err);
|
||||
});
|
||||
} else {
|
||||
callback(err)
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Function to build an index for a specific year
|
||||
// Inputs: Year, callback
|
||||
// Returns: err
|
||||
Post.buildYearIndex = function (year, callback) {
|
||||
console.log('Rendering year index: '+year);
|
||||
|
||||
directory = config.buildDir
|
||||
directory += "/blog/";
|
||||
directory += year + '/';
|
||||
|
||||
start = new Date(year, 0, 1, 0, 0, 0);
|
||||
end = new Date((year+1), 0, 0, 23, 59, 59);
|
||||
console.log('Finding posts between '+start+' and '+end);
|
||||
|
||||
mkdirp(directory, function (err) {
|
||||
db.collection('posts').find({
|
||||
$where: '(this.postDate >= start) && (this.postDate <= end)'
|
||||
}).toArray(function (err, docs) {
|
||||
posts = []
|
||||
for (i=0; i<docs.length; i++) {
|
||||
posts.push(new Post(docs[i]));
|
||||
}
|
||||
var options = {
|
||||
pretty: true,
|
||||
posts: posts,
|
||||
title: 'Posts from '+year
|
||||
};
|
||||
var jadeOut = jade.renderFile('views/render-post-index.jade', options);
|
||||
|
||||
file = path.join(directory, 'index.html');
|
||||
|
||||
fs.writeFile(file, jadeOut, 'utf-8', function (err) {
|
||||
callback(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Export the Post object for external use.
|
||||
module.exports = Post;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
var config = require('./config.js').config;
|
||||
var markdown = require( "markdown" ).markdown;
|
||||
|
||||
// Post management routing
|
||||
|
||||
|
@ -34,7 +33,6 @@ module.exports = function(app) {
|
|||
successNotice: req.flash('successNotice'),
|
||||
failureNotice: req.flash('failureNotice'),
|
||||
post: post,
|
||||
content: markdown.toHTML(post.get("markdown")),
|
||||
user: req.user
|
||||
});
|
||||
});
|
||||
|
@ -77,7 +75,6 @@ module.exports = function(app) {
|
|||
}
|
||||
Category.getByName(req.body.category, function (err, category) {
|
||||
post.set("category", category.get("uuid"));
|
||||
//console.log(post);
|
||||
post.save(null, function (err) {
|
||||
if (!err) {
|
||||
req.flash('successNotice', 'Post created.');
|
||||
|
@ -156,19 +153,23 @@ module.exports = function(app) {
|
|||
if (err) console.log(err);
|
||||
});
|
||||
post = Post.getByUUID(req.params.uuid, function (err, post) {
|
||||
console.log("Got post: "+post.get("title"));
|
||||
if (err) console.log(err);
|
||||
post.build(function (err) {
|
||||
if (!err) {
|
||||
req.flash('successNotice', 'Post rebuilt successfully.');
|
||||
res.redirect(req.header('Referer') || '/admin');
|
||||
}
|
||||
else {
|
||||
console.log(err);
|
||||
req.flash('failureNotice', 'Post regeneration failed, check logs.');
|
||||
res.redirect(req.header('Referer') || '/admin');
|
||||
}
|
||||
});
|
||||
if (!err && post) {
|
||||
post.build(function (err) {
|
||||
if (!err) {
|
||||
req.flash('successNotice', 'Post rebuilt successfully.');
|
||||
res.redirect(req.header('Referer') || '/admin');
|
||||
}
|
||||
else {
|
||||
console.log(err);
|
||||
req.flash('failureNotice', 'Post regeneration failed, check logs.');
|
||||
res.redirect(req.header('Referer') || '/admin');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log(err);
|
||||
req.flash('failureNotice', 'Post regeneration failed, check logs.');
|
||||
res.redirect(req.header('Referer') || '/admin');
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -17,4 +17,4 @@ block content
|
|||
| -
|
||||
a(href="/admin/post/build/#{post.get('uuid')}") Build
|
||||
|
||||
div(class='row') !{content}
|
||||
div(class='row') !{post.renderMarkdown()}
|
||||
|
|
5
views/partial-post.jade
Normal file
5
views/partial-post.jade
Normal file
|
@ -0,0 +1,5 @@
|
|||
article
|
||||
header
|
||||
h3: a(href="#{post.getURL()}") #{post.get("title")}
|
||||
p #{post.getShortDate()}
|
||||
!{post.renderMarkdown()}
|
7
views/render-post-index.jade
Normal file
7
views/render-post-index.jade
Normal file
|
@ -0,0 +1,7 @@
|
|||
extends render-layout
|
||||
|
||||
block content
|
||||
h1 #{title}
|
||||
|
||||
each post in posts
|
||||
include ./partial-post.jade
|
|
@ -1,6 +1,5 @@
|
|||
extends render-layout
|
||||
|
||||
block content
|
||||
h3: a(href="#{post.getURL()}") #{post.get("title")}
|
||||
p #{post.getShortDate()}
|
||||
div #{post.renderMarkdown()}
|
||||
include ./partial-post.jade
|
||||
|
||||
|
|
Loading…
Reference in a new issue