From 9e4ef3cbff050f214856bdbc1af96593e34c161f Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 26 May 2014 20:09:33 -0400 Subject: [PATCH] Add the paginator as a plugin: jekyll-paginate --- jekyll.gemspec | 1 + lib/jekyll.rb | 1 + lib/jekyll/generators/pagination.rb | 217 ---------------------------- test/test_pager.rb | 118 --------------- 4 files changed, 2 insertions(+), 335 deletions(-) delete mode 100644 lib/jekyll/generators/pagination.rb delete mode 100644 test/test_pager.rb diff --git a/jekyll.gemspec b/jekyll.gemspec index 7192910379c..081ea8de499 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -38,6 +38,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('colorator', "~> 0.1") s.add_runtime_dependency('redcarpet', "~> 3.1") s.add_runtime_dependency('toml', '~> 0.1.0') + s.add_runtime_dependency('jekyll-paginate', '~> 1.0.0.rc3') s.add_runtime_dependency('jekyll-coffeescript', '~> 1.0') s.add_runtime_dependency('jekyll-sass-converter', '~> 1.0') diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 26c6e569954..6daf04014c0 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -72,6 +72,7 @@ def require_all(path) # plugins require 'jekyll-coffeescript' require 'jekyll-sass-converter' +require 'jekyll-paginate' SafeYAML::OPTIONS[:suppress_warnings] = true diff --git a/lib/jekyll/generators/pagination.rb b/lib/jekyll/generators/pagination.rb deleted file mode 100644 index 72dc5292d56..00000000000 --- a/lib/jekyll/generators/pagination.rb +++ /dev/null @@ -1,217 +0,0 @@ -module Jekyll - module Generators - class Pagination < Generator - # This generator is safe from arbitrary code execution. - safe true - - # This generator should be passive with regard to its execution - priority :lowest - - # Generate paginated pages if necessary. - # - # site - The Site. - # - # Returns nothing. - def generate(site) - if Pager.pagination_enabled?(site) - if template = template_page(site) - paginate(site, template) - else - Jekyll.logger.warn "Pagination:", "Pagination is enabled, but I couldn't find " + - "an index.html page to use as the pagination template. Skipping pagination." - end - end - end - - # Paginates the blog's posts. Renders the index.html file into paginated - # directories, e.g.: page2/index.html, page3/index.html, etc and adds more - # site-wide data. - # - # site - The Site. - # page - The index.html Page that requires pagination. - # - # {"paginator" => { "page" => , - # "per_page" => , - # "posts" => [], - # "total_posts" => , - # "total_pages" => , - # "previous_page" => , - # "next_page" => }} - def paginate(site, page) - all_posts = site.site_payload['site']['posts'] - pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i) - (1..pages).each do |num_page| - pager = Pager.new(site, num_page, all_posts, pages) - if num_page > 1 - newpage = Page.new(site, site.source, page.dir, page.name) - newpage.pager = pager - newpage.dir = Pager.paginate_path(site, num_page) - site.pages << newpage - else - page.pager = pager - end - end - end - - # Static: Fetch the URL of the template page. Used to determine the - # path to the first pager in the series. - # - # site - the Jekyll::Site object - # - # Returns the url of the template page - def self.first_page_url(site) - if page = Pagination.new.template_page(site) - page.url - else - nil - end - end - - # Public: Find the Jekyll::Page which will act as the pager template - # - # site - the Jekyll::Site object - # - # Returns the Jekyll::Page which will act as the pager template - def template_page(site) - site.pages.dup.select do |page| - Pager.pagination_candidate?(site.config, page) - end.sort do |one, two| - two.path.size <=> one.path.size - end.first - end - end - end - - class Pager - attr_reader :page, :per_page, :posts, :total_posts, :total_pages, - :previous_page, :previous_page_path, :next_page, :next_page_path - - # Calculate the number of pages. - # - # all_posts - The Array of all Posts. - # per_page - The Integer of entries per page. - # - # Returns the Integer number of pages. - def self.calculate_pages(all_posts, per_page) - (all_posts.size.to_f / per_page.to_i).ceil - end - - # Determine if pagination is enabled the site. - # - # site - the Jekyll::Site object - # - # Returns true if pagination is enabled, false otherwise. - def self.pagination_enabled?(site) - !site.config['paginate'].nil? && - site.pages.size > 0 - end - - # Static: Determine if a page is a possible candidate to be a template page. - # Page's name must be `index.html` and exist in any of the directories - # between the site source and `paginate_path`. - # - # config - the site configuration hash - # page - the Jekyll::Page about which we're inquiring - # - # Returns true if the - def self.pagination_candidate?(config, page) - page_dir = File.dirname(File.expand_path(remove_leading_slash(page.path), config['source'])) - paginate_path = remove_leading_slash(config['paginate_path']) - paginate_path = File.expand_path(paginate_path, config['source']) - page.name == 'index.html' && - in_hierarchy(config['source'], page_dir, File.dirname(paginate_path)) - end - - # Determine if the subdirectories of the two paths are the same relative to source - # - # source - the site source - # page_dir - the directory of the Jekyll::Page - # paginate_path - the absolute paginate path (from root of FS) - # - # Returns whether the subdirectories are the same relative to source - def self.in_hierarchy(source, page_dir, paginate_path) - return false if paginate_path == File.dirname(paginate_path) - return false if paginate_path == Pathname.new(source).parent - page_dir == paginate_path || - in_hierarchy(source, page_dir, File.dirname(paginate_path)) - end - - # Static: Return the pagination path of the page - # - # site - the Jekyll::Site object - # num_page - the pagination page number - # - # Returns the pagination path as a string - def self.paginate_path(site, num_page) - return nil if num_page.nil? - return Generators::Pagination.first_page_url(site) if num_page <= 1 - format = site.config['paginate_path'] - format = format.sub(':num', num_page.to_s) - ensure_leading_slash(format) - end - - # Static: Return a String version of the input which has a leading slash. - # If the input already has a forward slash in position zero, it will be - # returned unchanged. - # - # path - a String path - # - # Returns the path with a leading slash - def self.ensure_leading_slash(path) - path[0..0] == "/" ? path : "/#{path}" - end - - # Static: Return a String version of the input without a leading slash. - # - # path - a String path - # - # Returns the input without the leading slash - def self.remove_leading_slash(path) - ensure_leading_slash(path)[1..-1] - end - - # Initialize a new Pager. - # - # site - the Jekyll::Site object - # page - The Integer page number. - # all_posts - The Array of all the site's Posts. - # num_pages - The Integer number of pages or nil if you'd like the number - # of pages calculated. - def initialize(site, page, all_posts, num_pages = nil) - @page = page - @per_page = site.config['paginate'].to_i - @total_pages = num_pages || Pager.calculate_pages(all_posts, @per_page) - - if @page > @total_pages - raise RuntimeError, "page number can't be greater than total pages: #{@page} > #{@total_pages}" - end - - init = (@page - 1) * @per_page - offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1) - - @total_posts = all_posts.size - @posts = all_posts[init..offset] - @previous_page = @page != 1 ? @page - 1 : nil - @previous_page_path = Pager.paginate_path(site, @previous_page) - @next_page = @page != @total_pages ? @page + 1 : nil - @next_page_path = Pager.paginate_path(site, @next_page) - end - - # Convert this Pager's data to a Hash suitable for use by Liquid. - # - # Returns the Hash representation of this Pager. - def to_liquid - { - 'page' => page, - 'per_page' => per_page, - 'posts' => posts, - 'total_posts' => total_posts, - 'total_pages' => total_pages, - 'previous_page' => previous_page, - 'previous_page_path' => previous_page_path, - 'next_page' => next_page, - 'next_page_path' => next_page_path - } - end - end -end diff --git a/test/test_pager.rb b/test/test_pager.rb deleted file mode 100644 index 976925f08ad..00000000000 --- a/test/test_pager.rb +++ /dev/null @@ -1,118 +0,0 @@ -require 'helper' - -class TestPager < Test::Unit::TestCase - - def build_site(config = {}) - base = build_configs({ - 'source' => source_dir, - 'destination' => dest_dir, - 'paginate' => 1 - }) - site = Jekyll::Site.new(site_configuration( - {"paginate" => 1}.merge(config) - )) - site.process - site - end - - should "calculate number of pages" do - assert_equal(0, Pager.calculate_pages([], '2')) - assert_equal(1, Pager.calculate_pages([1], '2')) - assert_equal(1, Pager.calculate_pages([1,2], '2')) - assert_equal(2, Pager.calculate_pages([1,2,3], '2')) - assert_equal(2, Pager.calculate_pages([1,2,3,4], '2')) - assert_equal(3, Pager.calculate_pages([1,2,3,4,5], '2')) - end - - should "determine the pagination path" do - assert_equal("/index.html", Pager.paginate_path(build_site, 1)) - assert_equal("/page2", Pager.paginate_path(build_site, 2)) - assert_equal("/index.html", Pager.paginate_path(build_site({'paginate_path' => '/blog/page-:num'}), 1)) - assert_equal("/blog/page-2", Pager.paginate_path(build_site({'paginate_path' => '/blog/page-:num'}), 2)) - assert_equal("/index.html", Pager.paginate_path(build_site({'paginate_path' => '/blog/page/:num'}), 1)) - assert_equal("/blog/page/2", Pager.paginate_path(build_site({'paginate_path' => '/blog/page/:num'}), 2)) - assert_equal("/contacts/index.html", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page:num'}), 1)) - assert_equal("/contacts/page2", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page:num'}), 2)) - assert_equal("/contacts/index.html", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page/:num'}), 1)) - assert_equal("/contacts/page/2", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page/:num'}), 2)) - end - - context "pagination disabled" do - should "report that pagination is disabled" do - assert !Pager.pagination_enabled?(build_site('paginate' => nil)) - end - end - - context "pagination enabled for 2" do - setup do - @site = build_site('paginate' => 2) - @posts = @site.posts - end - - should "report that pagination is enabled" do - assert Pager.pagination_enabled?(@site) - end - - context "with 4 posts" do - setup do - @posts = @site.posts[1..4] # limit to 4 - end - - should "create first pager" do - pager = Pager.new(@site, 1, @posts) - assert_equal(2, pager.posts.size) - assert_equal(2, pager.total_pages) - assert_nil(pager.previous_page) - assert_equal(2, pager.next_page) - end - - should "create second pager" do - pager = Pager.new(@site, 2, @posts) - assert_equal(2, pager.posts.size) - assert_equal(2, pager.total_pages) - assert_equal(1, pager.previous_page) - assert_nil(pager.next_page) - end - - should "not create third pager" do - assert_raise(RuntimeError) { Pager.new(@site, 3, @posts) } - end - - end - - context "with 5 posts" do - setup do - @posts = @site.posts[1..5] # limit to 5 - end - - should "create first pager" do - pager = Pager.new(@site, 1, @posts) - assert_equal(2, pager.posts.size) - assert_equal(3, pager.total_pages) - assert_nil(pager.previous_page) - assert_equal(2, pager.next_page) - end - - should "create second pager" do - pager = Pager.new(@site, 2, @posts) - assert_equal(2, pager.posts.size) - assert_equal(3, pager.total_pages) - assert_equal(1, pager.previous_page) - assert_equal(3, pager.next_page) - end - - should "create third pager" do - pager = Pager.new(@site, 3, @posts) - assert_equal(1, pager.posts.size) - assert_equal(3, pager.total_pages) - assert_equal(2, pager.previous_page) - assert_nil(pager.next_page) - end - - should "not create fourth pager" do - assert_raise(RuntimeError) { Pager.new(@site, 4, @posts) } - end - - end - end -end