Class: Middleman::Blog::CalendarPages

Inherits:
Object
  • Object
show all
Includes:
UriTemplates
Defined in:
lib/middleman-blog/calendar_pages.rb

Overview

A sitemap plugin that adds month/day/year pages to the sitemap based on the dates of blog articles.

Instance Method Summary collapse

Methods included from UriTemplates

apply_uri_template, date_to_params, extract_directory_path, extract_params, safe_parameterize, uri_template

Constructor Details

#initialize(app, blog_controller) ⇒ CalendarPages

Returns a new instance of CalendarPages.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/middleman-blog/calendar_pages.rb', line 12

def initialize(app, blog_controller)
  @sitemap = app.sitemap
  @blog_controller = blog_controller
  @blog_data = blog_controller.data

  blog_options = blog_controller.options
  @day_link_template = uri_template blog_options.day_link
  @month_link_template = uri_template blog_options.month_link
  @year_link_template = uri_template blog_options.year_link
  @day_template = blog_options.day_template
  @month_template = blog_options.month_template
  @year_template = blog_options.year_template
  @generate_year_pages = blog_options.generate_year_pages
  @generate_month_pages = blog_options.generate_month_pages
  @generate_day_pages = blog_options.generate_day_pages
end

Instance Method Details

Get a path to the given calendar page, based on the :year_link, :month_link or :day_link setting.

Parameters:

  • year (Number)
  • month (Number) (defaults to: nil)
  • day (Number) (defaults to: nil)

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/middleman-blog/calendar_pages.rb', line 34

def link(year, month = nil, day = nil)
  template = if day
    @day_link_template
  elsif month
    @month_link_template
  else
    @year_link_template
  end

  apply_uri_template template, date_to_params(Date.new(year, month || 1, day || 1))
end

#manipulate_resource_list(resources) ⇒ Array<Middleman::Sitemap::Resource>

Update the main sitemap resource list

Returns:

  • (Array<Middleman::Sitemap::Resource>)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/middleman-blog/calendar_pages.rb', line 48

def manipulate_resource_list(resources)
  new_resources = []

  # Set up date pages if the appropriate templates have been specified
  @blog_data.articles.group_by { |a| a.date.year }.each do |year, year_articles|
    new_resources << year_page_resource(year, year_articles) if @generate_year_pages && @year_template

    year_articles.group_by { |a| a.date.month }.each do |month, month_articles|
      new_resources << month_page_resource(year, month, month_articles) if @generate_month_pages && @month_template

      month_articles.group_by { |a| a.date.day }.each do |day, day_articles|
        new_resources << day_page_resource(year, month, day, day_articles) if @generate_day_pages && @day_template
      end
    end
  end

  resources + new_resources
end