Class: Jekyll::Pager
- Inherits:
-
Object
- Object
- Jekyll::Pager
- Defined in:
- lib/jekyll/generators/pagination.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_liquid ⇒ Object
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_page ⇒ Object (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 |
#page ⇒ Object (readonly)
Returns the value of attribute page.
43 44 45 |
# File 'lib/jekyll/generators/pagination.rb', line 43 def page @page end |
#per_page ⇒ Object (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 |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
43 44 45 |
# File 'lib/jekyll/generators/pagination.rb', line 43 def posts @posts end |
#previous_page ⇒ Object (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_pages ⇒ Object (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_posts ⇒ Object (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
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_liquid ⇒ Object
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 |