summaryrefslogtreecommitdiffstats
path: root/blog/_plugins/multi-paginate.rb
blob: 9978116243488a0dc9ccf73eb52ea93a550a6f75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Jekyll
  class Paginate < Page
    attr_accessor :pagination, :page_num
    
    def initialize(site, pagepath, pagination)
      super(site, site.source, File.dirname(pagepath), File.basename(pagepath))
      @pagination = pagination
      @page_num = @pagination['num']
      
      self.data['pagination']=pagination
      self.data.delete "paginate"
    end

    def url
      orig = super
      orig = File.join(orig, @page_num.to_s) if @page_num > 0
      File.join(orig, 'index.html')
    end
  end

  class MultiPaginateGenerator < Generator
    safe true

    def generate(site)
      # We're only interested in pages with `paginate`set
      paginate_pages = site.pages.reject { |page| page.data['paginate'].nil? }
      paginate_pages.each do |page|
        posts_per_page = page.data['paginate'].to_i
        lang = page.data['lang']
        posts_in_lang = site.posts.docs
                          .reject { |post| post.data['lang'] != lang }
                          .reverse
        
        num_pages = (posts_in_lang.length.to_f / posts_per_page.to_f).ceil
        num_pages.times do |i|
          page_posts = posts_in_lang.slice(i * posts_per_page, posts_per_page)
          pagination = {
            'posts' => page_posts,
            'num' => i,
          }
          site.pages << Paginate.new(site, page.path, pagination)
        end
      end
    end
  end
end