From ffecc3e4cc5c3a40f14c6d07a97cd053c2b910ef Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Wed, 11 Jul 2012 15:14:08 +0930 Subject: [PATCH 1/2] Optimisation, Hash#delete returns the deleted item --- middleman-core/lib/middleman-core/sitemap/resource.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 44c3bc9b..7d6975d3 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -57,15 +57,13 @@ module Middleman path_meta = store.metadata_for_path(path).dup if path_meta.has_key?(:blocks) - result[:blocks] << path_meta[:blocks] - path_meta.delete(:blocks) + result[:blocks] << path_meta.delete(:blocks) end result.deep_merge!(path_meta) local_meta = @local_metadata.dup if local_meta.has_key?(:blocks) - result[:blocks] << local_meta[:blocks] - local_meta.delete(:blocks) + result[:blocks] << local_meta.delete(:blocks) end result.deep_merge!(local_meta) @@ -78,8 +76,7 @@ module Middleman # @param [Hash] metadata A metadata block like provides_metadata_for_path takes def add_metadata(metadata={}, &block) if metadata.has_key?(:blocks) - @local_metadata[:blocks] << metadata[:blocks] - metadata.delete(:blocks) + @local_metadata[:blocks] << metadata.delete(:blocks) end @local_metadata.deep_merge!(metadata) @local_metadata[:blocks] << block if block_given? From d30f883e488b3b92c6eda47b620e22c5aa49eb2d Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Wed, 11 Jul 2012 15:16:18 +0930 Subject: [PATCH 2/2] Regenerate @_lookup_cache after each manipulation Trying to call Resource#metadata from within MyExtension#manipulate_resource_list generates an exception in Proxies::ResourceInstanceMethods#get_source_file because @_lookup_cache is empty. Moving the recalculation inside the loop means regenerating the cache after each manipulation but allows extensions to examine the page metadata when manipulating proxied resources. --- .../lib/middleman-core/sitemap/store.rb | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index a72f9512..1b5f90a7 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -47,14 +47,16 @@ module Middleman # @return [void] def rebuild_resource_list!(reason=nil) @resources = @resource_list_manipulators.inject([]) do |result, (_, inst)| - inst.manipulate_resource_list(result) - end - - # Reset lookup cache - @_lookup_cache = { :path => {}, :destination_path => {} } - @resources.each do |resource| - @_lookup_cache[:path][resource.path] = resource - @_lookup_cache[:destination_path][resource.destination_path] = resource + 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 @@ -214,4 +216,4 @@ module Middleman end end end -end \ No newline at end of file +end