Class: Middleman::Blog::CalendarPages

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, controller = nil) ⇒ CalendarPages

Returns a new instance of CalendarPages.



32
33
34
35
# File 'lib/middleman-blog/calendar_pages.rb', line 32

def initialize(app, controller=nil)
  @app = app
  @blog_controller = controller
end

Class Method Details

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

Parameters:

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

Returns:

  • (String)


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

def link(blog_options, year, month=nil, day=nil)
  path = if day
           blog_options.day_link.
             sub(':year', year.to_s).
             sub(':month', month.to_s.rjust(2,'0')).
             sub(':day', day.to_s.rjust(2,'0'))
         elsif month
           blog_options.month_link.
             sub(':year', year.to_s).
             sub(':month', month.to_s.rjust(2,'0'))
         else
           blog_options.year_link.
             sub(':year', year.to_s)
         end
  ::Middleman::Util.normalize_path(path)
end

Instance Method Details

#blog_dataObject



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

def blog_data
  if @blog_controller
    @blog_controller.data
  else
    @app.blog
  end
end

#blog_optionsObject



45
46
47
48
49
50
51
# File 'lib/middleman-blog/calendar_pages.rb', line 45

def blog_options
  if @blog_controller
    @blog_controller.options
  else
    @app.blog.options
  end
end

#manipulate_resource_list(resources)

This method returns an undefined value.

Update the main sitemap resource list



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/middleman-blog/calendar_pages.rb', line 55

def manipulate_resource_list(resources)
  new_resources = []

  # Set up date pages if the appropriate templates have been specified
  self.blog_data.articles.group_by {|a| a.date.year }.each do |year, year_articles|
    if self.blog_options.year_template
      path = CalendarPages.link(self.blog_options, year)
    
      p = ::Middleman::Sitemap::Resource.new(
        @app.sitemap,
        path
      )
      p.proxy_to(self.blog_options.year_template)

      # Add metadata in local variables so it's accessible to
      # later extensions
      p. :locals => {
        'page_type' => 'year',
        'year' => year,
        'articles' => year_articles,
        'blog_controller' => @blog_controller
      }
      # Add metadata in instance variables for backwards compatibility
      p. do
        @year = year
        @articles = year_articles
      end

      new_resources << p
    end
      
    year_articles.group_by {|a| a.date.month }.each do |month, month_articles|
      if self.blog_options.month_template
        path = CalendarPages.link(self.blog_options, year, month)
    
        p = ::Middleman::Sitemap::Resource.new(
          @app.sitemap,
          path
        )
        p.proxy_to(self.blog_options.month_template)

        p. :locals => {
          'page_type' => 'month',
          'year' => year,
          'month' => month,
          'articles' => month_articles,
          'blog_controller' => @blog_controller
        }
        p. do
          @year = year
          @month = month
          @articles = month_articles
        end

        new_resources << p
      end
      
      month_articles.group_by {|a| a.date.day }.each do |day, day_articles|
        if self.blog_options.day_template
          path = CalendarPages.link(self.blog_options, year, month, day)

          p = ::Middleman::Sitemap::Resource.new(
            @app.sitemap,
            path
          )
          p.proxy_to(self.blog_options.day_template)

          p. :locals => {
            'page_type' => 'day',
            'year' => year,
            'month' => month,
            'day' => day,
            'articles' => day_articles,
            'blog_controller' => @blog_controller
          }
          p. do
            @year = year
            @month = month
            @day = day
            @articles = day_articles
          end

          new_resources << p
        end
      end
    end
  end

  resources + new_resources
end