From b68149c7bc861cb758c6b0f8b211261705cc2452 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Tue, 12 Jan 2010 14:43:28 -0800 Subject: [PATCH] fix pagination to adhere to read/render/write paradigm --- History.txt | 1 + features/markdown.feature | 4 +-- features/pagination.feature | 2 +- lib/jekyll/page.rb | 6 ++++- lib/jekyll/site.rb | 50 ++++++++++++++++++++++--------------- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/History.txt b/History.txt index 50a53414..d776a639 100644 --- a/History.txt +++ b/History.txt @@ -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 diff --git a/features/markdown.feature b/features/markdown.feature index ddb472f6..859e6f7e 100644 --- a/features/markdown.feature +++ b/features/markdown.feature @@ -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 "

My Title

" in "_site/2009/03/27/hackers.html" - And I should see "

My Title

" in "_site/index.html" + And I should see "

My Title

" 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 "

My Title

" in "_site/index.html" + And I should see "

My Title

" in "_site/index.html" \ No newline at end of file diff --git a/features/pagination.feature b/features/pagination.feature index 2b1b3467..90b10578 100644 --- a/features/pagination.feature +++ b/features/pagination.feature @@ -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 "" 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 | diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index 20463903..62a757dc 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -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 + "#" + end + private def index? diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 3d47d16e..2a7294f8 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -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,17 +199,13 @@ 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) + first3 = File.open(f_abs) { |fd| fd.read(3) } + if first3 == "---" + # file appears to have a YAML header so process it as a page + pages << Page.new(self, self.source, dir, f) 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 - 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 + # otherwise treat it as a static file + static_files << StaticFile.new(self, self.source, dir, f) end end end @@ -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" => , # "per_page" => , @@ -254,15 +260,19 @@ module Jekyll # "total_pages" => , # "previous_page" => , # "next_page" => }} - 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) - page.render(self.layouts, site_payload.merge({'paginator' => pager.to_hash})) - suffix = "page#{num_page}" if num_page > 1 - page.write(self.dest, suffix) + 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})) + end end end end