Skip to content
Browse files

Limit the number of articles displayed per page.

The number of articles displayed on the main pages is now limited
according to the config item articles_per_page.  The default is 10, but
this can be overwritten in the glog_confing.json file.  Older articles
can be accessed at /page/1, /page/2, /page/3 etc
  • Loading branch information...
1 parent 9094b13 commit 2721f512cf797099418d901b510652c943cea17c Guy Halford-Thompson committed Feb 13, 2012
Showing with 50 additions and 24 deletions.
  1. +9 −8 README.md
  2. +7 −6 glog_config.sample.json
  3. +31 −10 lib/glog.js
  4. +3 −0 server.js
View
17 README.md
@@ -81,14 +81,15 @@ The entire blog content is parsed and loaded into memory. Whenever a new articl
# Configuration options
These options should be placed in glog_cofig.json and should be valid JSON
- blog_repository - The url of the blog repository (e.g. git@github.com:guyht/Guido.git)
- author - The default blog author (will be used if author is not specified in an individual post)
- blog_title - The title to display at top of the blog
- port - The port number to run the blog on
- disqus_id - (Optional) Disqus ID to use for comments
- analytics_code - (Optional) Google Analytics code
- base_url - (Optional) Base URL for the blog. Defaults to '/'
- cache_time - (Optional) Time in seconds to cache each page
+ blog_repository - The url of the blog repository (e.g. git@github.com:guyht/Guido.git)
+ author - The default blog author (will be used if author is not specified in an individual post)
+ blog_title - The title to display at top of the blog
+ port - The port number to run the blog on
+ disqus_id - (Optional) Disqus ID to use for comments
+ analytics_code - (Optional) Google Analytics code
+ base_url - (Optional) Base URL for the blog. Defaults to '/'
+ cache_time - (Optional) Time in seconds to cache each page
+ article_per_page - (Optional) Number of articles to display on a page. Defaults to 10
# Running the tests
View
13 glog_config.sample.json
@@ -1,8 +1,9 @@
{
- "blog_repository" : "git@github.com:guyht/Guido.git",
- "author" : "Glog",
- "blog_title" : "The Glob Blog",
- "port" : 8080,
- "base_url" : "",
- "cache_time" : 28800
+ "blog_repository" : "git@github.com:guyht/Guido.git",
+ "author" : "Glog",
+ "blog_title" : "The Glob Blog",
+ "port" : 8080,
+ "base_url" : "",
+ "cache_time" : 28800,
+ "articles_per_page" : 10
}
View
41 lib/glog.js
@@ -28,7 +28,8 @@ Glog.prototype.load_configs = function(cb) {
// Set defaults
var options = {
- blog_title : 'Glog Blog'
+ blog_title : 'Glog Blog',
+ articles_per_page : 10
},
fn = this,
key;
@@ -144,7 +145,7 @@ Glog.prototype.load_articles = function(options, cb) {
// If that was the last article then trigger the callback
if(files.length === articles.length) {
- cb(articles);
+ cb(articles.reverse());
}
});
@@ -166,14 +167,25 @@ Glog.prototype.render_blog = function(options, articles, cb) {
}
console.log('Compiling template for home page');
- var ja = jade.compile(data);
+ var ja = jade.compile(data),
+ per_page = options.articles_per_page,
+ i, len = Math.floor((articles.length / per_page) + 1),
+ start, end;
- fn.pages['/'] = ja({
- 'title' : options.blog_title,
- 'analytics_code' : options.analytics_code || null,
- 'base_url' : options.base_url || '',
- 'articles' : articles
- });
+ for(i = 0; i < len; i++) {
+ start = (per_page * i);
+ end = articles.length < (per_page * (i + 1)) - 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 || '',
+ 'articles' : articles.slice(start, end)
+ });
+
+ // pages[/] === pages[_page1]
+ fn.pages['/'] = fn.pages['_page1'];
+ }
console.log('Compiling template for individual pages');
@@ -201,11 +213,20 @@ Glog.prototype.render_blog = function(options, articles, cb) {
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)');
- res.end(this.pages['/']);
+
+ if(typeof this.pages['_page' + pagenum] !== 'undefined') {
+ res.end(this.pages['_page' + pagenum]);
+ } else {
+ res.end(this.pages['/']);
+ }
};
View
3 server.js
@@ -25,6 +25,9 @@ glog.rebuild(function() {
app.get('/', function(req, res, next) {
glog.req_home(req, res, next, options)
});
+ app.get('/page/:pagenum', function(req, res, next) {
+ glog.req_home(req, res, next, options)
+ });
app.get('/:year/:month/:article', function(req, res, next) {
glog.req_article(req, res, next, options)
});

0 comments on commit 2721f51

Please sign in to comment.
Something went wrong with that request. Please try again.