class Middleman::Sitemap::Resource
Attributes
@return [Middleman::Application]
The source path of this resource (relative to the source directory, without template extensions) @return [String]
@return [Middleman::Sitemap::Store]
Public Class Methods
Initialize resource with parent store and URL @param [Middleman::Sitemap::Store] store @param [String] path @param [String] source_file
# File lib/middleman-core/sitemap/resource.rb, line 36 def initialize(store, path, source_file=nil) @store = store @app = @store.app @path = path @source_file = source_file @destination_paths = [@path] @local_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] } end
Public Instance Methods
Merge in new metadata specific to this resource. @param [Hash] metadata A metadata block like provides_metadata_for_path takes
# File lib/middleman-core/sitemap/resource.rb, line 78 def add_metadata(metadata={}, &block) if metadata.has_key?(:blocks) @local_metadata[:blocks] << metadata.delete(:blocks) end @local_metadata.deep_merge!(metadata) @local_metadata[:blocks] << block if block_given? end
Whether the source file is binary.
@retrun [Boolean]
# File lib/middleman-core/sitemap/resource.rb, line 147 def binary? ::Middleman::Util.binary?(source_file) end
Get the output/preview URL for this resource @return [String]
# File lib/middleman-core/sitemap/resource.rb, line 88 def destination_path @destination_paths.last end
Set the output/preview URL for this resource @param [String] path @return [void]
# File lib/middleman-core/sitemap/resource.rb, line 95 def destination_path=(path) @destination_paths << path end
Extension of the path (i.e. ‘.js’) @return [String]
# File lib/middleman-core/sitemap/resource.rb, line 101 def ext File.extname(path) end
Get the metadata for both the current source_file
and the current path @return [Hash]
# File lib/middleman-core/sitemap/resource.rb, line 56 def metadata result = store.metadata_for_path(path).dup file_meta = store.metadata_for_file(source_file).dup if file_meta.has_key?(:blocks) result[:blocks] << file_meta.delete(:blocks) end result.deep_merge!(file_meta) local_meta = @local_metadata.dup if local_meta.has_key?(:blocks) result[:blocks] << local_meta.delete(:blocks) end result.deep_merge!(local_meta) result[:blocks] = result[:blocks].flatten.compact result end
Render this resource @return [String]
# File lib/middleman-core/sitemap/resource.rb, line 107 def render(opts={}, locs={}, &block) if !template? return app.template_data_for_file(source_file) end relative_source = Pathname(source_file).relative_path_from(Pathname(app.root)) instrument "render.resource", :path => relative_source do md = metadata.dup opts = md[:options].deep_merge(opts) locs = md[:locals].deep_merge(locs) # Forward remaining data to helpers if md.has_key?(:page) app.data.store("page", md[:page]) end blocks = md[:blocks].dup rescue [] blocks << block if block_given? app.current_path ||= self.destination_path app.render_template(source_file, locs, opts, blocks) end end
Set the on-disk source file for this resource @return [String] attr_reader :source_file
# File lib/middleman-core/sitemap/resource.rb, line 28 def source_file @source_file || get_source_file end
Whether this resource has a template file @return [Boolean]
# File lib/middleman-core/sitemap/resource.rb, line 49 def template? return false if source_file.nil? !::Tilt[source_file].nil? end
A path without the directory index - so foo/index.html becomes just foo. Best for linking. @return [String]
# File lib/middleman-core/sitemap/resource.rb, line 135 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