automatically wire up sitemap manipulators, let extensions control their name, add self-registration method

This commit is contained in:
Thomas Reynolds 2013-04-20 13:52:44 -07:00
parent 10e1fd92d6
commit 65c8dda565
5 changed files with 66 additions and 87 deletions

View file

@ -106,6 +106,7 @@ module Middleman
class Extension class Extension
class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false class_attribute :supports_multiple_instances, :instance_reader => false, :instance_writer => false
class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false class_attribute :defined_helpers, :instance_reader => false, :instance_writer => false
class_attribute :ext_name, :instance_reader => false, :instance_writer => false
class << self class << self
def config def config
@ -123,6 +124,14 @@ module Middleman
m.module_eval(&block) m.module_eval(&block)
self.defined_helpers << m self.defined_helpers << m
end end
def extension_name
self.ext_name || self.name.underscore.split("/").last.to_sym
end
def register(n=self.extension_name)
::Middleman::Extensions.register(n, self)
end
end end
attr_accessor :app, :options attr_accessor :app, :options
@ -148,11 +157,15 @@ module Middleman
end end
end end
klass.after_configuration(&method(:after_configuration)) klass.after_configuration do
if ext.respond_to?(:manipulate_resource_list)
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
end end
def after_configuration if ext.respond_to?(:after_configuration)
nil ext.after_configuration
end
end
end end
end end
end end

View file

@ -81,24 +81,18 @@ module Middleman
# AssetHash appends a hash of the file contents to the assets filename # AssetHash appends a hash of the file contents to the assets filename
# to avoid browser caches failing to update to your new content. # to avoid browser caches failing to update to your new content.
Middleman::Extensions.register(:asset_hash) do
require "middleman-more/extensions/asset_hash" require "middleman-more/extensions/asset_hash"
Middleman::Extensions::AssetHash Middleman::Extensions::AssetHash.register
end
# AssetHost allows you to setup multiple domains to host your static # AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate # assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load. # through each of the asset servers to better spread the load.
Middleman::Extensions.register(:asset_host) do
require "middleman-more/extensions/asset_host" require "middleman-more/extensions/asset_host"
Middleman::Extensions::AssetHost Middleman::Extensions::AssetHost.register
end
# Provide Apache-style index.html files for directories # Provide Apache-style index.html files for directories
Middleman::Extensions.register(:directory_indexes) do
require "middleman-more/extensions/directory_indexes" require "middleman-more/extensions/directory_indexes"
Middleman::Extensions::DirectoryIndexes Middleman::Extensions::DirectoryIndexes.register
end
# Lorem provides a handful of helpful prototyping methods to generate # Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses. # words, paragraphs, fake images, names and email addresses.
@ -108,10 +102,8 @@ module Middleman
# AutomaticImageSizes inspects the images used in your dynamic templates # AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML # and automatically adds width and height attributes to their HTML
# elements. # elements.
Middleman::Extensions.register(:automatic_image_sizes) do
require "middleman-more/extensions/automatic_image_sizes" require "middleman-more/extensions/automatic_image_sizes"
Middleman::Extensions::AutomaticImageSizes Middleman::Extensions::AutomaticImageSizes.register
end
end end
end end
end end

View file

@ -16,8 +16,6 @@ module Middleman
# Allow specifying regexes to ignore, plus always ignore apple touch icons # Allow specifying regexes to ignore, plus always ignore apple touch icons
@ignore = Array(options.ignore) + [/^apple-touch-icon/] @ignore = Array(options.ignore) + [/^apple-touch-icon/]
app.sitemap.register_resource_list_manipulator(:asset_hash, self)
app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => @ignore app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => @ignore
end end

View file

@ -10,6 +10,7 @@ module Middleman
# Include 3rd-party fastimage library # Include 3rd-party fastimage library
require "middleman-more/extensions/automatic_image_sizes/fastimage" require "middleman-more/extensions/automatic_image_sizes/fastimage"
end
helpers do helpers do
# Override default image_tag helper to automatically calculate and include # Override default image_tag helper to automatically calculate and include
@ -45,4 +46,3 @@ module Middleman
end end
end end
end end
end

View file

@ -3,34 +3,11 @@ module Middleman
module Extensions module Extensions
# Directory Indexes extension # Directory Indexes extension
module DirectoryIndexes class DirectoryIndexes < ::Middleman::Extension
# Setup extension
class << self
# Once registered
def registered(app)
app.after_configuration do
sitemap.register_resource_list_manipulator(
:directory_indexes,
DirectoryIndexManager.new(self)
)
end
end
alias :included :registered
end
# Central class for managing the directory indexes extension
class DirectoryIndexManager
def initialize(app)
@app = app
end
# 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)
index_file = @app.index_file index_file = app.index_file
new_index_path = "/#{index_file}" new_index_path = "/#{index_file}"
resources.each do |resource| resources.each do |resource|
@ -51,4 +28,3 @@ module Middleman
end end
end end
end end
end