class Middleman::Blog::TagPages

A sitemap resource manipulator that adds a tag page to the sitemap for each tag in the associated blog

Public Class Methods

new(app, blog_controller) click to toggle source

Initialise Tag pages

@param app [Object] Middleman app @param blog_controller [Object] Blog controller

# File lib/middleman-blog/tag_pages.rb, line 18
def initialize(app, blog_controller)
  @sitemap            = app.sitemap
  @blog_controller    = blog_controller
  @tag_link_template  = uri_template blog_controller.options.taglink
  @tag_template       = blog_controller.options.tag_template
  @blog_data          = blog_controller.data
  @generate_tag_pages = blog_controller.options.generate_tag_pages
end

Public Instance Methods

manipulate_resource_list(resources) click to toggle source

Update the main sitemap resource list

@param resources [Object] Tag name @return [void]

# File lib/middleman-blog/tag_pages.rb, line 43
def manipulate_resource_list(resources)
  return resources unless @generate_tag_pages

  resources + @blog_data.tags.map do |tag, articles|
    tag_page_resource(tag, articles)
  end
end

Private Instance Methods

tag_page_resource(tag, articles) click to toggle source

Create the tag page resources

@param tag [String] Tag name @param articles [Object] Articles @return [Object] Sitemap

@todo Can we inject the correct locale into the metadata here

# File lib/middleman-blog/tag_pages.rb, line 62
def tag_page_resource(tag, articles)
  Sitemap::ProxyResource.new(@sitemap, link(tag), @tag_template).tap do |p|
    # Detect "formatted" tag in first article - trying to guess the correct format to show
    # tagname = articles.first.tags.detect { |article_tag| safe_parameterize(article_tag) == tag }

    # Add metadata in local variables so it's accessible to later extensions
    p.add_metadata locals: {
      'page_type' => 'tag',
      'tagname' => tag,
      'articles' => articles,
      'blog_controller' => @blog_controller
    }
  end
end