Class: Middleman::Sitemap::Store
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Store
- Defined in:
- lib/middleman-core/sitemap/store.rb
Overview
The Store class
The Store manages a collection of Resource objects, which represent individual items in the sitemap. Resources are indexed by “source path”, which is the path relative to the source directory, minus any template extensions. All “path” parameters used in this class are source paths.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#extensionless_path(file) ⇒ String
Get a path without templating extensions.
-
#file_to_path(file) ⇒ String
Get the URL path for an on-disk file.
-
#find_resource_by_destination_path(request_path) ⇒ Middleman::Sitemap::Resource
Find a resource given its destination path.
-
#find_resource_by_path(request_path) ⇒ Middleman::Sitemap::Resource
Find a resource given its original path.
-
#initialize(app) ⇒ Store
constructor
Initialize with parent app.
-
#metadata_for_file(source_file) ⇒ Hash
Get the metadata for a specific file.
-
#metadata_for_path(request_path) ⇒ Hash
Get the metadata for a specific URL.
-
#provides_metadata(matcher = nil, &block) ⇒ Array<Array<Proc, Regexp>>
Register a handler to provide metadata on a file path.
-
#provides_metadata_for_path(matcher = nil, origin = nil, &block) ⇒ Array<Array<Proc, Regexp>>
Register a handler to provide metadata on a url path.
-
#rebuild_resource_list!(reason = nil) ⇒ void
Rebuild the list of resources from scratch, using registed manipulators.
-
#register_resource_list_manipulator(name, inst, immediately_rebuild = true) ⇒ void
Register a klass which can manipulate the main site map list.
-
#resources(include_ignored = false) ⇒ Array<Middleman::Sitemap::Resource>
Get the array of all resources.
Constructor Details
#initialize(app) ⇒ Store
Initialize with parent app
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/middleman-core/sitemap/store.rb', line 22 def initialize(app) @app = app @resources = [] @_cached_metadata = {} @_lookup_cache = { :path => {}, :destination_path => {} } @resource_list_manipulators = [] # Register classes which can manipulate the main site map list register_resource_list_manipulator(:on_disk, Middleman::Sitemap::Extensions::OnDisk.new(self), false) # Proxies register_resource_list_manipulator(:proxies, @app.proxy_manager, false) end |
Instance Attribute Details
#app ⇒ Middleman::Application
18 19 20 |
# File 'lib/middleman-core/sitemap/store.rb', line 18 def app @app end |
Instance Method Details
#extensionless_path(file) ⇒ String
Get a path without templating extensions
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/middleman-core/sitemap/store.rb', line 191 def extensionless_path(file) path = file.dup end_of_the_line = false while !end_of_the_line if !::Tilt[path].nil? path = path.sub(File.extname(path), "") else end_of_the_line = true end end # If there is no extension, look for one if File.extname(path).empty? input_ext = File.extname(file) if !input_ext.empty? input_ext = input_ext.split(".").last.to_sym if @app.template_extensions.has_key?(input_ext) path << ".#{@app.template_extensions[input_ext]}" end end end path end |
#file_to_path(file) ⇒ String
Get the URL path for an on-disk file
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/middleman-core/sitemap/store.rb', line 172 def file_to_path(file) file = File.(file, @app.root) prefix = @app.source_dir.sub(/\/$/, "") + "/" return false unless file.start_with?(prefix) path = file.sub(prefix, "") # Replace a file name containing automatic_directory_matcher with a folder unless @app.automatic_directory_matcher.nil? path = path.gsub(@app.automatic_directory_matcher, "/") end extensionless_path(path) end |
#find_resource_by_destination_path(request_path) ⇒ Middleman::Sitemap::Resource
Find a resource given its destination path
74 75 76 77 |
# File 'lib/middleman-core/sitemap/store.rb', line 74 def find_resource_by_destination_path(request_path) request_path = ::Middleman::Util.normalize_path(request_path) @_lookup_cache[:destination_path][request_path] end |
#find_resource_by_path(request_path) ⇒ Middleman::Sitemap::Resource
Find a resource given its original path
66 67 68 69 |
# File 'lib/middleman-core/sitemap/store.rb', line 66 def find_resource_by_path(request_path) request_path = ::Middleman::Util.normalize_path(request_path) @_lookup_cache[:path][request_path] end |
#metadata_for_file(source_file) ⇒ Hash
Get the metadata for a specific file
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/middleman-core/sitemap/store.rb', line 102 def (source_file) = { :options => {}, :locals => {}, :page => {}, :blocks => [] } .inject() do |result, (callback, matcher)| next result if !matcher.nil? && !source_file.match(matcher) = callback.call(source_file) if .has_key?(:blocks) result[:blocks] << [:blocks] .delete(:blocks) end result.deep_merge() end end |
#metadata_for_path(request_path) ⇒ Hash
Get the metadata for a specific URL
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/middleman-core/sitemap/store.rb', line 145 def (request_path) return @_cached_metadata[request_path] if @_cached_metadata[request_path] = { :options => {}, :locals => {}, :page => {}, :blocks => [] } @_cached_metadata[request_path] = .inject() do |result, (callback, matcher)| case matcher when Regexp next result unless request_path.match(matcher) when String next result unless File.fnmatch("/" + matcher.sub(%r{^/}, ''), "/#{request_path}") end = callback.call(request_path) if .has_key?(:blocks) result[:blocks] << [:blocks] .delete(:blocks) end result.deep_merge() end end |
#provides_metadata(matcher = nil, &block) ⇒ Array<Array<Proc, Regexp>>
Register a handler to provide metadata on a file path
93 94 95 96 97 |
# File 'lib/middleman-core/sitemap/store.rb', line 93 def (matcher=nil, &block) @_provides_metadata ||= [] @_provides_metadata << [block, matcher] if block_given? @_provides_metadata end |
#provides_metadata_for_path(matcher = nil, origin = nil, &block) ⇒ Array<Array<Proc, Regexp>>
Register a handler to provide metadata on a url path
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/middleman-core/sitemap/store.rb', line 124 def (matcher=nil, origin=nil, &block) @_provides_metadata_for_path ||= [] if block_given? if origin existing_provider = @_provides_metadata_for_path.find {|b,m,o| o == origin && m == matcher} end if existing_provider existing_provider[0] = block else @_provides_metadata_for_path << [block, matcher, origin] end @_cached_metadata = {} end @_provides_metadata_for_path end |
#rebuild_resource_list!(reason = nil) ⇒ void
This method returns an undefined value.
Rebuild the list of resources from scratch, using registed manipulators
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/middleman-core/sitemap/store.rb', line 48 def rebuild_resource_list!(reason=nil) @resources = @resource_list_manipulators.inject([]) do |result, (_, inst)| newres = inst.manipulate_resource_list(result) # Reset lookup cache @_lookup_cache = { :path => {}, :destination_path => {} } newres.each do |resource| @_lookup_cache[:path][resource.path] = resource @_lookup_cache[:destination_path][resource.destination_path] = resource end newres end end |
#register_resource_list_manipulator(name, inst, immediately_rebuild = true) ⇒ void
This method returns an undefined value.
Register a klass which can manipulate the main site map list
41 42 43 44 |
# File 'lib/middleman-core/sitemap/store.rb', line 41 def register_resource_list_manipulator(name, inst, immediately_rebuild=true) @resource_list_manipulators << [name, inst] rebuild_resource_list!(:registered_new) if immediately_rebuild end |
#resources(include_ignored = false) ⇒ Array<Middleman::Sitemap::Resource>
Get the array of all resources
82 83 84 85 86 87 88 |
# File 'lib/middleman-core/sitemap/store.rb', line 82 def resources(include_ignored=false) if include_ignored @resources else @resources.reject(&:ignored?) end end |