Paginate articles.

This commit is contained in:
Jez Cope 2011-08-13 17:39:53 +01:00
parent 0d4e49156a
commit dcf872f74f
8 changed files with 78 additions and 22 deletions

23
Rules
View File

@ -1,14 +1,8 @@
#!/usr/bin/env ruby
# A few helpful tips about the Rules file:
#
# * The order of rules is important: for each item, only the first matching
# rule is applied.
#
# * Item identifiers start and end with a slash (e.g. “/about/” for the file
# “content/about.html”). To select all children, grandchildren, … of an
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.
preprocess do
paginate_articles
end
require 'compass'
@ -23,15 +17,24 @@ compile '/' do
layout 'home'
end
compile '/archives/*/' do
filter :haml
layout 'home'
end
compile '/articles/*' do
filter :kramdown
layout 'home'
layout 'article'
end
route '/assets/style/*' do
item.identifier.chop + '.css'
end
route '/articles/*' do
'/' + item.identifier.split('/')[-1] + '.html'
end
route '*' do
item.identifier + 'index.html'
end

View File

@ -1,6 +1,8 @@
author_name: Jez Cope
author_uri: http://erambler.co.uk/
page_size: 10
# A list of file extensions that nanoc will consider to be textual rather than
# binary. If an item with an extension not in this list is found, the file
# will be considered as binary.

View File

@ -1,8 +1,4 @@
---
title: Home
---
- sorted_articles.each do |article|
%article
%h2= link_to article[:title], article
.post-date= date_for(article).strftime '%A %e %B, %Y'
.post-content= article.compiled_content
= render 'partials/article_index', :page => 0

17
layouts/article.haml Normal file
View File

@ -0,0 +1,17 @@
!!!
%html{ :lang => 'en' }
%head= render 'partials/head'
%body.bp
#container
%header
.blog-title e-Rambler
.blog-subtitle Jez Cope's e-learning blog
%nav= render 'partials/nav'
%article
%h1.post-title= @item[:title]
.post-date= date_for(@item).strftime '%A %e %B, %Y'
.post-content= yield
#sidebar
Sidebar
%footer
Footer

View File

@ -9,8 +9,7 @@
<h2>Jez Cope's e-learning blog</h2>
%nav= render 'partials/nav'
#content
%article
= yield
= yield
#sidebar
Sidebar
%footer

View File

@ -0,0 +1,11 @@
- n = @config[:page_size]
- sorted_articles.drop(@page*n).take(n).each do |article|
%article
%h2= link_to article[:title], article
.post-date= date_for(article).strftime '%A %e %B, %Y'
.post-content= article.compiled_content
#page-nav
- unless @page == 0
= link_to '<< Newer posts', archive_page_url(@page - 1)
- unless (@page+1) * n >= articles.count
= link_to 'Older posts >>', archive_page_url(@page + 1)

33
lib/blogging.rb Normal file
View File

@ -0,0 +1,33 @@
require 'nokogiri'
include Nanoc3::Helpers::Blogging
def date_for(article)
attribute_to_time article[:created_at]
end
def excerpt_for(article, pars = 1)
article[:excerpt] || generate_excerpt(article, pars)
end
def generate_excerpt(article, pars = 1)
doc = Nokogiri::HTML::DocumentFragment.parse(article.compiled_content)
doc.css('p')[0,pars].to_html
end
def paginate_articles
1.upto(articles.count/@config[:page_size]) do |page|
@items << Nanoc3::Item.new(
"= render 'partials/article_index', :page => #{page}",
{ :title => "Archive page #{page}" },
"/archives/#{page}")
end
end
def archive_page_url(i)
if i == 0
'/'
else
"/archives/#{i}/"
end
end

View File

@ -3,8 +3,3 @@
include Nanoc3::Helpers::LinkTo
include Nanoc3::Helpers::Rendering
include Nanoc3::Helpers::Blogging
def date_for(article)
DateTime.parse article[:created_at]
end