Class: Jekyll::Pager
- Inherits:
-
Object
- Object
- Jekyll::Pager
- Defined in:
- lib/jekyll/pager.rb
Instance Attribute Summary collapse
-
#next_page ⇒ Object
readonly
Returns the value of attribute next_page.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#previous_page ⇒ Object
readonly
Returns the value of attribute previous_page.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
-
#total_posts ⇒ Object
readonly
Returns the value of attribute total_posts.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(config, page, all_posts, num_pages = nil) ⇒ Pager
constructor
A new instance of Pager.
- #to_hash ⇒ Object
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_page ⇒ Object (readonly)
Returns the value of attribute next_page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def next_page @next_page end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def per_page @per_page end |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
3 4 5 |
# File 'lib/jekyll/pager.rb', line 3 def posts @posts end |
#previous_page ⇒ Object (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_pages ⇒ Object (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_posts ⇒ Object (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
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_hash ⇒ Object
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 |