Class: Alula::Paginate

Inherits:
Generator show all
Defined in:
lib/alula/generators/paginate.rb

Instance Attribute Summary

Attributes inherited from Generator

#options, #site

Instance Method Summary collapse

Methods inherited from Generator

#allow_compressing?, generators, #generators, #initialize, load, register

Constructor Details

This class inherits a constructor from Alula::Generator

Instance Method Details

#generateObject



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
47
48
49
# File 'lib/alula/generators/paginate.rb', line 3

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..generator == self.generator and item.languages.include?(locale) }
    }
    )
  end
end

#substitutes(locale, item) ⇒ Object



51
52
53
54
55
# File 'lib/alula/generators/paginate.rb', line 51

def substitutes(locale, item)
  {
    "page" => item..pagenum.to_s,
  }
end