Class: Jekyll::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/generators/pagination.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, page, all_posts, num_pages = nil) ⇒ Pager

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.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jekyll/generators/pagination.rb', line 85

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

Instance Attribute Details

#next_pageObject (readonly)

Returns the value of attribute next_page.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def next_page
  @next_page
end

#pageObject (readonly)

Returns the value of attribute page.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def previous_page
  @previous_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



56
57
58
# File 'lib/jekyll/generators/pagination.rb', line 56

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object

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.



64
65
66
# File 'lib/jekyll/generators/pagination.rb', line 64

def self.calculate_pages(all_posts, per_page)
  (all_posts.size.to_f / per_page.to_i).ceil
end

.pagination_enabled?(config, file) ⇒ Boolean

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.

Returns:

  • (Boolean)


74
75
76
# File 'lib/jekyll/generators/pagination.rb', line 74

def self.pagination_enabled?(config, file)
  file == 'index.html' && !config['paginate'].nil?
end

Instance Method Details

#to_liquidObject

Convert this Pager’s data to a Hash suitable for use by Liquid.

Returns the Hash representation of this Pager.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll/generators/pagination.rb', line 106

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