class Middleman::Sitemap::Resource

Sitemap Resource class

Attributes

app[R]

@return [Middleman::Application]

destination_path[RW]

The output path for this resource @return [String]

path[R]

The source path of this resource (relative to the source directory, without template extensions) @return [String]

store[R]

@return [Middleman::Sitemap::Store]

Public Class Methods

new(store, path, source_file=nil) click to toggle source

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 40
def initialize(store, path, source_file=nil)
  @store       = store
  @app         = @store.app
  @path        = path.gsub(' ', '%20') # handle spaces in filenames
  @source_file = source_file
  @destination_path = @path

  @local_metadata = { options: {}, locals: {}, page: {}, blocks: [] }
end

Public Instance Methods

add_metadata(metadata={}, &block) click to toggle source

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 76
def add_metadata(metadata={}, &block)
  metadata = metadata.dup
  @local_metadata[:blocks] += metadata.delete(:blocks) if metadata.key?(:blocks)
  @local_metadata.deep_merge!(metadata)
  @local_metadata[:blocks] += [block] if block_given?
end
binary?() click to toggle source

Whether the source file is binary.

@return [Boolean]

# File lib/middleman-core/sitemap/resource.rb, line 150
def binary?
  ::Middleman::Util.binary?(source_file)
end
ext() click to toggle source

Extension of the path (i.e. ‘.js’) @return [String]

# File lib/middleman-core/sitemap/resource.rb, line 89
def ext
  File.extname(path)
end
metadata() click to toggle source

Get the metadata for both the current source_file and the current path @return [Hash]

# File lib/middleman-core/sitemap/resource.rb, line 59
def metadata
  result = store.metadata_for_path(path).dup

  file_meta = store.metadata_for_file(source_file).dup
  result[:blocks] += file_meta.delete(:blocks) if file_meta.key?(:blocks)
  result.deep_merge!(file_meta)

  local_meta = @local_metadata.dup
  result[:blocks] += local_meta.delete(:blocks) if local_meta.key?(:blocks)
  result.deep_merge!(local_meta)

  result[:blocks] = result[:blocks].flatten.compact
  result
end
render(opts={}, locs={}, &block) click to toggle source

Render this resource @return [String]

# File lib/middleman-core/sitemap/resource.rb, line 99
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   = metadata.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() click to toggle source
# File lib/middleman-core/sitemap/resource.rb, line 93
def request_path
  destination_path
end
source_file() click to toggle source

Set the on-disk source file for this resource @return [String] attr_reader :source_file

# File lib/middleman-core/sitemap/resource.rb, line 32
def source_file
  @source_file || get_source_file
end
template?() click to toggle source

Whether this resource has a template file @return [Boolean]

# File lib/middleman-core/sitemap/resource.rb, line 52
def template?
  return false if source_file.nil?
  !::Tilt[source_file].nil?
end
url() click to toggle source

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 138
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