Merge pull request #1016 from mojombo/fix-pagination
Paginate in subdirectories properly
This commit is contained in:
commit
0b1d22dd61
|
@ -2,13 +2,13 @@ Feature: Site pagination
|
||||||
In order to paginate my blog
|
In order to paginate my blog
|
||||||
As a blog's user
|
As a blog's user
|
||||||
I want divide the posts in several pages
|
I want divide the posts in several pages
|
||||||
|
|
||||||
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" page 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 posts:
|
||||||
| title | date | layout | content |
|
| title | date | layout | content |
|
||||||
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
|
| 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. |
|
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
|
||||||
|
@ -32,10 +32,10 @@ Feature: Site pagination
|
||||||
| paginate | 1 |
|
| paginate | 1 |
|
||||||
| paginate_path | /blog/page-:num |
|
| paginate_path | /blog/page-:num |
|
||||||
| permalink | /blog/:year/:month/:day/:title |
|
| permalink | /blog/:year/:month/:day/:title |
|
||||||
And I have a _layouts directory
|
And I have a blog directory
|
||||||
And I have an "index.html" page that contains "{{ paginator.posts.size }}"
|
And I have an "blog/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 posts:
|
||||||
| title | date | layout | content |
|
| title | date | layout | content |
|
||||||
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
|
| 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. |
|
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
|
||||||
|
|
|
@ -5,7 +5,7 @@ Before do
|
||||||
end
|
end
|
||||||
|
|
||||||
After do
|
After do
|
||||||
Dir.chdir(TEST_DIR)
|
Dir.chdir(File.expand_path("..", TEST_DIR))
|
||||||
FileUtils.rm_rf(TEST_DIR)
|
FileUtils.rm_rf(TEST_DIR)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Jekyll
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
def generate(site)
|
def generate(site)
|
||||||
site.pages.dup.each do |page|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,11 +65,23 @@ module Jekyll
|
||||||
# Determine if pagination is enabled for a given file.
|
# Determine if pagination is enabled for a given file.
|
||||||
#
|
#
|
||||||
# config - The configuration Hash.
|
# 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.
|
# Returns true if pagination is enabled, false otherwise.
|
||||||
def self.pagination_enabled?(config, file)
|
def self.pagination_enabled?(config, page)
|
||||||
file == 'index.html' && !config['paginate'].nil?
|
!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
|
end
|
||||||
|
|
||||||
# Static: Return the pagination path of the page
|
# Static: Return the pagination path of the page
|
||||||
|
@ -80,7 +92,7 @@ module Jekyll
|
||||||
# Returns the pagination path as a string
|
# Returns the pagination path as a string
|
||||||
def self.paginate_path(site_config, num_page)
|
def self.paginate_path(site_config, num_page)
|
||||||
return nil if num_page.nil? || num_page <= 1
|
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)
|
format.sub(':num', num_page.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ end
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
require 'ostruct'
|
||||||
gem 'RedCloth', '>= 4.2.1'
|
gem 'RedCloth', '>= 4.2.1'
|
||||||
|
|
||||||
require 'jekyll'
|
require 'jekyll'
|
||||||
|
|
|
@ -23,7 +23,8 @@ class TestPager < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "report that pagination is disabled" do
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -45,7 +46,8 @@ class TestPager < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "report that pagination is enabled" do
|
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
|
end
|
||||||
|
|
||||||
context "with 4 posts" do
|
context "with 4 posts" do
|
||||||
|
|
Loading…
Reference in New Issue