2013-05-11 20:44:25 +04:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
var Ghost = require('../../ghost'),
|
|
|
|
api = require('../api'),
|
|
|
|
RSS = require('rss'),
|
|
|
|
_ = require('underscore'),
|
2013-09-17 04:54:36 +04:00
|
|
|
errors = require('../errorHandling'),
|
2013-09-24 14:46:30 +04:00
|
|
|
when = require('when'),
|
2013-09-26 16:52:53 +04:00
|
|
|
url = require('url'),
|
|
|
|
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
ghost = new Ghost(),
|
2013-06-25 15:43:15 +04:00
|
|
|
frontendControllers;
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
frontendControllers = {
|
2013-09-17 04:54:36 +04:00
|
|
|
'homepage': function (req, res, next) {
|
2013-08-18 21:41:55 +04:00
|
|
|
// Parse the page number
|
2013-09-05 16:55:28 +04:00
|
|
|
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
2013-09-15 21:52:37 +04:00
|
|
|
postsPerPage = parseInt(ghost.settings('postsPerPage'), 10),
|
2013-09-05 16:55:28 +04:00
|
|
|
options = {};
|
2013-08-20 06:18:39 +04:00
|
|
|
|
|
|
|
// No negative pages
|
2013-09-05 16:55:28 +04:00
|
|
|
if (isNaN(pageParam) || pageParam < 1) {
|
|
|
|
//redirect to 404 page?
|
2013-09-24 14:46:30 +04:00
|
|
|
return res.redirect('/');
|
2013-08-20 06:18:39 +04:00
|
|
|
}
|
2013-09-05 16:55:28 +04:00
|
|
|
options.page = pageParam;
|
|
|
|
|
2013-09-06 06:09:47 +04:00
|
|
|
// Redirect '/page/1/' to '/' for all teh good SEO
|
|
|
|
if (pageParam === 1 && req.route.path === '/page/:page/') {
|
|
|
|
return res.redirect('/');
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:55:28 +04:00
|
|
|
// No negative posts per page, must be number
|
|
|
|
if (!isNaN(postsPerPage) && postsPerPage > 0) {
|
|
|
|
options.limit = postsPerPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
api.posts.browse(options).then(function (page) {
|
2013-08-21 18:05:17 +04:00
|
|
|
var maxPage = page.pages;
|
|
|
|
|
|
|
|
// A bit of a hack for situations with no content.
|
|
|
|
if (maxPage === 0) {
|
|
|
|
maxPage = 1;
|
|
|
|
page.pages = 1;
|
|
|
|
}
|
|
|
|
|
2013-08-18 21:41:55 +04:00
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
2013-08-21 18:05:17 +04:00
|
|
|
if (pageParam > maxPage) {
|
2013-09-06 06:09:47 +04:00
|
|
|
return res.redirect(maxPage === 1 ? '/' : ('/page/' + maxPage + '/'));
|
2013-08-18 21:41:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page of posts
|
2013-09-15 01:34:12 +04:00
|
|
|
ghost.doFilter('prePostsRender', page.posts).then(function (posts) {
|
2013-06-25 19:13:19 +04:00
|
|
|
res.render('index', {posts: posts, pagination: {page: page.page, prev: page.prev, next: page.next, limit: page.limit, total: page.total, pages: page.pages}});
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
2013-09-17 04:54:36 +04:00
|
|
|
}).otherwise(function (err) {
|
|
|
|
return next(new Error(err));
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
},
|
2013-09-17 04:54:36 +04:00
|
|
|
'single': function (req, res, next) {
|
2013-06-25 15:43:15 +04:00
|
|
|
api.posts.read({'slug': req.params.slug}).then(function (post) {
|
2013-09-17 04:54:36 +04:00
|
|
|
if (post) {
|
2013-09-15 01:34:12 +04:00
|
|
|
ghost.doFilter('prePostsRender', post).then(function (post) {
|
2013-09-17 04:54:36 +04:00
|
|
|
res.render('post', {post: post});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
}).otherwise(function (err) {
|
|
|
|
return next(new Error(err));
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-08-28 02:31:43 +04:00
|
|
|
},
|
2013-09-17 04:54:36 +04:00
|
|
|
'rss': function (req, res, next) {
|
2013-08-28 02:31:43 +04:00
|
|
|
// Initialize RSS
|
2013-09-11 19:23:07 +04:00
|
|
|
var siteUrl = ghost.config().url,
|
2013-09-16 15:11:17 +04:00
|
|
|
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
|
|
|
|
feed;
|
|
|
|
//needs refact for multi user to not use first user as default
|
|
|
|
api.users.read({id : 1}).then(function (user) {
|
2013-09-02 05:29:56 +04:00
|
|
|
feed = new RSS({
|
2013-09-15 21:52:37 +04:00
|
|
|
title: ghost.settings('title'),
|
|
|
|
description: ghost.settings('description'),
|
2013-09-14 15:12:27 +04:00
|
|
|
generator: 'Ghost v' + res.locals.version,
|
2013-10-17 19:15:25 +04:00
|
|
|
author: user ? user.name : null,
|
2013-09-26 16:52:53 +04:00
|
|
|
feed_url: url.resolve(siteUrl, '/rss/'),
|
2013-09-02 05:29:56 +04:00
|
|
|
site_url: siteUrl,
|
|
|
|
ttl: '60'
|
2013-09-16 15:11:17 +04:00
|
|
|
});
|
2013-08-28 02:31:43 +04:00
|
|
|
|
2013-09-16 15:11:17 +04:00
|
|
|
// No negative pages
|
|
|
|
if (isNaN(pageParam) || pageParam < 1) {
|
2013-09-24 14:46:30 +04:00
|
|
|
return res.redirect('/rss/');
|
2013-08-28 02:31:43 +04:00
|
|
|
}
|
|
|
|
|
2013-09-16 15:11:17 +04:00
|
|
|
if (pageParam === 1 && req.route.path === '/rss/:page/') {
|
|
|
|
return res.redirect('/rss/');
|
2013-08-28 02:31:43 +04:00
|
|
|
}
|
|
|
|
|
2013-09-16 15:11:17 +04:00
|
|
|
api.posts.browse({page: pageParam}).then(function (page) {
|
|
|
|
var maxPage = page.pages;
|
|
|
|
|
|
|
|
// A bit of a hack for situations with no content.
|
|
|
|
if (maxPage === 0) {
|
|
|
|
maxPage = 1;
|
|
|
|
page.pages = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
|
|
|
if (pageParam > maxPage) {
|
2013-09-24 14:46:30 +04:00
|
|
|
return res.redirect('/rss/' + maxPage + '/');
|
2013-09-16 15:11:17 +04:00
|
|
|
}
|
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
ghost.doFilter('prePostsRender', page.posts).then(function (posts) {
|
2013-09-16 15:11:17 +04:00
|
|
|
posts.forEach(function (post) {
|
|
|
|
var item = {
|
2013-09-26 16:52:53 +04:00
|
|
|
title: _.escape(post.title),
|
|
|
|
guid: post.uuid,
|
|
|
|
url: siteUrl + '/' + post.slug + '/',
|
|
|
|
date: post.published_at,
|
|
|
|
},
|
|
|
|
content = post.html;
|
|
|
|
|
|
|
|
//set img src to absolute url
|
|
|
|
content = content.replace(/src=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) {
|
|
|
|
p1 = url.resolve(siteUrl, p1);
|
|
|
|
return "src='" + p1 + "' ";
|
|
|
|
});
|
|
|
|
//set a href to absolute url
|
|
|
|
content = content.replace(/href=["|'|\s]?([\w\/\?\$\.\+\-;%:@&=,_]+)["|'|\s]?/gi, function (match, p1) {
|
|
|
|
p1 = url.resolve(siteUrl, p1);
|
|
|
|
return "href='" + p1 + "' ";
|
|
|
|
});
|
|
|
|
item.description = content;
|
2013-09-16 15:11:17 +04:00
|
|
|
feed.item(item);
|
|
|
|
});
|
|
|
|
res.set('Content-Type', 'text/xml');
|
|
|
|
res.send(feed.xml());
|
2013-08-28 02:31:43 +04:00
|
|
|
});
|
|
|
|
});
|
2013-09-17 04:54:36 +04:00
|
|
|
}).otherwise(function (err) {
|
|
|
|
return next(new Error(err));
|
2013-08-28 02:31:43 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-10-18 21:18:49 +04:00
|
|
|
module.exports = frontendControllers;
|