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.
# File lib/jekyll/generators/pagination.rb, line 58 def self.calculate_pages(all_posts, per_page) (all_posts.size.to_f / per_page.to_i).ceil end
Initialize a new Pager.
config - The Hash configuration of the site. 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.
# File lib/jekyll/generators/pagination.rb, line 79 def initialize(config, page, all_posts, num_pages = nil) @page = page @per_page = 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 @next_page = @page != @total_pages ? @page + 1 : nil end
Determine if pagination is enabled for a given file.
config - The configuration Hash. file - The String filename of the file.
Returns true if pagination is enabled, false otherwise.
# File lib/jekyll/generators/pagination.rb, line 68 def self.pagination_enabled?(config, file) file == 'index.html' && !config['paginate'].nil? end
Convert this Pager's data to a Hash suitable for use by Liquid.
Returns the Hash representation of this Pager.
# File lib/jekyll/generators/pagination.rb, line 100 def to_liquid { 'page' => page, 'per_page' => per_page, 'posts' => posts, 'total_posts' => total_posts, 'total_pages' => total_pages, 'previous_page' => previous_page, 'next_page' => next_page } end
Generated with the Darkfish Rdoc Generator 2.