Class: JekyllIndexPages::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-index-pages/generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
50
51
52
53
54
55
56
# File 'lib/jekyll-index-pages/generator.rb', line 5

def generate(site)
  config = site.config["index_pages"] || {}
  config.each do |kind, item|
    permalink = item["permalink"] || "/:label/"
    per_page = item['per_page'] || 10
    layout = item["layout"] || "#{kind}"
    collection = item["collection"]

    next if !site.layouts.key? layout

    doc_group =
      case kind
      when "posts"
        { "posts" => site.posts.docs }
      when "categories"
        site.categories
      when "tags"
        site.tags
      when "archive"
        site.data["archive"]
      when "authors"
        site.data["authors"]
      else
        { collection => site.data["collectionz"][collection] } if collection
      end

    doc_group.each do |label, docs|
      sorted_docs = docs.sort { |x, y| y.date <=> x.date }
      pagination = Pagination.new(sorted_docs, per_page)

      (0...pagination.total).each do |current|
        pagination.paginate(current)

        pager = pagination.pager

        label_slug =
          I18n.transliterate(
            Jekyll::Utils.slugify(label),
            :locale => I18n.locale
          )
        dir =
          File.join(
            permalink.sub(":label", label_slug),
            (pager.current_page > 1) ? pager.current_page.to_s : ""
          )

        site.pages <<
          IndexPage.new(site, site.source, dir, item, label, pager)
      end
    end
  end
end