mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Merge pull request #1279 from bhollis/extensions
Resource list manipulator priority
This commit is contained in:
commit
6238bb0716
3 changed files with 26 additions and 6 deletions
|
@ -81,6 +81,12 @@ module Middleman
|
||||||
# @return [Symbol] the name this extension is registered under. This is the symbol used to activate the extension.
|
# @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
|
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
|
class << self
|
||||||
# @api private
|
# @api private
|
||||||
# @return [Middleman::Configuration::ConfigurationManager] The defined options for this extension.
|
# @return [Middleman::Configuration::ConfigurationManager] The defined options for this extension.
|
||||||
|
@ -150,7 +156,7 @@ module Middleman
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def activated_extension(instance)
|
def activated_extension(instance)
|
||||||
name = instance.class.ext_name
|
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|
|
@_extension_activation_callbacks[name].each do |block|
|
||||||
block.arity == 1 ? block.call(instance) : block.call
|
block.arity == 1 ? block.call(instance) : block.call
|
||||||
end
|
end
|
||||||
|
@ -276,7 +282,7 @@ module Middleman
|
||||||
|
|
||||||
# rubocop:disable IfUnlessModifier
|
# rubocop:disable IfUnlessModifier
|
||||||
if ext.respond_to?(:manipulate_resource_list)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
# Directory Indexes extension
|
# Directory Indexes extension
|
||||||
class Middleman::Extensions::DirectoryIndexes < ::Middleman::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
|
# Update the main sitemap resource list
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def manipulate_resource_list(resources)
|
def manipulate_resource_list(resources)
|
||||||
|
|
|
@ -62,9 +62,19 @@ module Middleman
|
||||||
#
|
#
|
||||||
# @param [Symbol] name Name of the manipulator for debugging
|
# @param [Symbol] name Name of the manipulator for debugging
|
||||||
# @param [#manipulate_resource_list] manipulator Resource list manipulator
|
# @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]
|
# @return [void]
|
||||||
def register_resource_list_manipulator(name, manipulator, *)
|
def register_resource_list_manipulator(name, manipulator, priority=50)
|
||||||
@resource_list_manipulators << [name, manipulator]
|
# 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)
|
rebuild_resource_list!(:registered_new)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -212,8 +222,8 @@ module Middleman
|
||||||
|
|
||||||
@app.logger.debug '== Rebuilding resource list'
|
@app.logger.debug '== Rebuilding resource list'
|
||||||
|
|
||||||
@resources = @resource_list_manipulators.reduce([]) do |result, (_, inst)|
|
@resources = @resource_list_manipulators.reduce([]) do |result, (_, manipulator, _)|
|
||||||
newres = inst.manipulate_resource_list(result)
|
newres = manipulator.manipulate_resource_list(result)
|
||||||
|
|
||||||
# Reset lookup cache
|
# Reset lookup cache
|
||||||
reset_lookup_cache!
|
reset_lookup_cache!
|
||||||
|
|
Loading…
Reference in a new issue