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 * Bug Fixes
* Categories isn't always an array (#73) * Categories isn't always an array (#73)
* Empty tags causes error in read_posts (#84) * Empty tags causes error in read_posts (#84)
* Fix pagination to adhere to read/render/write paradigm
* Test Enhancement * Test Enhancement
* cucumber features no longer use site.ports.first where a better alternative is available * 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 Then the _site directory should exist
And I should see "Index" in "_site/index.html" 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 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 Scenario: Markdown in pagination on index
Given I have a configuration file with "paginate" set to "5" Given I have a configuration file with "paginate" set to "5"
@ -26,5 +26,5 @@ Feature: Markdown
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
And I should see "Index" in "_site/index.html" 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 Scenario Outline: Paginate with N posts per page
Given I have a configuration file with "paginate" set to "<num>" Given I have a configuration file with "paginate" set to "<num>"
And I have a _layouts directory 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 a _posts directory
And I have the following post: And I have the following post:
| title | date | layout | content | | title | date | layout | content |

View File

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

View File

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