summaryrefslogblamecommitdiffstats
path: root/blog/_plugins/multi-paginate.rb
blob: 9978116243488a0dc9ccf73eb52ea93a550a6f75 (plain) (tree)













































                                                                               
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