Class: Middleman::Sitemap::Resource

Inherits:
Object
  • Object
show all
Includes:
Contracts, Extensions::Traversal
Defined in:
middleman-core/lib/middleman-core/sitemap/resource.rb

Overview

Sitemap Resource class

Direct Known Subclasses

Extensions::EndpointResource, Extensions::RedirectResource, ProxyResource

Constant Summary

METADATA_HASH =
({ options: Maybe[Hash], locals: Maybe[Hash], page: Maybe[Hash] })

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Extensions::Traversal

#children, #directory_index?, #eponymous_directory?, #eponymous_directory_path, #parent, #siblings

Methods included from Contracts

#Contract

Constructor Details

- (Resource) initialize(store, path, source_file = nil)

Initialize resource with parent store and URL

Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 44

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

  # 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

- (String) destination_path Also known as: request_path

The output path in the build directory for this resource

Returns:

  • (String)


22
23
24
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 22

def destination_path
  @destination_path
end

- (Object) metadata (readonly)

Returns the value of attribute metadata



38
39
40
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 38

def 
  @metadata
end

- (String) path (readonly)

The source path of this resource (relative to the source directory, without template extensions)

Returns:

  • (String)


18
19
20
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 18

def path
  @path
end

- (String) source_file (readonly)

The on-disk source file for this resource, if there is one

Returns:

  • (String)


26
27
28
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 26

def source_file
  @source_file
end

Instance Method Details

- (Object) add_metadata(meta = {})



73
74
75
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 73

def (meta={})
  @metadata.deep_merge!(meta)
end

- (Boolean) binary?

Returns:

  • (Boolean)


148
149
150
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 148

def binary?
  !source_file.nil? && ::Middleman::Util.binary?(source_file)
end

- (Object) content_type



174
175
176
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 174

def content_type
  options[:content_type] || ::Rack::Mime.mime_type(ext, nil)
end

- (Object) data



80
81
82
83
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 80

def data
  # TODO: Should this really be a HashWithIndifferentAccess?
  ::Middleman::Util.recursively_enhance([:page]).freeze
end

- (Object) ext



103
104
105
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 103

def ext
  File.extname(path)
end

- (String) Hash

Render this resource

Returns:

  • (String)


109
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 109

Contract Hash, Hash => String

- (Object) ignore!



156
157
158
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 156

def ignore!
  @ignored = true
end

- (Boolean) ignored?

Returns:

  • (Boolean)


163
164
165
166
167
168
169
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 163

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)
  !self.is_a?(ProxyResource) && @app.sitemap.ignored?(source_file.sub("#{@app.source_dir}/", ''))
end

- (Object) locals



96
97
98
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 96

def locals
  [:locals]
end

- (Object) options



89
90
91
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 89

def options
  [:options]
end

- (Object) render(opts = {}, locs = {})



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 110

def render(opts={}, locs={})
  return ::Middleman::FileRenderer.new(@app, source_file).template_data_for_file unless template?

  relative_source = Pathname(source_file).relative_path_from(Pathname(@app.root))

  ::Middleman::Util.instrument 'render.resource', path: relative_source, 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
    unless opts.key?(:layout)
      opts[:layout] = false if %w(.js .json .css .txt).include?(ext)
    end

    renderer = ::Middleman::TemplateRenderer.new(@app, source_file)
    renderer.render(locs, opts)
  end
end

- (Boolean) template?

Returns:

  • (Boolean)


61
62
63
64
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 61

def template?
  return false if source_file.nil?
  !::Tilt[source_file].nil?
end

- (Object) url



135
136
137
138
139
140
141
142
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 135

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