Merge pull request #1016 from mojombo/fix-pagination

Paginate in subdirectories properly
This commit is contained in:
Parker Moore 2013-05-05 14:01:29 -07:00
commit 0b1d22dd61
5 changed files with 28 additions and 13 deletions

View File

@ -2,13 +2,13 @@ Feature: Site pagination
In order to paginate my blog
As a blog's user
I want divide the posts in several pages
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" page that contains "{{ paginator.posts.size }}"
And I have a _posts directory
And I have the following post:
And I have the following posts:
| title | date | layout | content |
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
@ -32,10 +32,10 @@ Feature: Site pagination
| paginate | 1 |
| paginate_path | /blog/page-:num |
| permalink | /blog/:year/:month/:day/:title |
And I have a _layouts directory
And I have an "index.html" page that contains "{{ paginator.posts.size }}"
And I have a blog directory
And I have an "blog/index.html" page that contains "{{ paginator.posts.size }}"
And I have a _posts directory
And I have the following post:
And I have the following posts:
| title | date | layout | content |
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |

View File

@ -5,7 +5,7 @@ Before do
end
After do
Dir.chdir(TEST_DIR)
Dir.chdir(File.expand_path("..", TEST_DIR))
FileUtils.rm_rf(TEST_DIR)
end

View File

@ -11,7 +11,7 @@ module Jekyll
# Returns nothing.
def generate(site)
site.pages.dup.each do |page|
paginate(site, page) if Pager.pagination_enabled?(site.config, page.name)
paginate(site, page) if Pager.pagination_enabled?(site.config, page)
end
end
@ -65,11 +65,23 @@ module Jekyll
# Determine if pagination is enabled for a given file.
#
# config - The configuration Hash.
# file - The String filename of the file.
# page - The Jekyll::Page with which to paginate
#
# Returns true if pagination is enabled, false otherwise.
def self.pagination_enabled?(config, file)
file == 'index.html' && !config['paginate'].nil?
def self.pagination_enabled?(config, page)
!config['paginate'].nil? &&
page.name == 'index.html' &&
subdirectories_identical(config['paginate_path'], page.dir)
end
# Determine if the subdirectories of the two paths are the same relative to source
#
# paginate_path - the paginate_path configuration setting
# page_dir - the directory of the Jekyll::Page
#
# Returns whether the subdirectories are the same relative to source
def self.subdirectories_identical(paginate_path, page_dir)
File.dirname(paginate_path).gsub(/\A\./, '') == page_dir.gsub(/\/\z/, '')
end
# Static: Return the pagination path of the page
@ -80,7 +92,7 @@ module Jekyll
# Returns the pagination path as a string
def self.paginate_path(site_config, num_page)
return nil if num_page.nil? || num_page <= 1
format = site_config['paginate_path']
format = File.basename(site_config['paginate_path'])
format.sub(':num', num_page.to_s)
end

View File

@ -6,6 +6,7 @@ end
require 'rubygems'
require 'test/unit'
require 'ostruct'
gem 'RedCloth', '>= 4.2.1'
require 'jekyll'

View File

@ -23,7 +23,8 @@ class TestPager < Test::Unit::TestCase
end
should "report that pagination is disabled" do
assert !Pager.pagination_enabled?(@config, 'index.html')
page = OpenStruct.new({ :name => 'index.html', :dir => '/' })
assert !Pager.pagination_enabled?(@config, page)
end
end
@ -45,7 +46,8 @@ class TestPager < Test::Unit::TestCase
end
should "report that pagination is enabled" do
assert Pager.pagination_enabled?(@config, 'index.html')
page = OpenStruct.new({ :name => 'index.html', :dir => '/' })
assert Pager.pagination_enabled?(@config, page)
end
context "with 4 posts" do