From d51abbfe635b60ac97c698a62add330821c21645 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 4 Oct 2017 20:23:52 +0200 Subject: Add multi paginate plugin. Inspired by https://github.com/fadhilnapis/jekyll-multi-paginate, but completely rewritten for our needs. --- blog/_plugins/multi-paginate.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 blog/_plugins/multi-paginate.rb (limited to 'blog') diff --git a/blog/_plugins/multi-paginate.rb b/blog/_plugins/multi-paginate.rb new file mode 100644 index 0000000..9978116 --- /dev/null +++ b/blog/_plugins/multi-paginate.rb @@ -0,0 +1,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 -- cgit v1.2.3