aboutsummaryrefslogblamecommitdiff
path: root/_plugins/generate-feeds-for-categories.rb
blob: 870f86f33bf5266ed8960395da29bb4a89fcd8e5 (plain) (tree)
1
2
             
                                  






















                                                                           

                               








                                                                                                                                     
                              


             






















                                                                                      




                                       


                           


       
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