diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2017-10-04 20:23:52 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2017-10-04 20:23:52 +0200 |
commit | d51abbfe635b60ac97c698a62add330821c21645 (patch) | |
tree | f3564d75e9ad683daeb5b08d85c699f0b3f7426e /blog | |
parent | 18608c867a6cbe7880ea0273c463d4be93f2cc9f (diff) | |
download | norsk-urskog-main-d51abbfe635b60ac97c698a62add330821c21645.tar.gz norsk-urskog-main-d51abbfe635b60ac97c698a62add330821c21645.tar.bz2 norsk-urskog-main-d51abbfe635b60ac97c698a62add330821c21645.zip |
Add multi paginate plugin.
Inspired by https://github.com/fadhilnapis/jekyll-multi-paginate,
but completely rewritten for our needs.
Diffstat (limited to 'blog')
-rw-r--r-- | blog/_plugins/multi-paginate.rb | 46 |
1 files changed, 46 insertions, 0 deletions
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 |