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

Returns a new instance of Pager.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll/generators/pagination.rb', line 55

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.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def next_page
  @next_page
end

#pageObject (readonly)

Returns the value of attribute page.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def previous_page
  @previous_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



43
44
45
# File 'lib/jekyll/generators/pagination.rb', line 43

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object



45
46
47
48
49
# File 'lib/jekyll/generators/pagination.rb', line 45

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

.pagination_enabled?(config, file) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/jekyll/generators/pagination.rb', line 51

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

Instance Method Details

#to_liquidObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll/generators/pagination.rb', line 73

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