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_attribute :supports_multiple_instances, :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
def config
@ -123,6 +124,14 @@ module Middleman
m.module_eval(&block)
self.defined_helpers << m
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
attr_accessor :app, :options
@ -148,11 +157,15 @@ module Middleman
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
def after_configuration
nil
if ext.respond_to?(:after_configuration)
ext.after_configuration
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
# to avoid browser caches failing to update to your new content.
Middleman::Extensions.register(:asset_hash) do
require "middleman-more/extensions/asset_hash"
Middleman::Extensions::AssetHash
end
Middleman::Extensions::AssetHash.register
# AssetHost allows you to setup multiple domains to host your static
# assets. Calls to asset paths in dynamic templates will then rotate
# through each of the asset servers to better spread the load.
Middleman::Extensions.register(:asset_host) do
require "middleman-more/extensions/asset_host"
Middleman::Extensions::AssetHost
end
Middleman::Extensions::AssetHost.register
# Provide Apache-style index.html files for directories
Middleman::Extensions.register(:directory_indexes) do
require "middleman-more/extensions/directory_indexes"
Middleman::Extensions::DirectoryIndexes
end
Middleman::Extensions::DirectoryIndexes.register
# Lorem provides a handful of helpful prototyping methods to generate
# words, paragraphs, fake images, names and email addresses.
@ -108,10 +102,8 @@ module Middleman
# AutomaticImageSizes inspects the images used in your dynamic templates
# and automatically adds width and height attributes to their HTML
# elements.
Middleman::Extensions.register(:automatic_image_sizes) do
require "middleman-more/extensions/automatic_image_sizes"
Middleman::Extensions::AutomaticImageSizes
end
Middleman::Extensions::AutomaticImageSizes.register
end
end
end

View file

@ -16,8 +16,6 @@ module Middleman
# Allow specifying regexes to ignore, plus always ignore apple touch icons
@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
end

View file

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

View file

@ -3,34 +3,11 @@ module Middleman
module Extensions
# Directory Indexes extension
module DirectoryIndexes
# 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
class DirectoryIndexes < ::Middleman::Extension
# Update the main sitemap resource list
# @return [void]
def manipulate_resource_list(resources)
index_file = @app.index_file
index_file = app.index_file
new_index_path = "/#{index_file}"
resources.each do |resource|
@ -51,4 +28,3 @@ module Middleman
end
end
end
end