aboutsummaryrefslogtreecommitdiff
path: root/_plugins/generate-feeds-for-categories.rb
blob: 870f86f33bf5266ed8960395da29bb4a89fcd8e5 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Jekyll
  class FeedsGenerator < 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 feeds_by_category(site)
      feed_pages = []
      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
            feed_pages << page
          end
        end
      end
      feed_pages
    end

    def global_feeds(site)
      feed_pages = []
      langs = site.config['langs']
      collections = site.config['collections'].keys
      langs.each do |lang|
        collections.each do |collection|
          feed_name = "feed.#{collection}.#{lang}.atom"
          page = PageWithoutAFile.new(site, site.source, "", feed_name)
          page.content = <<-EOF
{% include feed.atom entries=site.#{collection} kind="#{collection}" lang="#{lang}" %}
          EOF
          feed_pages << page
        end
      end
      feed_pages
    end

    def generate(site)
      site.pages.concat(feeds_by_category(site))
      site.pages.concat(global_feeds(site))
    end
  end

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