Skip to content

Instantly share code, notes, and snippets.

@tysoncadenhead
Created July 30, 2013 20:21
Show Gist options
  • Save tysoncadenhead/6116565 to your computer and use it in GitHub Desktop.
Save tysoncadenhead/6116565 to your computer and use it in GitHub Desktop.
var server = require('./server');
server.listen(3000);
var html2text = require('html-to-text');
module.exports = function (core, app, poet) {
var postsPerPage = 10;
/**
* Blog listing
*/
app.get('/', function (req, res) {
var start = (req.query.page || 0) * postsPerPage,
end = start + postsPerPage,
category = req.query.category,
tag = req.query.tag,
posts, pages, filter = {};
if (category) {
filter.category = category;
pages = poet.helpers.postsWithCategory(category).length / postsPerPage;
} else if (tag) {
filter.tag = tag;
pages = poet.helpers.postsWithTag(tag).length / postsPerPage;
} else {
pages = poet.helpers.getPosts().length / postsPerPage;
}
posts = poet.helpers.filterPaginated(filter, start, end);
posts.forEach(function (post) {
post.rssDescription = html2text.fromString(post.preview);
});
res.render('index', {
posts: posts,
page: req.query.page || 0,
pages: pages,
category: category,
tag: tag
});
});
/**
* Returns the related posts for a post
*/
app.get('/api/related', function (req, res) {
var title = req.query.title,
post = poet.helpers.getPost(title);
res.send({
posts: poet.helpers.getRelatedPosts(post, 3)
});
});
/**
* About Page
*/
app.get( '/about', function (req, res) {
res.render('about');
});
/**
* RSS feed
*/
app.get('/feed', function (req, res) {
var posts = poet.helpers.filterPaginated({}, 0, postsPerPage);
posts.forEach(function (post) {
post.rssDescription = html2text.fromString(post.preview);
});
res.render( 'rss', { posts: posts });
});
/**
* Sitemap
*/
app.get('/sitemap.xml', function (req, res) {
var posts = poet.helpers.filterPaginated({});
res.render( 'sitemap', { posts: posts });
});
};
var Poet = require('poet'),
_ = require('underscore');
module.exports = function (app) {
var poet = Poet(app, {
postsPerPage: 10,
posts: '_posts',
metaformat: 'json',
routes: {
'/blog/:post': 'post',
'/pages/:page': 'page',
'/tags/:tag': 'tag',
'/catagories/:category': 'category'
}
});
function filterList (post, options) {
var display = true;
// Filter based on the tags
if (options.tag && !~JSON.stringify(post.tags).toLowerCase().indexOf(options.tag.toLowerCase())){
display = false;
}
// Filter based on the category
if (options.category && post.category !== options.category) {
display = false;
}
// Don't display if published is false
if (post.published === false) {
display = false;
}
// Don't display if the publish date is in the future
if (post.date > new Date()) {
display = false;
}
return display;
}
poet.watch(function (poet) {
poet.clearCache();
});
/**
* Filter and paginate list of pages
*/
poet.helpers.filterPaginated = function (options, from, to) {
var posts = poet.helpers.getPosts(poet).filter(function (post) {
return filterList(post, options);
});
if (from != null && to != null) {
posts = posts.slice(from, to);
}
return posts;
};
/**
* Return posts that are related
*/
poet.helpers.getRelatedPosts = function (post, number) {
var posts = [];
if (post) {
if (posts.length < number + 1) {
for (var i = 0; i < post.tags.length; i++) {
posts = _.unique(posts.concat(
poet.helpers.filterPaginated({
tag: post.tags[i]
})
));
}
}
if (posts.length < number + 1) {
posts = _.unique(posts.concat(
poet.helpers.filterPaginated({
category: post.category
})
));
}
if (posts.length < number + 1) {
posts = _.unique(posts.concat(poet.helpers.filterPaginated(poet)));
}
}
for (var i = 0; i < posts.length; i++) {
if (posts[i].title === post.title) {
posts.splice(i, 1);
}
}
return _.shuffle(posts).slice(0, number);
};
return poet;
};
var
express = require( 'express' ),
app = express(),
controller = require('./app/controller.js'),
poet = require('./app/poet.js')(app);
app.set( 'view engine', 'ejs' );
app.set( 'views', __dirname + '/views' );
app.use( express.static( __dirname + '/public' ));
app.use( app.router );
app.locals.pretty = true;
poet.init(setupRoutes);
function setupRoutes ( core ) {
controller(core, app, poet);
};
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment