diff --git a/middleman-core/lib/middleman-core/extensions.rb b/middleman-core/lib/middleman-core/extensions.rb index f59bf78f..9a29d035 100644 --- a/middleman-core/lib/middleman-core/extensions.rb +++ b/middleman-core/lib/middleman-core/extensions.rb @@ -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)) - end + 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 diff --git a/middleman-more/lib/middleman-more.rb b/middleman-more/lib/middleman-more.rb index df73efa9..4f01a2fe 100644 --- a/middleman-more/lib/middleman-more.rb +++ b/middleman-more/lib/middleman-more.rb @@ -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 + require "middleman-more/extensions/asset_hash" + 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 + require "middleman-more/extensions/asset_host" + 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 + require "middleman-more/extensions/directory_indexes" + 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 + require "middleman-more/extensions/automatic_image_sizes" + Middleman::Extensions::AutomaticImageSizes.register end end end diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index 93e86a06..69bd36fc 100644 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -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 diff --git a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb index fff6ac8d..32205bf7 100644 --- a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb +++ b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb @@ -10,37 +10,37 @@ 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 - # image dimensions. - # - # @param [String] path - # @param [Hash] params - # @return [String] - def image_tag(path, params={}) - if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://") - params[:alt] ||= "" + helpers do + # Override default image_tag helper to automatically calculate and include + # image dimensions. + # + # @param [String] path + # @param [Hash] params + # @return [String] + def image_tag(path, params={}) + if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://") + params[:alt] ||= "" - real_path = path - real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') - full_path = File.join(source_dir, real_path) + real_path = path + real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') + full_path = File.join(source_dir, real_path) - if File.exists?(full_path) - begin - width, height = ::FastImage.size(full_path, :raise_on_failure => true) - params[:width] = width - params[:height] = height - rescue FastImage::UnknownImageType - # No message, it's just not supported - rescue - warn "Couldn't determine dimensions for image #{path}: #{$!.message}" - end + if File.exists?(full_path) + begin + width, height = ::FastImage.size(full_path, :raise_on_failure => true) + params[:width] = width + params[:height] = height + rescue FastImage::UnknownImageType + # No message, it's just not supported + rescue + warn "Couldn't determine dimensions for image #{path}: #{$!.message}" end end - - super(path, params) end + + super(path, params) end end end diff --git a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb index b5bdd721..4394ca3a 100644 --- a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb +++ b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb @@ -3,50 +3,26 @@ module Middleman module Extensions # Directory Indexes extension - module DirectoryIndexes + class DirectoryIndexes < ::Middleman::Extension + # Update the main sitemap resource list + # @return [void] + def manipulate_resource_list(resources) + index_file = app.index_file + new_index_path = "/#{index_file}" - # Setup extension - class << self + resources.each do |resource| + # Check if it would be pointless to reroute + next if resource.destination_path == index_file || + resource.destination_path.end_with?(new_index_path) || + File.extname(index_file) != resource.ext - # Once registered - def registered(app) - app.after_configuration do - sitemap.register_resource_list_manipulator( - :directory_indexes, - DirectoryIndexManager.new(self) - ) - end - end + # Check if frontmatter turns directory_index off + next if resource.data[:directory_index] == false - alias :included :registered - end + # Check if file metadata (options set by "page" in config.rb) turns directory_index off + next if resource.metadata[:options][:directory_index] == false - # Central class for managing the directory indexes extension - class DirectoryIndexManager - def initialize(app) - @app = app - end - - # Update the main sitemap resource list - # @return [void] - def manipulate_resource_list(resources) - index_file = @app.index_file - new_index_path = "/#{index_file}" - - resources.each do |resource| - # Check if it would be pointless to reroute - next if resource.destination_path == index_file || - resource.destination_path.end_with?(new_index_path) || - File.extname(index_file) != resource.ext - - # Check if frontmatter turns directory_index off - next if resource.data[:directory_index] == false - - # Check if file metadata (options set by "page" in config.rb) turns directory_index off - next if resource.metadata[:options][:directory_index] == false - - resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path - end + resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path end end end