Class: Middleman::Sitemap::Resource
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Resource
- Includes:
- Extensions::Traversal
- Defined in:
- middleman-core/lib/middleman-core/sitemap/resource.rb
Overview
Sitemap Resource class
Instance Attribute Summary (collapse)
- - (Middleman::Application) app readonly
-
- (String) path
readonly
The source path of this resource (relative to the source directory, without template extensions).
- - (Middleman::Sitemap::Store) store readonly
Instance Method Summary (collapse)
-
- (Object) add_metadata(metadata = {}, &block)
Merge in new metadata specific to this resource.
-
- (String) destination_path
Get the output/preview URL for this resource.
-
- (void) destination_path=(path)
Set the output/preview URL for this resource.
-
- (String) ext
Extension of the path (i.e. '.js').
-
- (Resource) initialize(store, path, source_file = nil)
constructor
Initialize resource with parent store and URL.
-
- (Hash) metadata
Get the metadata for both the current source_file and the current path.
-
- (String) mime_type
Mime type of the path.
-
- (String) render(opts = {}, locs = {}, &block)
Render this resource.
-
- (String) source_file
Set the on-disk source file for this resource attr_reader :source_file.
-
- (Boolean) template?
Whether this resource has a template file.
-
- (String) url
A path without the directory index - so foo/index.html becomes just foo.
Methods included from Extensions::Traversal
#children, #directory_index?, #eponymous_directory?, #eponymous_directory_path, #parent, #siblings
Constructor Details
- (Resource) initialize(store, path, source_file = nil)
Initialize resource with parent store and URL
35 36 37 38 39 40 41 42 43 44 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 35 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 |
Instance Attribute Details
- (Middleman::Application) app (readonly)
13 14 15 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 13 def app @app end |
- (String) path (readonly)
The source path of this resource (relative to the source directory, without template extensions)
21 22 23 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 21 def path @path end |
- (Middleman::Sitemap::Store) store (readonly)
16 17 18 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 16 def store @store end |
Instance Method Details
- (Object) add_metadata(metadata = {}, &block)
Merge in new metadata specific to this resource.
77 78 79 80 81 82 83 84 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 77 def (={}, &block) if .has_key?(:blocks) @local_metadata[:blocks] << [:blocks] .delete(:blocks) end @local_metadata.deep_merge!() @local_metadata[:blocks] << block if block_given? end |
- (String) destination_path
Get the output/preview URL for this resource
88 89 90 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 88 def destination_path @destination_paths.last end |
- (void) destination_path=(path)
This method returns an undefined value.
Set the output/preview URL for this resource
95 96 97 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 95 def destination_path=(path) @destination_paths << path end |
- (String) ext
Extension of the path (i.e. '.js')
101 102 103 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 101 def ext File.extname(path) end |
- (Hash) metadata
Get the metadata for both the current source_file and the current path
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 55 def result = store.(source_file).dup = store.(path).dup if .has_key?(:blocks) result[:blocks] << [:blocks] .delete(:blocks) end result.deep_merge!() = @local_metadata.dup if .has_key?(:blocks) result[:blocks] << [:blocks] .delete(:blocks) end result.deep_merge!() result end |
- (String) mime_type
Mime type of the path
107 108 109 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 107 def mime_type app.mime_type ext end |
- (String) render(opts = {}, locs = {}, &block)
Render this resource
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 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 113 def render(opts={}, locs={}, &block) return File.open(source_file).read unless template? start_time = Time.now puts "== Render Start: #{source_file}" if app.logging? md = .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 md[:blocks].flatten.compact.each do |block| app.instance_eval(&block) end app.instance_eval(&block) if block_given? result = app.render_template(source_file, locs, opts) puts "== Render End: #{source_file} (#{(Time.now - start_time).round(2)}s)" if app.logging? result end |
- (String) source_file
Set the on-disk source file for this resource attr_reader :source_file
27 28 29 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 27 def source_file @source_file || get_source_file end |
- (Boolean) template?
Whether this resource has a template file
48 49 50 51 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 48 def template? return false if source_file.nil? !::Tilt[source_file].nil? end |
- (String) url
A path without the directory index - so foo/index.html becomes just foo. Best for linking.
142 143 144 |
# File 'middleman-core/lib/middleman-core/sitemap/resource.rb', line 142 def url '/' + destination_path.sub(/#{Regexp.escape(app.index_file)}$/, '') end |