Sha256: 01cfe1322cc9cb6e9fa0a51b8c553f9cd963386164031c110acc509db2e86094

Contents?: true

Size: 2 KB

Versions: 13

Compression:

Stored size: 2 KB

Contents

module Alula
  class Paginate < Generator
    def generate
      # Loop all languages and count posts per language
      @languages = fetch_languages
      # @languages = {}
      # self.site.content.posts.each do |post|
      #   post.languages.each do |lang|
      #     @languages[lang] ||= []
      #     @languages[lang] << post
      #   end
      # end
      
      # Maximum amount of posts
      num_posts = self.site.content.posts.count
      # Maximum number of required pages
      pages = (num_posts / self.options.items).ceil
      
      (0..pages).each do |page|
        languages = @languages.select { |lang, posts| posts.count > page * self.options.items }
        posts = Hash[
          languages.collect do |lang, posts|
            [ lang, posts.slice(page * self.options.items, self.options.items) ]
          end
        ]
        titles = Hash[languages.collect {|lang, x| [lang, I18n.t("paginate.title", locale: lang, page: (page + 1))]}]
        descriptions = Hash[languages.collect {|lang, x| [lang, I18n.t("paginate.description", locale: lang, page: (page + 1))]}]

        self.site.content.pages << Alula::Content::Page.new({
          generator: self,
          posts: posts,
          pagenum: (page + 1),
          pages: (pages + 1),
          title: titles,
          description: descriptions,
          name: "page-#{(page + 1)}",
          slug: "page-#{(page + 1)}",
          sidebar: false,
          template: self.options.template,
          site: self.site,
          view: self.options.view || "paginate",
        },
        :navigation => ->(hook, locale = nil) {
          locale ||= self.current_locale || self.site.config.locale
          @navigation[locale] ||= self.site.content.pages.select { |item| item.metadata.generator == self.generator and item.languages.include?(locale) }
        }
        )
      end
    end
    
    def substitutes(locale, item)
      {
        "page" => item.metadata.pagenum.to_s,
      }
    end
  end
end

Alula::Generator.register :paginate, Alula::Paginate

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
alula-0.4.27 lib/alula/generators/paginate.rb
alula-0.4.26 lib/alula/generators/paginate.rb
alula-0.4.25 lib/alula/generators/paginate.rb
alula-0.4.24 lib/alula/generators/paginate.rb
alula-0.4.23 lib/alula/generators/paginate.rb
alula-0.4.22 lib/alula/generators/paginate.rb
alula-0.4.21 lib/alula/generators/paginate.rb
alula-0.4.20 lib/alula/generators/paginate.rb
alula-0.4.19 lib/alula/generators/paginate.rb
alula-0.4.18 lib/alula/generators/paginate.rb
alula-0.4.17 lib/alula/generators/paginate.rb
alula-0.4.16 lib/alula/generators/paginate.rb
alula-0.4.15 lib/alula/generators/paginate.rb