Class: Jekyll::PTPager

Inherits:
Paginate::Pager
  • Object
show all
Defined in:
lib/jekyll-pagination-task/pagination_task.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, pager, current, all_posts, per_page) ⇒ PTPager

Initialize a new PTPager.

site - the Jekyll::Site object pager - the pager template page current - 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.


211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/jekyll-pagination-task/pagination_task.rb', line 211

def initialize(site, pager, current, all_posts, per_page)
  super(site, current, all_posts, Paginate::Pager.calculate_pages(all_posts, per_page))

  @per_page = per_page
  init = (@page - 1) * @per_page
  offset = (init + @per_page - 1) >= all_posts.size ? all_posts.size : (init + @per_page - 1)

  @posts = all_posts[init..offset]

  @previous_page_path = PTPager.paginate_path(site, pager, @previous_page)
  @next_page_path = PTPager.paginate_path(site, pager, @next_page)
end

Class Method Details

.paginate_path(site, pager, page_num) ⇒ Object

Get the path for the generated pagers

site - the Jekyll::Site object pager - the template for the pager



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/jekyll-pagination-task/pagination_task.rb', line 189

def self.paginate_path(site, pager, page_num)
  return nil if page_num.nil?
  format = pager['pagination_format']
  format = format || site.config['pagination_task']['format']
  format = format || '/:dir/:name/:Num'
  dir = remove_leading_slash(pager.dup.dir)
  name = File.basename(pager.name, File.extname(pager.name))
  format = format.sub(':dir', dir)
  format = format.sub(':name', name)
  format = format.sub(':num', page_num.to_s)
  format = page_num > 1 ? format.sub(':Num', page_num.to_s) : format.sub(':Num', '')
  ensure_leading_slash(format)
end