Class: Middleman::Blog::BlogData

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-blog/blog_data.rb

Overview

A store of all the blog articles in the site, with accessors for the articles by various dimensions. Accessed via “blog” in templates.

Constant Summary collapse

[:year, :month, :day, :title]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



19
20
21
# File 'lib/middleman-blog/blog_data.rb', line 19

def controller
  @controller
end

#matcher_indexesHash (readonly)

A hash of indexes into the path_matcher captures

Returns:

  • (Hash)


13
14
15
# File 'lib/middleman-blog/blog_data.rb', line 13

def matcher_indexes
  @matcher_indexes
end

#optionsThor::CoreExt::HashWithIndifferentAccess (readonly)

The configured options for this blog

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)


17
18
19
# File 'lib/middleman-blog/blog_data.rb', line 17

def options
  @options
end

#path_matcherRegex (readonly)

A regex for matching blog article source paths

Returns:

  • (Regex)


9
10
11
# File 'lib/middleman-blog/blog_data.rb', line 9

def path_matcher
  @path_matcher
end

Instance Method Details

#article(path) ⇒ Middleman::Sitemap::Resource

The BlogArticle for the given path, or nil if one doesn’t exist.

Returns:

  • (Middleman::Sitemap::Resource)


62
63
64
65
66
67
68
69
# File 'lib/middleman-blog/blog_data.rb', line 62

def article(path)
  article = @app.sitemap.find_resource_by_path(path.to_s)
  if article && article.is_a?(BlogArticle)
    article
  else
    nil
  end
end

#articlesArray<Middleman::Sitemap::Resource>

A list of all blog articles, sorted by descending date

Returns:

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


56
57
58
# File 'lib/middleman-blog/blog_data.rb', line 56

def articles
  @_articles.sort_by(&:date).reverse
end


157
158
159
# File 'lib/middleman-blog/blog_data.rb', line 157

def custom_permalink_components
  permalink_url_components.reject { |component| DEFAULT_PERMALINK_COMPONENTS.include? component.to_sym }
end

#inspectObject



165
166
167
# File 'lib/middleman-blog/blog_data.rb', line 165

def inspect
  "#<Middleman::Blog::BlogData: #{articles.inspect}>"
end

#manipulate_resource_list(resources)

This method returns an undefined value.

Updates’ blog articles destination paths to be the permalink.



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
# File 'lib/middleman-blog/blog_data.rb', line 93

def manipulate_resource_list(resources)
  @_articles = []
  used_resources = []

  resources.each do |resource|
    if resource.path =~ path_matcher
      resource.extend BlogArticle

      if @controller
        resource.blog_controller = controller
      end

      # Skip articles that are not published (in non-development environments)
      next unless @app.environment == :development || resource.published?

      # compute output path:
      #   substitute date parts to path pattern
      resource.destination_path = Middleman::Util.normalize_path parse_permalink_options(resource)

      @_articles << resource

    elsif resource.path =~ @subdir_matcher
      match = $~.captures

      article_path = options.sources
      %w(year month day title).each do |token|
        article_path = article_path.sub(":#{token}", match[@matcher_indexes[token]]) if @matcher_indexes[token]
      end

      article = @app.sitemap.find_resource_by_path(article_path)
      raise "Article for #{resource.path} not found" if article.nil?
      article.extend BlogArticle

      # Skip files that belong to articles that are not published (in non-development environments)
      next unless @app.environment == :development || article.published?

      # The subdir path is the article path with the index file name
      # or file extension stripped off.
      resource.destination_path = parse_permalink_options(article).
        sub(/(\/#{@app.index_file}$)|(\.[^.]+$)|(\/$)/, match[@matcher_indexes["path"]])

      resource.destination_path = Middleman::Util.normalize_path(resource.destination_path)
    end

    used_resources << resource
  end

  used_resources
end


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/middleman-blog/blog_data.rb', line 143

def parse_permalink_options(resource)
  permalink = options.permalink.
    sub(':year', resource.date.year.to_s).
    sub(':month', resource.date.month.to_s.rjust(2, '0')).
    sub(':day', resource.date.day.to_s.rjust(2, '0')).
    sub(':title', resource.slug)

  custom_permalink_components.each do |component|
    permalink = permalink.sub(":#{component}", resource.data[component].parameterize)
  end

  permalink
end


161
162
163
# File 'lib/middleman-blog/blog_data.rb', line 161

def permalink_url_components
  options.permalink.scan(/:([A-Za-z0-9]+)/).flatten
end

#tagsHash<String, Array<Middleman::Sitemap::Resource>>

Returns a map from tag name to an array of BlogArticles associated with that tag.

Returns:

  • (Hash<String, Array<Middleman::Sitemap::Resource>>)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/middleman-blog/blog_data.rb', line 74

def tags
  tags = {}
  @_articles.each do |article|
    article.tags.each do |tag|
      tags[tag] ||= []
      tags[tag] << article
    end
  end

  tags.each do |tag, articles|
    tags[tag] = articles.sort_by(&:date).reverse
  end

  tags
end