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::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

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


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

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


20
21
22
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 20

def app
  @app
end

#destination_pathString Also known as: request_path

The output path in the build directory for this resource


29
30
31
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 29

def destination_path
  @destination_path
end

#file_descriptorObject (readonly)

Returns the value of attribute file_descriptor


34
35
36
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 34

def file_descriptor
  @file_descriptor
end

#pathString (readonly)

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


25
26
27
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 25

def path
  @path
end

#priorityObject (readonly)

Returns the value of attribute priority


42
43
44
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 42

def priority
  @priority
end

#verticesObject (readonly)

Returns the value of attribute vertices


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

def vertices
  @vertices
end

Instance Method Details

#<=>(other) ⇒ Object


317
318
319
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 317

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

#add_filter(filter) ⇒ Object


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

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


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

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


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

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


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

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


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

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.


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

Contract Any

#binary?Boolean


272
273
274
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 272

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


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

Contract Bool

#content_typeObject


302
303
304
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 302

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

#dataObject


161
162
163
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 161

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

#extObject


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

def ext
  File.extname(path)
end

#HashString

Render this resource without content filters


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

Contract Hash

#ignore!Object


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

def ignore!
  @ignored = true
end

#ignored?Boolean


295
296
297
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 295

def ignored?
  @ignored
end

#localsObject


181
182
183
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 181

def locals
  @metadata_locals
end

#metadataObject


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

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)


309
310
311
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 309

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

#optionsObject


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

def options
  @metadata_options
end

#pageObject


166
167
168
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 166

def page
  @metadata_page
end

#page_idObject


109
110
111
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 109

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

#proxy_to(_path) ⇒ Object


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

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


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

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


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

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


104
105
106
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 104

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

#static_file?Boolean


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

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.


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

Contract String

#template?Boolean


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

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


321
322
323
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 321

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

#urlObject


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

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