Add the ability to set a priority order for sitemap resource list manipulators.

This allows us to do things like forcing :directory_indexes to always run last, alleviating the problem of the sitemap output differing depending on when you activate your extensions.
This commit is contained in:
Ben Hollis 2014-05-12 00:00:53 -07:00
parent 5c75d26c86
commit 6b10d9d428
3 changed files with 26 additions and 6 deletions

View file

@ -81,6 +81,12 @@ module Middleman
# @return [Symbol] the name this extension is registered under. This is the symbol used to activate the extension.
class_attribute :ext_name, instance_reader: false, instance_writer: false
# @!attribute resource_list_manipulator_priority
# @!scope class
# @return [Numeric] the priority for this extension's `manipulate_resource_list` method, if it has one.
# @see Middleman::Sitemap::Store#register_resource_list_manipulator
class_attribute :resource_list_manipulator_priority, instance_reader: false, instance_writer: false
class << self
# @api private
# @return [Middleman::Configuration::ConfigurationManager] The defined options for this extension.
@ -150,7 +156,7 @@ module Middleman
# @return [void]
def activated_extension(instance)
name = instance.class.ext_name
return unless @_extension_activation_callbacks && @_extension_activation_callbacks.has_key?(name)
return unless @_extension_activation_callbacks && @_extension_activation_callbacks.key?(name)
@_extension_activation_callbacks[name].each do |block|
block.arity == 1 ? block.call(instance) : block.call
end
@ -276,7 +282,7 @@ module Middleman
# rubocop:disable IfUnlessModifier
if ext.respond_to?(:manipulate_resource_list)
ext.app.sitemap.register_resource_list_manipulator(ext.class.ext_name, ext)
ext.app.sitemap.register_resource_list_manipulator(ext.class.ext_name, ext, ext.class.resource_list_manipulator_priority)
end
end
end

View file

@ -1,5 +1,9 @@
# Directory Indexes extension
class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension
# This should run after most other sitemap manipulators so that it
# gets a chance to modify any new resources that get added.
self.resource_list_manipulator_priority = 100
# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)

View file

@ -62,9 +62,19 @@ module Middleman
#
# @param [Symbol] name Name of the manipulator for debugging
# @param [#manipulate_resource_list] manipulator Resource list manipulator
# @param [Numeric] priority Sets the order of this resource list manipulator relative to the rest. By default this is 50, and manipulators run in the order they are registered, but if a priority is provided then this will run ahead of or behind other manipulators.
# @return [void]
def register_resource_list_manipulator(name, manipulator, *)
@resource_list_manipulators << [name, manipulator]
def register_resource_list_manipulator(name, manipulator, priority=50)
# The third argument used to be a boolean - handle those who still pass one
priority = 50 unless priority.is_a? Numeric
@resource_list_manipulators << [name, manipulator, priority]
# The index trick is used so that the sort is stable - manipulators with the same priority
# will always be ordered in the same order as they were registered.
n = 0
@resource_list_manipulators = @resource_list_manipulators.sort_by do |m|
n += 1
[m[2], n]
end
rebuild_resource_list!(:registered_new)
end
@ -212,8 +222,8 @@ module Middleman
@app.logger.debug '== Rebuilding resource list'
@resources = @resource_list_manipulators.reduce([]) do |result, (_, inst)|
newres = inst.manipulate_resource_list(result)
@resources = @resource_list_manipulators.reduce([]) do |result, (_, manipulator, _)|
newres = manipulator.manipulate_resource_list(result)
# Reset lookup cache
reset_lookup_cache!