diff --git a/features/pagination.feature b/features/pagination.feature index cc3cf4d6..9482f18f 100644 --- a/features/pagination.feature +++ b/features/pagination.feature @@ -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 "" 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. | diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index c30b9034..75330738 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -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 diff --git a/lib/jekyll/generators/pagination.rb b/lib/jekyll/generators/pagination.rb index 71e27fba..0cd934c5 100644 --- a/lib/jekyll/generators/pagination.rb +++ b/lib/jekyll/generators/pagination.rb @@ -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 diff --git a/test/helper.rb b/test/helper.rb index a20d236f..3ee3f2b6 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -6,6 +6,7 @@ end require 'rubygems' require 'test/unit' +require 'ostruct' gem 'RedCloth', '>= 4.2.1' require 'jekyll' diff --git a/test/test_pager.rb b/test/test_pager.rb index fe1156ca..7e153ea4 100644 --- a/test/test_pager.rb +++ b/test/test_pager.rb @@ -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