Class: Middleman::Sitemap::Store

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(app) ⇒ Store

Initialize with parent app



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/middleman-core/sitemap/store.rb', line 23

def initialize(app)
  @app   = app
  @resources = []
   = {}
  @resource_list_manipulators = []
  @needs_sitemap_rebuild = true
  @lock = Monitor.new

  reset_lookup_cache!

  # Register classes which can manipulate the main site map list
  register_resource_list_manipulator(:on_disk, Middleman::Sitemap::Extensions::OnDisk.new(self))

  # Proxies
  register_resource_list_manipulator(:proxies, @app.proxy_manager)
end

Instance Attribute Details

#appMiddleman::Application



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

def app
  @app
end

Instance Method Details

#ensure_resource_list_updated!Object

Actually update the resource list, assuming anything has called rebuild_resource_list! since the last time it was run. This is very expensive!



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/middleman-core/sitemap/store.rb', line 214

def ensure_resource_list_updated!
  @lock.synchronize do
    return unless @needs_sitemap_rebuild
    @needs_sitemap_rebuild = false

    @app.logger.debug "== Rebuilding resource list"

    @resources = @resource_list_manipulators.inject([]) do |result, (_, inst)|
      newres = inst.manipulate_resource_list(result)

      # Reset lookup cache
      reset_lookup_cache!
      newres.each do |resource|
        @_lookup_by_path[resource.path] = resource
        @_lookup_by_destination_path[resource.destination_path] = resource
      end

      newres
    end
  end
end

#extensionless_path(file) ⇒ String

Get a path without templating extensions



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/middleman-core/sitemap/store.rb', line 184

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/middleman-core/sitemap/store.rb', line 165

def file_to_path(file)
  file = File.expand_path(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



73
74
75
76
77
78
79
# File 'lib/middleman-core/sitemap/store.rb', line 73

def find_resource_by_destination_path(request_path)
  @lock.synchronize do
    request_path = ::Middleman::Util.normalize_path(request_path)
    ensure_resource_list_updated!
    @_lookup_by_destination_path[request_path]
  end
end

#find_resource_by_path(request_path) ⇒ Middleman::Sitemap::Resource

Find a resource given its original path



62
63
64
65
66
67
68
# File 'lib/middleman-core/sitemap/store.rb', line 62

def find_resource_by_path(request_path)
  @lock.synchronize do
    request_path = ::Middleman::Util.normalize_path(request_path)
    ensure_resource_list_updated!
    @_lookup_by_path[request_path]
  end
end

#metadata_for_file(source_file) ⇒ Hash

Get the metadata for a specific file



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/middleman-core/sitemap/store.rb', line 107

def (source_file)
   = { :options => {}, :locals => {}, :page => {}, :blocks => [] }

  .inject() do |result, (callback, matcher)|
    next result if matcher && !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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/middleman-core/sitemap/store.rb', line 141

def (request_path)
  return [request_path] if [request_path]

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

  [request_path] = .inject() do |result, (callback, matcher)|
    case matcher
    when Regexp
      next result unless request_path =~ matcher
    when String
      next result unless File.fnmatch("/" + Util.strip_leading_slash(matcher), "/#{request_path}")
    end

     = callback.call(request_path)

    result[:blocks] += Array(.delete(:blocks))

    result.deep_merge()
  end
end

#provides_metadata(matcher = nil, &block) ⇒ Array<Array<Proc, Regexp>>

Register a handler to provide metadata on a file path



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

def (matcher=nil, &block)
   ||= []
   << [block, matcher] if block_given?
  
end

#provides_metadata_for_path(matcher = nil, &block) ⇒ Array<Array<Proc, Regexp>>

Register a handler to provide metadata on a url path



129
130
131
132
133
134
135
136
# File 'lib/middleman-core/sitemap/store.rb', line 129

def (matcher=nil, &block)
   ||= []
  if block_given?
     << [block, matcher]
     = {}
  end
  
end

#rebuild_resource_list!(reason = nil) ⇒ void

This method returns an undefined value.

Rebuild the list of resources from scratch, using registed manipulators



53
54
55
56
57
# File 'lib/middleman-core/sitemap/store.rb', line 53

def rebuild_resource_list!(reason=nil)
  @lock.synchronize do
    @needs_sitemap_rebuild = true
  end
end

#register_resource_list_manipulator(name, inst, unused = true) ⇒ void

This method returns an undefined value.

Register a klass which can manipulate the main site map list. Best to register these in a before_configuration or after_configuration hook.



46
47
48
49
# File 'lib/middleman-core/sitemap/store.rb', line 46

def register_resource_list_manipulator(name, inst, unused=true)
  @resource_list_manipulators << [name, inst]
  rebuild_resource_list!(:registered_new)
end

#resources(include_ignored = false) ⇒ Array<Middleman::Sitemap::Resource>

Get the array of all resources



84
85
86
87
88
89
90
91
92
93
# File 'lib/middleman-core/sitemap/store.rb', line 84

def resources(include_ignored=false)
  @lock.synchronize do
    ensure_resource_list_updated!
    if include_ignored
      @resources
    else
      @resources.reject(&:ignored?)
    end
  end
end