Class: Middleman::Sitemap::Resource
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Resource
- Includes:
- Contracts, Extensions::Traversal
- Defined in:
- lib/middleman-core/sitemap/resource.rb,
lib/middleman-core/sitemap/extensions/proxies.rb
Overview
Sitemap Resource class
Direct Known Subclasses
Extensions::EndpointResource, Extensions::RedirectResource, ProxyResource, StringResource
Constant Summary collapse
Constants included from Contracts
Instance Attribute Summary collapse
-
#destination_path ⇒ String
(also: #request_path)
The output path in the build directory for this resource.
-
#file_descriptor ⇒ Object
readonly
Returns the value of attribute file_descriptor.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ String
readonly
The source path of this resource (relative to the source directory, without template extensions).
Instance Method Summary collapse
- #add_metadata(meta = {}) ⇒ 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.
- #ignore! ⇒ Object
- #ignored? ⇒ Boolean
-
#indifferent_access? ⇒ Hash
Data about this resource, populated from frontmatter or extensions.
-
#initialize(store, path, source = nil) ⇒ Resource
constructor
A new instance of Resource.
- #locals ⇒ Object
-
#METADATA_HASH ⇒ Hash
The metadata for this resource.
- #options ⇒ Object
- #proxy_to(_path) ⇒ Object
- #render(opts = {}, locs = {}) ⇒ Object
- #source_file ⇒ Object
-
#String ⇒ String
A path without the directory index - so foo/index.html becomes just foo.
- #template? ⇒ Boolean
- #to_s ⇒ Object (also: #inspect)
- #url ⇒ Object
Methods included from Extensions::Traversal
#children, #directory_index?, #eponymous_directory?, #eponymous_directory_path, #parent, #siblings
Methods included from Contracts
Constructor Details
#initialize(store, path, source = nil) ⇒ Resource
Returns a new instance of Resource.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/middleman-core/sitemap/resource.rb', line 46 def initialize(store, path, source=nil) @store = store @app = @store.app @path = path source = Pathname(source) if source && source.is_a?(String) if source && source.is_a?(Pathname) @file_descriptor = ::Middleman::SourceFile.new(source.relative_path_from(@app.source_dir), source, @app.source_dir, Set.new([:source])) else @file_descriptor = source end @destination_path = @path # Options are generally rendering/sitemap options # Locals are local variables for rendering this resource's template # Page are data that is exposed through this resource's data member. # Note: It is named 'page' for backwards compatibility with older MM. @metadata = { options: {}, locals: {}, page: {} } end |
Instance Attribute Details
#destination_path ⇒ String Also known as: request_path
The output path in the build directory for this resource
22 23 24 |
# File 'lib/middleman-core/sitemap/resource.rb', line 22 def destination_path @destination_path end |
#file_descriptor ⇒ Object (readonly)
Returns the value of attribute file_descriptor.
27 28 29 |
# File 'lib/middleman-core/sitemap/resource.rb', line 27 def file_descriptor @file_descriptor end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
39 40 41 |
# File 'lib/middleman-core/sitemap/resource.rb', line 39 def @metadata end |
#path ⇒ String (readonly)
The source path of this resource (relative to the source directory, without template extensions)
18 19 20 |
# File 'lib/middleman-core/sitemap/resource.rb', line 18 def path @path end |
Instance Method Details
#add_metadata(meta = {}) ⇒ Object
90 91 92 |
# File 'lib/middleman-core/sitemap/resource.rb', line 90 def (={}) @metadata.deep_merge!() end |
#Any
This method returns an undefined value.
Ignore a resource directly, without going through the whole ignore filter stuff.
167 |
# File 'lib/middleman-core/sitemap/resource.rb', line 167 Contract Any |
#binary? ⇒ Boolean
160 161 162 |
# File 'lib/middleman-core/sitemap/resource.rb', line 160 def binary? !file_descriptor.nil? && ::Middleman::Util.binary?(file_descriptor[:full_path].to_s) end |
#Bool ⇒ Boolean
Whether the Resource is ignored
70 |
# File 'lib/middleman-core/sitemap/resource.rb', line 70 Contract Bool |
#content_type ⇒ Object
190 191 192 |
# File 'lib/middleman-core/sitemap/resource.rb', line 190 def content_type [:content_type] || ::Rack::Mime.mime_type(ext, nil) end |
#data ⇒ Object
97 98 99 |
# File 'lib/middleman-core/sitemap/resource.rb', line 97 def data ::Middleman::Util.recursively_enhance([:page]) end |
#ext ⇒ Object
119 120 121 |
# File 'lib/middleman-core/sitemap/resource.rb', line 119 def ext File.extname(path) end |
#Hash ⇒ String
Render this resource
104 |
# File 'lib/middleman-core/sitemap/resource.rb', line 104 Contract Hash |
#ignore! ⇒ Object
168 169 170 |
# File 'lib/middleman-core/sitemap/resource.rb', line 168 def ignore! @ignored = true end |
#ignored? ⇒ Boolean
175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/middleman-core/sitemap/resource.rb', line 175 def ignored? return true if @ignored # Ignore based on the source path (without template extensions) return true if @app.sitemap.ignored?(path) # This allows files to be ignored by their source file name (with template extensions) if !self.is_a?(ProxyResource) && file_descriptor && @app.sitemap.ignored?(file_descriptor[:relative_path].to_s) true else false end end |
#indifferent_access? ⇒ Hash
Data about this resource, populated from frontmatter or extensions.
|
# File 'lib/middleman-core/sitemap/resource.rb', line 96
|
#locals ⇒ Object
112 113 114 |
# File 'lib/middleman-core/sitemap/resource.rb', line 112 def locals [:locals] end |
#METADATA_HASH ⇒ Hash
The metadata for this resource
38 |
# File 'lib/middleman-core/sitemap/resource.rb', line 38 Contract METADATA_HASH |
#options ⇒ Object
105 106 107 |
# File 'lib/middleman-core/sitemap/resource.rb', line 105 def [:options] end |
#proxy_to(_path) ⇒ Object
89 90 91 |
# File 'lib/middleman-core/sitemap/extensions/proxies.rb', line 89 def proxy_to(_path) throw 'Resource#proxy_to has been removed. Use ProxyResource class instead.' end |
#render(opts = {}, locs = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/middleman-core/sitemap/resource.rb', line 126 def render(opts={}, locs={}) return ::Middleman::FileRenderer.new(@app, file_descriptor[:full_path].to_s).template_data_for_file unless template? ::Middleman::Util.instrument 'render.resource', path: file_descriptor[:full_path].to_s, destination_path: destination_path do md = opts = md[:options].deep_merge(opts) locs = md[:locals].deep_merge(locs) locs[:current_path] ||= destination_path # Certain output file types don't use layouts opts[:layout] = false if !opts.key?(:layout) && ext != '.html' renderer = ::Middleman::TemplateRenderer.new(@app, file_descriptor[:full_path].to_s) renderer.render(locs, opts) end end |
#source_file ⇒ Object
79 80 81 |
# File 'lib/middleman-core/sitemap/resource.rb', line 79 def source_file file_descriptor && file_descriptor[:full_path].to_s end |
#String ⇒ String
A path without the directory index - so foo/index.html becomes just foo. Best for linking.
78 |
# File 'lib/middleman-core/sitemap/resource.rb', line 78 Contract String |
#template? ⇒ Boolean
71 72 73 74 |
# File 'lib/middleman-core/sitemap/resource.rb', line 71 def template? return false if file_descriptor.nil? !::Tilt[file_descriptor[:full_path].to_s].nil? end |
#to_s ⇒ Object Also known as: inspect
194 195 196 |
# File 'lib/middleman-core/sitemap/resource.rb', line 194 def to_s "#<#{self.class} path=#{@path}>" end |
#url ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/middleman-core/sitemap/resource.rb', line 147 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 |