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.


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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

Instance Attribute Details

#next_pageObject (readonly)

Returns the value of attribute next_page.



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

def next_page
  @next_page
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



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

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



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

def previous_page
  @previous_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



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

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



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

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.



58
59
60
# 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

.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)


68
69
70
# File 'lib/jekyll/generators/pagination.rb', line 68

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.



100
101
102
103
104
105
106
107
108
109
110
# 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