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.

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)


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

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>)


54
55
56
# File 'lib/middleman-blog/blog_data.rb', line 54

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

#manipulate_resource_list(resources)

This method returns an undefined value.

Updates’ blog articles destination paths to be the permalink.



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
145
146
147
148
149
150
# File 'lib/middleman-blog/blog_data.rb', line 91

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 = 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)

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

      @_articles << resource

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

      article_path = options.sources.
        sub(':year', match[@matcher_indexes["year"]]).
        sub(':month', match[@matcher_indexes["month"]]).
        sub(':day', match[@matcher_indexes["day"]]).
        sub(':title', match[@matcher_indexes["title"]])

      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 = options.permalink.
        sub(':year', article.date.year.to_s).
        sub(':month', article.date.month.to_s.rjust(2,'0')).
        sub(':day', article.date.day.to_s.rjust(2,'0')).
        sub(':title', article.slug).
        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

#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>>)


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

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