aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-feeds-for-categories.rb
blob: f5cc8db045e346ccd26f2da7e1c6f58fc995e883 (about) (plain) (blame)
1
2
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
module Jekyll
  class FeedsForCategoriesGenerator < Generator
    safe true

    def gen_categories_index(site)
      categories_index = {}
      site.collections.each do |collection|
        collection_name, collection_documents = collection
        collection_documents.docs.each do |document|
          lang = document.data["lang"]
          categories = document.data["eu_categories"]
          if categories != nil
            categories.split(",").each do |category|
              categories_index[collection_name] ||= {}
              categories_index[collection_name][lang] ||= {}
              categories_index[collection_name][lang][category] ||= []

              categories_index[collection_name][lang][category] << document
            end
          end
        end
      end
      categories_index
    end

    def generate(site)
      gen_categories_index(site).each do |collection_name, langs|
        langs.each do |lang, categories|
          categories.each do |category_name, articles|
            category = category_name.gsub(/ /, '-')
            feed_name = "feed.#{collection_name}-by-category.#{lang}.#{category}.atom"
            page = PageWithoutAFile.new(site, site.source, "", feed_name)
            page.content = <<-EOF
{% include feed.atom entries=site.#{collection_name} kind="#{collection_name}" lang="#{lang}" categories_filter="#{category_name}" %}
            EOF
            site.pages << page
          end
        end
      end
    end
  end

  class PageWithoutAFile < Jekyll::Page
    def read_yaml(*)
      @data ||= {}
    end
  end
end