Class: Middleman::Sitemap::Resource

Inherits:
Object
  • Object
show all
Includes:
Comparable, Contracts, Extensions::Traversal
Defined in:
middleman-core/lib/middleman-core/sitemap/resource.rb,
middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb

Overview

Sitemap Resource class

Constant Summary collapse

MAYBE_METADATA_CONTRACT =

Backwards compat to old API from MM v4.

{ page: Maybe[Hash], options: Maybe[Hash], locals: Maybe[Hash] }.freeze
METADATA_CONTRACT =

Backwards compat to old API from MM v4.

{ page: Hash, options: Hash, locals: Hash }.freeze
FILTER =
Or[RespondTo[:call], Filter]

Constants included from Contracts

Contracts::ImmutableSortedSetOf, Contracts::PATH_MATCHER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Traversal

#children, #directory_index?, #eponymous_directory?, #eponymous_directory_path, #parent, #parent_helper, #siblings, #traversal_root

Methods included from Contracts

#Contract

Constructor Details

#initialize(store, path, source = nil, priority = 1) ⇒ Resource

Returns a new instance of Resource.


54
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
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 54

def initialize(store, path, source = nil, priority = 1)
  @store    = store
  @app      = @store.app
  @path     = path
  @ignored  = false
  @filters  = ::Hamster::SortedSet.empty
  @priority = priority
  @vertices = ::Hamster::Set.empty

  source = Pathname(source) if source&.is_a?(String)

  @file_descriptor = if source&.is_a?(Pathname)
                       ::Middleman::SourceFile.new(source.relative_path_from(@app.source_dir), source, @app.source_dir, Set.new([:source]), 0)
                     else
                       source
                     end

  @destination_path = @path

  # Options are generally rendering/sitemap options
  @metadata_options = ::Middleman::EMPTY_HASH

  # Locals are local variables for rendering this resource's template
  @metadata_locals = ::Middleman::EMPTY_HASH

  # Page are data that is exposed through this resource's data member.
  # Note: It is named 'page' for backwards compatibility with older MM.
  @metadata_page = ::Middleman::EMPTY_HASH

  # Recursively enhanced page data cache
  @page_data = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


22
23
24
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 22

def app
  @app
end

#destination_pathString Also known as: request_path

The output path in the build directory for this resource

Returns:


31
32
33
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 31

def destination_path
  @destination_path
end

#file_descriptorObject (readonly)

Returns the value of attribute file_descriptor


36
37
38
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 36

def file_descriptor
  @file_descriptor
end

#pathString (readonly)

The source path of this resource (relative to the source directory, without template extensions)

Returns:


27
28
29
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 27

def path
  @path
end

#priorityObject (readonly)

Returns the value of attribute priority


44
45
46
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 44

def priority
  @priority
end

#verticesObject (readonly)

Returns the value of attribute vertices


47
48
49
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 47

def vertices
  @vertices
end

Instance Method Details

#<=>(other) ⇒ Object


319
320
321
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 319

def <=>(other)
  [priority, object_id] <=> [other.priority, other.object_id]
end

#add_filter(filter) ⇒ Object


288
289
290
291
292
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 288

def add_filter(filter)
  filter = ProcFilter.new(:"proc_#{filter.object_id}", filter) if filter.respond_to?(:call)

  @filters = @filters.add filter
end

#add_metadata(data, reverse = false) ⇒ Object


154
155
156
157
158
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 154

def (data, reverse = false)
  (data[:page], reverse) if data.key? :page
  (data[:options], reverse) if data.key? :options
  (data[:locals], reverse) if data.key? :locals
end

#add_metadata_locals(locs, reverse = false) ⇒ Object


127
128
129
130
131
132
133
134
135
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 127

def (locs, reverse = false)
  if @metadata_locals == ::Middleman::EMPTY_HASH
    @metadata_locals = locs.dup
  elsif reverse
    @metadata_locals = locs.deep_merge(@metadata_locals)
  else
    @metadata_locals.deep_merge!(locs)
  end
end

#add_metadata_options(opts, reverse = false) ⇒ Object


116
117
118
119
120
121
122
123
124
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 116

def (opts, reverse = false)
  if @metadata_options == ::Middleman::EMPTY_HASH
    @metadata_options = opts.dup
  elsif reverse
    @metadata_options = opts.deep_merge(@metadata_options)
  else
    @metadata_options.deep_merge!(opts)
  end
end

#add_metadata_page(page, reverse = false) ⇒ Object


138
139
140
141
142
143
144
145
146
147
148
149
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 138

def (page, reverse = false)
  # Clear recursively enhanced cache
  @page_data = nil

  if @metadata_page == ::Middleman::EMPTY_HASH
    @metadata_page = page.dup
  elsif reverse
    @metadata_page = page.deep_merge(@metadata_page)
  else
    @metadata_page.deep_merge!(page)
  end
end

#Any

This method returns an undefined value.

Ignore a resource directly, without going through the whole ignore filter stuff.


281
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 281

Contract Any

#binary?Boolean

Returns:

  • (Boolean)

274
275
276
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 274

def binary?
  !file_descriptor.nil? && (file_descriptor[:types].include?(:binary) || ::Middleman::Util.binary?(file_descriptor[:full_path].to_s))
end

#BoolBoolean

Whether the Resource is ignored

Returns:

  • (Boolean)

89
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 89

Contract Bool

#content_typeObject


304
305
306
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 304

def content_type
  options[:content_type] || ::Rack::Mime.mime_type(ext, nil)
end

#dataObject


163
164
165
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 163

def data
  @page_data ||= ::Middleman::Util.recursively_enhance(page)
end

#extObject


201
202
203
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 201

def ext
  File.extname(path)
end

#HashString

Render this resource without content filters

Returns:


175
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 175

Contract Hash

#ignore!Object


282
283
284
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 282

def ignore!
  @ignored = true
end

#ignored?Boolean

Returns:

  • (Boolean)

297
298
299
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 297

def ignored?
  @ignored
end

#localsObject


183
184
185
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 183

def locals
  @metadata_locals
end

#metadataObject


190
191
192
193
194
195
196
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 190

def 
  {
    page: page,
    options: options,
    locals: locals
  }.freeze
end

#normalized_pathString

The normalized source path of this resource (relative to the source directory, without template extensions)

Returns:


311
312
313
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 311

def normalized_path
  @normalized_path ||= ::Middleman::Util.normalize_path @path
end

#optionsObject


176
177
178
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 176

def options
  @metadata_options
end

#pageObject


168
169
170
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 168

def page
  @metadata_page
end

#page_idObject


111
112
113
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 111

def page_id
  @metadata_page[:id] || make_implicit_page_id(destination_path)
end

#proxy_to(_path) ⇒ Object


64
65
66
# File 'middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb', line 64

def proxy_to(_path)
  throw 'Resource#proxy_to has been removed. Use ProxyResource class instead.'
end

#render(options_hash = ::Middleman::EMPTY_HASH, locs = ::Middleman::EMPTY_HASH, &_block) ⇒ Object


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 208

def render(options_hash = ::Middleman::EMPTY_HASH, locs = ::Middleman::EMPTY_HASH, &_block)
  @vertices = ::Hamster::Set.empty

  body = render_without_filters(options_hash, locs)

  return body if @filters.empty?

  @filters.reduce(body) do |output, filter|
    if block_given? && !yield(filter)
      output
    elsif filter.is_a?(Filter)
      result = filter.execute_filter(output)
      @vertices |= result[1]
      result[0]
    else
      output
    end
  end
end

#render_without_filters(options_hash = ::Middleman::EMPTY_HASH, locals_hash = ::Middleman::EMPTY_HASH) ⇒ Object


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 231

def render_without_filters(options_hash = ::Middleman::EMPTY_HASH, locals_hash = ::Middleman::EMPTY_HASH)
  return ::Middleman::FileRenderer.new(@app, file_descriptor[:full_path].to_s).template_data_for_file unless template?

  opts = if options_hash == ::Middleman::EMPTY_HASH
           options.dup
         else
           options.deep_merge(options_hash)
         end

  # Certain output file types don't use layouts
  opts[:layout] = false if !opts.key?(:layout) && !@app.set_of_extensions_with_layout.include?(ext)

  locs = if locals_hash == ::Middleman::EMPTY_HASH
           locals.dup
         else
           locals.deep_merge(locals_hash)
         end

  locs[:current_path] ||= destination_path

  renderer = ::Middleman::TemplateRenderer.new(@app, file_descriptor[:full_path].to_s)
  renderer.render(locs, opts).to_str.tap do
    @vertices |= renderer.vertices
  end
end

#source_fileObject


106
107
108
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 106

def source_file
  file_descriptor && file_descriptor[:full_path].to_s
end

#static_file?Boolean

Returns:

  • (Boolean)

97
98
99
100
101
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 97

def static_file?
  return false if file_descriptor.nil?

  ::Middleman::Util.static_file?(file_descriptor[:full_path].to_s, app.config[:frontmatter_delims])
end

#StringString

A path without the directory index - so foo/index.html becomes just foo. Best for linking.

Returns:


200
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 200

Contract String

#template?Boolean

Returns:

  • (Boolean)

90
91
92
93
94
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 90

def template?
  return false if file_descriptor.nil?

  !::Middleman::Util.tilt_class(file_descriptor[:full_path].to_s).nil?
end

#to_sObject Also known as: inspect


323
324
325
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 323

def to_s
  "#<#{self.class} path=#{@path}>"
end

#urlObject


261
262
263
264
265
266
267
268
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 261

def url
  url_path = destination_path
  if @app.config[:strip_index_file]
    url_path = url_path.sub(%r{(^|/)#{Regexp.escape(@app.config[:index_file])}$},
                            @app.config[:trailing_slash] ? '/' : '')
  end
  File.join(@app.config[:http_prefix], url_path)
end