Class: Middleman::BlogPage::BlogPageData

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-blog_page/blog_page_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

Constructor Details

#initialize(app, options = {}, controller = nil) ⇒ BlogPageData

Returns a new instance of BlogPageData.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/middleman-blog_page/blog_page_data.rb', line 22

def initialize(app, options={}, controller=nil)
  @app = app
  @options = options
  @controller = controller

  # A list of resources corresponding to blog page articles
  @_articles = []

  matcher = Regexp.escape(options.sources).
      sub(/^\//, "").
      sub(":title", "([^/]+)")

  subdir_matcher = matcher.sub(/\\\.[^.]+$/, "(/.*)$")

  @path_matcher = /^#{matcher}/
  @subdir_matcher = /^#{subdir_matcher}/

  # Build a hash of part name to capture index, e.g. {"year" => 0}
  @matcher_indexes = {}
  options.sources.scan(/:title/).
    each_with_index do |key, i|
      @matcher_indexes[key[1..-1]] = i
    end
  # The path always appears at the end.
  @matcher_indexes["path"] = @matcher_indexes.size
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



19
20
21
# File 'lib/middleman-blog_page/blog_page_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_page/blog_page_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_page/blog_page_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_page/blog_page_data.rb', line 9

def path_matcher
  @path_matcher
end

Instance Method Details



128
129
130
# File 'lib/middleman-blog_page/blog_page_data.rb', line 128

def custom_permalink_components
  permalink_url_components.reject { |component| component.to_sym == :title }
end

#inspectObject



136
137
138
# File 'lib/middleman-blog_page/blog_page_data.rb', line 136

def inspect
  "#<Middleman::BlogPage::BlogPageData: #{articles.inspect}>"
end

#manipulate_resource_list(resources) ⇒ void

This method returns an undefined value.

Updates’ blog articles destination paths to be the permalink.



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
# File 'lib/middleman-blog_page/blog_page_data.rb', line 69

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

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

      if @controller
        resource.blog_page_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
      article_path = article_path.sub(":title", match[@matcher_indexes["title"]]) if @matcher_indexes["title"]
      puts article_path

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

      # 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

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

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

Returns:

  • (Middleman::Sitemap::Resource)


57
58
59
60
61
62
63
64
# File 'lib/middleman-blog_page/blog_page_data.rb', line 57

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

#pagesArray<Middleman::Sitemap::Resource>

A list of all blog articles, sorted by descending priority

Returns:

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


51
52
53
# File 'lib/middleman-blog_page/blog_page_data.rb', line 51

def pages
  @_articles.sort_by(&:priority).reverse
end


118
119
120
121
122
123
124
125
126
# File 'lib/middleman-blog_page/blog_page_data.rb', line 118

def parse_permalink_options(resource)
  permalink = options.permalink.sub(':title', resource.slug)

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

  permalink
end


132
133
134
# File 'lib/middleman-blog_page/blog_page_data.rb', line 132

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