aboutsummaryrefslogblamecommitdiff
path: root/_plugins/generate-feeds-for-categories.rb
blob: f5cc8db045e346ccd26f2da7e1c6f58fc995e883 (plain) (tree)















































                                                                                                                                     
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