Sha256: df9ad71035af8a003d10a2b75834e46177c807b52b38fa341d509a2da0acc6ad

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'middleman-blog/uri_templates'

module Middleman
  module Blog
    # This adds new summary pages for arbitrarily defined blog article properties
    class CustomPages
      include UriTemplates

      attr_reader :property

      def initialize(property, app, controller, options)
        @property = property
        @sitemap = app.sitemap
        @blog_controller = controller
        @blog_data = controller.data
        @link_template = uri_template options[:link]
        @page_template = options[:template]
      end

      # Return a path to the page for this property value.
      #
      # @param [String] value
      def link(value)
        apply_uri_template @link_template, property => safe_parameterize(value)
      end

      def manipulate_resource_list(resources)
        articles_by_property = @blog_data.articles
                                         .select { |a| a.data[property] }
                                         .group_by { |a| a.data[property] }
        resources + articles_by_property.map do |property_value, articles|
          build_resource(link(property_value), property_value, articles)
        end
      end

      private

      def build_resource(path, value, articles)
        articles = articles.sort_by(&:date).reverse
        Sitemap::ProxyResource.new(@sitemap, path, @page_template).tap do |p|
          p.add_metadata locals: {
            'page_type' => property.to_s,
            property => value,
            'articles' => articles,
            'blog_controller' => @blog_controller
          }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-blog-4.0.3 lib/middleman-blog/custom_pages.rb