Class: Middleman::Sitemap::Resource
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Resource
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
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::PATH_MATCHER
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#<=>(other) ⇒ Object
-
#add_filter(filter) ⇒ Object
-
#add_metadata(data, reverse = false) ⇒ Object
-
#add_metadata_locals(locs, reverse = false) ⇒ Object
-
#add_metadata_options(opts, reverse = false) ⇒ Object
-
#add_metadata_page(page, reverse = false) ⇒ Object
-
#Any
Ignore a resource directly, without going through the whole ignore filter stuff.
-
#binary? ⇒ Boolean
-
#Bool ⇒ Boolean
Whether the Resource is ignored.
-
#content_type ⇒ Object
-
#data ⇒ Object
-
#ext ⇒ Object
-
#Hash ⇒ String
Render this resource without content filters.
-
#ignore! ⇒ Object
-
#ignored? ⇒ Boolean
-
#initialize(store, path, source = nil, priority = 1) ⇒ Resource
constructor
A new instance of Resource.
-
#locals ⇒ Object
-
#metadata ⇒ Object
-
#normalized_path ⇒ String
The normalized source path of this resource (relative to the source directory, without template extensions).
-
#options ⇒ Object
-
#page ⇒ Object
-
#page_id ⇒ Object
-
#proxy_to(_path) ⇒ Object
-
#render(options_hash = ::Middleman::EMPTY_HASH, locs = ::Middleman::EMPTY_HASH, &_block) ⇒ Object
-
#render_without_filters(options_hash = ::Middleman::EMPTY_HASH, locals_hash = ::Middleman::EMPTY_HASH) ⇒ Object
-
#source_file ⇒ Object
-
#static_file? ⇒ Boolean
-
#String ⇒ String
A path without the directory index - so foo/index.html becomes just foo.
-
#template? ⇒ Boolean
-
#to_s ⇒ Object
(also: #inspect)
-
#url ⇒ Object
#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
@metadata_options = ::Middleman::EMPTY_HASH
@metadata_locals = ::Middleman::EMPTY_HASH
@metadata_page = ::Middleman::EMPTY_HASH
@page_data = nil
end
|
Instance Attribute Details
#app ⇒ Object
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_path ⇒ String
Also known as:
request_path
The output path in the build directory for this resource
31
32
33
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 31
def destination_path
@destination_path
end
|
#file_descriptor ⇒ Object
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
|
The source path of this resource (relative to the source directory,
without template extensions)
27
28
29
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 27
def path
@path
end
|
#priority ⇒ Object
Returns the value of attribute priority.
44
45
46
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 44
def priority
@priority
end
|
#vertices ⇒ Object
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
|
154
155
156
157
158
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 154
def add_metadata(data, reverse = false)
add_metadata_page(data[:page], reverse) if data.key? :page
add_metadata_options(data[:options], reverse) if data.key? :options
add_metadata_locals(data[:locals], reverse) if data.key? :locals
end
|
127
128
129
130
131
132
133
134
135
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 127
def add_metadata_locals(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
|
116
117
118
119
120
121
122
123
124
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 116
def add_metadata_options(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 add_metadata_page(page, reverse = false)
@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
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
|
#Bool ⇒ Boolean
Whether the Resource is ignored
89
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 89
Contract Bool
|
#content_type ⇒ Object
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
|
#ext ⇒ Object
201
202
203
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 201
def ext
File.extname(path)
end
|
Render this resource without content filters
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
297
298
299
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 297
def ignored?
@ignored
end
|
#locals ⇒ Object
183
184
185
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 183
def locals
@metadata_locals
end
|
190
191
192
193
194
195
196
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 190
def metadata
{
page: page,
options: options,
locals: locals
}.freeze
end
|
#normalized_path ⇒ String
The normalized source path of this resource (relative to the source directory,
without template extensions)
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
|
#options ⇒ Object
176
177
178
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 176
def options
@metadata_options
end
|
#page ⇒ Object
168
169
170
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 168
def page
@metadata_page
end
|
#page_id ⇒ Object
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
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_file ⇒ Object
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
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
|
A path without the directory index - so foo/index.html becomes
just foo. Best for linking.
200
|
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 200
Contract String
|
#template? ⇒ 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_s ⇒ Object
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
|
#url ⇒ Object
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
|