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

Constant Summary

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

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

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

#initialize(store, path, source_file = nil) ⇒ Resource



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 46

def initialize(store, path, source_file=nil)
  @store       = store
  @app         = @store.app
  @path        = path

  if source_file && source_file.is_a?(String)
    source_file = Pathname(source_file)
  end

  if source_file && source_file.is_a?(Pathname)
    @source_file = ::Middleman::SourceFile.new(source_file.relative_path_from(@app.source_dir), source_file, @app.source_dir, Set.new([:source]))
  else
    @source_file = source_file
  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_pathString Also known as: request_path

The output path in the build directory for this resource



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

def destination_path
  @destination_path
end

#metadataObject (readonly)

Returns the value of attribute metadata



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

def 
  @metadata
end

#pathString (readonly)

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



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

def path
  @path
end

#source_fileObject (readonly)

Returns the value of attribute source_file



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

def source_file
  @source_file
end

Instance Method Details

#add_metadata(meta = {}) ⇒ Object



85
86
87
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 85

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

#Any

This method returns an undefined value.

Ignore a resource directly, without going through the whole ignore filter stuff.



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

Contract Any

#binary?Boolean



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

def binary?
  !source_file.nil? && ::Middleman::Util.binary?(source_file[:full_path].to_s)
end

#BoolBoolean

Whether the Resource is ignored



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

Contract Bool

#content_typeObject



187
188
189
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 187

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

#dataObject



92
93
94
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 92

def data
  ::Middleman::Util.recursively_enhance([:page])
end

#extObject



114
115
116
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 114

def ext
  File.extname(path)
end

#HashString

Render this resource



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

Contract Hash

#ignore!Object



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

def ignore!
  @ignored = true
end

#ignored?Boolean



172
173
174
175
176
177
178
179
180
181
182
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 172

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) && source_file && @app.sitemap.ignored?(source_file[:relative_path].to_s)
    true
  else
    false
  end
end

#localsObject



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

def locals
  [:locals]
end

#METADATA_HASHHash

The metadata for this resource



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

Contract METADATA_HASH

#optionsObject



100
101
102
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 100

def options
  [:options]
end

#render(opts = {}, locs = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 121

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

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

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

#StringString

A path without the directory index - so foo/index.html becomes just foo. Best for linking.



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

Contract String

#template?Boolean



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

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

#to_sObject Also known as: inspect



191
192
193
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 191

def to_s
  "#<Middleman::Sitemap::Resource path=#{@path}>"
end

#urlObject



144
145
146
147
148
149
150
151
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 144

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