Class: Middleman::Sitemap::Resource

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

Overview

Sitemap Resource class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::ContentType

#content_type

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

Parameters:



38
39
40
41
42
43
44
45
46
47
# File 'lib/middleman-core/sitemap/resource.rb', line 38

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_paths = [@path]

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

Instance Attribute Details

#appMiddleman::Application (readonly)



15
16
17
# File 'lib/middleman-core/sitemap/resource.rb', line 15

def app
  @app
end

#pathString (readonly)

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

Returns:



24
25
26
# File 'lib/middleman-core/sitemap/resource.rb', line 24

def path
  @path
end

#storeMiddleman::Sitemap::Store (readonly)



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

def store
  @store
end

Instance Method Details

#add_metadata(metadata = {}, &block) ⇒ Object

Merge in new metadata specific to this resource.

Parameters:

  • metadata (Hash) (defaults to: {})

    A metadata block like provides_metadata_for_path takes



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

def (={}, &block)
   = .dup
  if .has_key?(:blocks)
    @local_metadata[:blocks] += .delete(:blocks)
  end
  @local_metadata.deep_merge!()
  @local_metadata[:blocks] += [ block ] if block_given?
end

#binary?Boolean

Whether the source file is binary.

Returns:

  • (Boolean)


168
169
170
# File 'lib/middleman-core/sitemap/resource.rb', line 168

def binary?
  ::Middleman::Util.binary?(source_file)
end

#destination_pathString

Get the output/preview URL for this resource

Returns:



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

def destination_path
  @destination_paths.last
end

#destination_path=(path) ⇒ void

This method returns an undefined value.

Set the output/preview URL for this resource

Parameters:



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

def destination_path=(path)
  @destination_paths << path
end

#extString

Extension of the path (i.e. ‘.js’)

Returns:



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

def ext
  File.extname(path)
end

#metadataHash

Get the metadata for both the current source_file and the current path

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/middleman-core/sitemap/resource.rb', line 58

def 
  result = store.(path).dup

  file_meta = store.(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(opts = {}, locs = {}, &block) ⇒ String

Render this resource

Returns:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/middleman-core/sitemap/resource.rb', line 113

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   = .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
    if md.has_key?(:page)
      app.data.store("page", md[:page])
    end

    blocks = Array(md[:blocks]).dup
    blocks << block if block_given?

    app.current_path ||= self.destination_path

    # Certain output file types don't use layouts
    if !opts.has_key?(:layout)
      opts[:layout] = false if %w(.js .json .css .txt).include?(self.ext)
    end

    app.render_template(source_file, locs, opts, blocks)
  end
end

#request_pathObject



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

def request_path
  self.destination_path
end

#source_fileString

Set the on-disk source file for this resource attr_reader :source_file

Returns:



30
31
32
# File 'lib/middleman-core/sitemap/resource.rb', line 30

def source_file
  @source_file || get_source_file
end

#template?Boolean

Whether this resource has a template file

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/middleman-core/sitemap/resource.rb', line 51

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

#urlString

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

Returns:



156
157
158
159
160
161
162
163
# File 'lib/middleman-core/sitemap/resource.rb', line 156

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