Class: Middleman::Blog::Drafts::Data

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

Overview

A store of all the draft articles in the site. Accessed via “blog.drafts” in templates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blog_data, app, options) ⇒ Data

Returns a new instance of Data.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 29

def initialize(blog_data, app, options)
  @blog_data = blog_data
  @options   = options
  @app       = app

  # A list of resources corresponding to draft articles
  @_drafts = []

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

  @path_matcher = /^#{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

#matcher_indexesObject (readonly)

Returns the value of attribute matcher_indexes.



26
27
28
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 26

def matcher_indexes
  @matcher_indexes
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 26

def options
  @options
end

#path_matcherObject (readonly)

Returns the value of attribute path_matcher.



26
27
28
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 26

def path_matcher
  @path_matcher
end

Instance Method Details

#articlesArray<Middleman::Sitemap::Resource>

A list of all draft articles.

Returns:

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


84
85
86
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 84

def articles
  @_drafts
end

#manipulate_resource_list(resources) ⇒ void

This method returns an undefined value.

Updates’ blog draft articles destination paths to be the permalink.



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
# File 'lib/middleman-blog-drafts/blog_data_extensions.rb', line 56

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

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

      next unless @app.environment == :development || @options.build

      # compute output path:
      resource.destination_path = options.permalink.
        sub(':title', resource.slug)

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

      @_drafts << resource
    end

    used_resources << resource
  end

  used_resources
end