Class: Jekyll::CategoryPager

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

Overview

Handle pagination of category index pages.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#next_pageObject (readonly)

Returns the value of attribute next_page.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def next_page
  @next_page
end

#next_page_pathObject (readonly)

Returns the value of attribute next_page_path.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def next_page_path
  @next_page_path
end

#pageObject (readonly)

Returns the value of attribute page.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def per_page
  @per_page
end

#postsObject (readonly)

Returns the value of attribute posts.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def posts
  @posts
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def previous_page
  @previous_page
end

#previous_page_pathObject (readonly)

Returns the value of attribute previous_page_path.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def previous_page_path
  @previous_page_path
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def total_pages
  @total_pages
end

#total_postsObject (readonly)

Returns the value of attribute total_posts.



233
234
235
# File 'lib/jekyll/category_pages.rb', line 233

def total_posts
  @total_posts
end

Class Method Details

.calculate_pages(all_posts, per_page) ⇒ Object

Static: 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.



242
243
244
# File 'lib/jekyll/category_pages.rb', line 242

def self.calculate_pages(all_posts, per_page)
  (all_posts.size.to_f / per_page.to_i).ceil
end

Instance Method Details

#add_posts(page, per_page, posts_in_category) ⇒ Object

Add page-specific post data.

page - Current page number. per_page - Posts per page. posts_in_category - Array with full list of Posts in the current category.



270
271
272
273
274
275
276
277
# File 'lib/jekyll/category_pages.rb', line 270

def add_posts(page, per_page, posts_in_category)
  total_posts = posts_in_category.size
  init = (page - 1) * per_page
  offset = (init + per_page - 1) >= total_posts ? total_posts : (init + per_page - 1)

  @total_posts = total_posts
  @posts = posts_in_category[init..offset]
end

#add_relations(page, per_page, total_pages, previous_page, next_page, previous_page_path, next_page_path) ⇒ Object

Add numeric relationships of this page to other pages.

page - Current page number. per_page - Posts per page. total_pages - Total number of pages. previous_page - Number of previous page or nil. next_page - Number of next page or nil. previous_page_path - String with path to previous page or nil. next_page_path - String with path to next page or nil.



255
256
257
258
259
260
261
262
263
# File 'lib/jekyll/category_pages.rb', line 255

def add_relations(page, per_page, total_pages, previous_page, next_page, previous_page_path, next_page_path)
  @page = page
  @per_page = per_page
  @total_pages = total_pages
  @previous_page = previous_page
  @next_page = next_page
  @previous_page_path = previous_page_path
  @next_page_path = next_page_path
end

#to_liquidObject

Convert this CategoryPager’s data to a Hash suitable for use by Liquid.

Returns the Hash representation of this CategoryPager.



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/jekyll/category_pages.rb', line 282

def to_liquid
  {
      'page' => page,
      'per_page' => per_page,
      'posts' => posts,
      'total_posts' => total_posts,
      'total_pages' => total_pages,
      'previous_page' => previous_page,
      'previous_page_path' => previous_page_path,
      'next_page' => next_page,
      'next_page_path' => next_page_path
  }
end