Class: Middleman::Sitemap::Resource
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Resource
- Includes:
- Extensions::ContentType, Extensions::Traversal
- Defined in:
- lib/middleman-core/sitemap/resource.rb
Overview
Sitemap Resource class
Direct Known Subclasses
Extensions::Redirects::RedirectResource, Extensions::RequestEndpoints::EndpointResource
Instance Attribute Summary collapse
- #app ⇒ Middleman::Application readonly
-
#destination_path ⇒ String
The output path for this resource.
-
#path ⇒ String
readonly
The source path of this resource (relative to the source directory, without template extensions).
- #store ⇒ Middleman::Sitemap::Store readonly
Instance Method Summary collapse
-
#add_metadata(metadata = {}, &block) ⇒ Object
Merge in new metadata specific to this resource.
-
#binary? ⇒ Boolean
Whether the source file is binary.
-
#ext ⇒ String
Extension of the path (i.e. ‘.js’).
-
#initialize(store, path, source_file = nil) ⇒ Resource
constructor
Initialize resource with parent store and URL.
-
#metadata ⇒ Hash
Get the metadata for both the current source_file and the current path.
-
#render(opts = {}, locs = {}, &block) ⇒ String
Render this resource.
- #request_path ⇒ Object
-
#source_file ⇒ String
Set the on-disk source file for this resource attr_reader :source_file.
-
#template? ⇒ Boolean
Whether this resource has a template file.
-
#url ⇒ String
A path without the directory index - so foo/index.html becomes just foo.
Methods included from Extensions::ContentType
Methods included from Extensions::Traversal
#children, #directory_index?, #eponymous_directory?, #eponymous_directory_path, #parent, #siblings
Constructor Details
#initialize(store, path, source_file = nil) ⇒ Resource
Initialize resource with parent store and URL
40 41 42 43 44 45 46 47 48 |
# File 'lib/middleman-core/sitemap/resource.rb', line 40 def initialize(store, path, source_file=nil) @store = store @app = @store.app @path = path @source_file = source_file @destination_path = @path @local_metadata = { options: {}, locals: {}, page: {}, blocks: [] } end |
Instance Attribute Details
#app ⇒ Middleman::Application (readonly)
13 14 15 |
# File 'lib/middleman-core/sitemap/resource.rb', line 13 def app @app end |
#destination_path ⇒ String
The output path for this resource
26 27 28 |
# File 'lib/middleman-core/sitemap/resource.rb', line 26 def destination_path @destination_path end |
#path ⇒ String (readonly)
The source path of this resource (relative to the source directory, without template extensions)
22 23 24 |
# File 'lib/middleman-core/sitemap/resource.rb', line 22 def path @path end |
#store ⇒ Middleman::Sitemap::Store (readonly)
17 18 19 |
# File 'lib/middleman-core/sitemap/resource.rb', line 17 def store @store end |
Instance Method Details
#add_metadata(metadata = {}, &block) ⇒ Object
Merge in new metadata specific to this resource.
76 77 78 79 80 81 |
# File 'lib/middleman-core/sitemap/resource.rb', line 76 def (={}, &block) = .dup @local_metadata[:blocks] += .delete(:blocks) if .key?(:blocks) @local_metadata.deep_merge!() @local_metadata[:blocks] += [block] if block_given? end |
#binary? ⇒ Boolean
Whether the source file is binary.
146 147 148 |
# File 'lib/middleman-core/sitemap/resource.rb', line 146 def binary? ::Middleman::Util.binary?(source_file) end |
#ext ⇒ String
Extension of the path (i.e. ‘.js’)
85 86 87 |
# File 'lib/middleman-core/sitemap/resource.rb', line 85 def ext File.extname(path) end |
#metadata ⇒ Hash
Get the metadata for both the current source_file and the current path
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/middleman-core/sitemap/resource.rb', line 59 def result = store.(path).dup = store.(source_file).dup result[:blocks] += .delete(:blocks) if .key?(:blocks) result.deep_merge!() = @local_metadata.dup result[:blocks] += .delete(:blocks) if .key?(:blocks) result.deep_merge!() result[:blocks] = result[:blocks].flatten.compact result end |
#render(opts = {}, locs = {}, &block) ⇒ String
Render this resource
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/middleman-core/sitemap/resource.rb', line 95 def render(opts={}, locs={}, &block) return app.template_data_for_file(source_file) unless template? relative_source = Pathname(source_file).relative_path_from(Pathname(app.root)) instrument 'render.resource', path: relative_source, destination_path: destination_path do md = .dup opts = md[:options].deep_merge(opts) # Pass "renderer_options" hash from frontmatter along to renderer if md[:page]['renderer_options'] opts[:renderer_options] = {} md[:page]['renderer_options'].each do |k, v| opts[:renderer_options][k.to_sym] = v end end locs = md[:locals].deep_merge(locs) # Forward remaining data to helpers app.data.store('page', md[:page]) if md.key?(:page) blocks = Array(md[:blocks]).dup blocks << block if block_given? app.current_path ||= destination_path # Certain output file types don't use layouts unless opts.key?(:layout) opts[:layout] = false if %w(.js .json .css .txt).include?(ext) end app.render_template(source_file, locs, opts, blocks) end end |
#request_path ⇒ Object
89 90 91 |
# File 'lib/middleman-core/sitemap/resource.rb', line 89 def request_path destination_path end |
#source_file ⇒ String
Set the on-disk source file for this resource attr_reader :source_file
32 33 34 |
# File 'lib/middleman-core/sitemap/resource.rb', line 32 def source_file @source_file || get_source_file end |
#template? ⇒ Boolean
Whether this resource has a template file
52 53 54 55 |
# File 'lib/middleman-core/sitemap/resource.rb', line 52 def template? return false if source_file.nil? !::Tilt[source_file].nil? end |
#url ⇒ String
A path without the directory index - so foo/index.html becomes just foo. Best for linking.
134 135 136 137 138 139 140 141 |
# File 'lib/middleman-core/sitemap/resource.rb', line 134 def url url_path = destination_path if app.strip_index_file url_path = url_path.sub(/(^|\/)#{Regexp.escape(app.index_file)}$/, app.trailing_slash ? '/' : '') end File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', url_path) end |