fix pagination to adhere to read/render/write paradigm

This commit is contained in:
Tom Preston-Werner 2010-01-12 14:43:28 -08:00
parent a076ce0702
commit b68149c7bc
5 changed files with 39 additions and 24 deletions

View File

@ -4,6 +4,7 @@
* Bug Fixes
* Categories isn't always an array (#73)
* Empty tags causes error in read_posts (#84)
* Fix pagination to adhere to read/render/write paradigm
* Test Enhancement
* cucumber features no longer use site.ports.first where a better alternative is available

View File

@ -14,7 +14,7 @@ Feature: Markdown
Then the _site directory should exist
And I should see "Index" in "_site/index.html"
And I should see "<h1 id='my_title'>My Title</h1>" in "_site/2009/03/27/hackers.html"
And I should see "<h1>My Title</h1>" in "_site/index.html"
And I should see "<h1 id='my_title'>My Title</h1>" in "_site/index.html"
Scenario: Markdown in pagination on index
Given I have a configuration file with "paginate" set to "5"
@ -26,5 +26,5 @@ Feature: Markdown
When I run jekyll
Then the _site directory should exist
And I should see "Index" in "_site/index.html"
And I should see "<h1>My Title</h1>" in "_site/index.html"
And I should see "<h1 id='my_title'>My Title</h1>" in "_site/index.html"

View File

@ -6,7 +6,7 @@ Feature: Site pagination
Scenario Outline: Paginate with N posts per page
Given I have a configuration file with "paginate" set to "<num>"
And I have a _layouts directory
And I have an "index.html" file that contains "{{ paginator.posts.size }}"
And I have an "index.html" page that contains "{{ paginator.posts.size }}"
And I have a _posts directory
And I have the following post:
| title | date | layout | content |

View File

@ -4,7 +4,7 @@ module Jekyll
include Convertible
attr_accessor :site
attr_accessor :name, :ext, :basename
attr_accessor :name, :ext, :basename, :dir
attr_accessor :data, :content, :output
# Initialize a new Page.
@ -101,6 +101,10 @@ module Jekyll
end
end
def inspect
"#<Jekyll:Page @name=#{self.name.inspect}>"
end
private
def index?

View File

@ -147,8 +147,16 @@ module Jekyll
end
def render
[self.posts, self.pages].flatten.each do |convertible|
convertible.render(self.layouts, site_payload)
self.posts.each do |post|
post.render(self.layouts, site_payload)
end
self.pages.dup.each do |page|
if Pager.pagination_enabled?(self.config, page.name)
paginate(page)
else
page.render(self.layouts, site_payload)
end
end
self.categories.values.map { |ps| ps.sort! { |a, b| b <=> a} }
@ -191,9 +199,6 @@ module Jekyll
next if self.dest.sub(/\/$/, '') == f_abs
read_directories(f_rel)
elsif !File.symlink?(f_abs)
if Pager.pagination_enabled?(self.config, f)
paginate_posts(f, dir)
else
first3 = File.open(f_abs) { |fd| fd.read(3) }
if first3 == "---"
# file appears to have a YAML header so process it as a page
@ -205,7 +210,6 @@ module Jekyll
end
end
end
end
# Constructs a hash map of Posts indexed by the specified Post attribute
#
@ -244,8 +248,10 @@ module Jekyll
end
end
# Paginates the blog's posts. Renders the index.html file into paginated directories, ie: page2, page3...
# and adds more site-wide data
# Paginates the blog's posts. Renders the index.html file into paginated
# directories, ie: page2/index.html, page3/index.html, etc and adds more
# site-wide data.
# +page+ is the index.html Page that requires pagination
#
# {"paginator" => { "page" => <Number>,
# "per_page" => <Number>,
@ -254,15 +260,19 @@ module Jekyll
# "total_pages" => <Number>,
# "previous_page" => <Number>,
# "next_page" => <Number> }}
def paginate_posts(file, dir)
all_posts = self.posts.sort { |a,b| b <=> a }
def paginate(page)
all_posts = site_payload['site']['posts']
pages = Pager.calculate_pages(all_posts, self.config['paginate'].to_i)
(1..pages).each do |num_page|
pager = Pager.new(self.config, num_page, all_posts, pages)
page = Page.new(self, self.source, dir, file)
if num_page > 1
newpage = Page.new(self, self.source, page.dir, page.name)
newpage.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash}))
newpage.dir = File.join(page.dir, "page#{num_page}")
self.pages << newpage
else
page.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash}))
suffix = "page#{num_page}" if num_page > 1
page.write(self.dest, suffix)
end
end
end
end