Class: Jekyll::Pager

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pager.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll/pager.rb', line 15

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.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def next_page
  @next_page
end

#pageObject (readonly)

Returns the value of attribute page.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def previous_page
  @previous_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



3
4
5
# File 'lib/jekyll/pager.rb', line 3

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object



5
6
7
8
9
# File 'lib/jekyll/pager.rb', line 5

def self.calculate_pages(all_posts, per_page)
  num_pages = all_posts.size / per_page.to_i
  num_pages.abs + 1 if all_posts.size % per_page.to_i != 0
  num_pages
end

.pagination_enabled?(config, file) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/jekyll/pager.rb', line 11

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

Instance Method Details

#to_hashObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/pager.rb', line 33

def to_hash
  {
    '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