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
end if ext.respond_to?(:manipulate_resource_list)
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
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.register
Middleman::Extensions::AssetHash
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.register
Middleman::Extensions::AssetHost
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.register
Middleman::Extensions::DirectoryIndexes
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.register
Middleman::Extensions::AutomaticImageSizes
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,37 +10,37 @@ 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
# image dimensions. # image dimensions.
# #
# @param [String] path # @param [String] path
# @param [Hash] params # @param [Hash] params
# @return [String] # @return [String]
def image_tag(path, params={}) def image_tag(path, params={})
if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://") if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
params[:alt] ||= "" params[:alt] ||= ""
real_path = path real_path = path
real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
full_path = File.join(source_dir, real_path) full_path = File.join(source_dir, real_path)
if File.exists?(full_path) if File.exists?(full_path)
begin begin
width, height = ::FastImage.size(full_path, :raise_on_failure => true) width, height = ::FastImage.size(full_path, :raise_on_failure => true)
params[:width] = width params[:width] = width
params[:height] = height params[:height] = height
rescue FastImage::UnknownImageType rescue FastImage::UnknownImageType
# No message, it's just not supported # No message, it's just not supported
rescue rescue
warn "Couldn't determine dimensions for image #{path}: #{$!.message}" warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
end
end end
end end
super(path, params)
end end
super(path, params)
end end
end end
end end

View file

@ -3,50 +3,26 @@ module Middleman
module Extensions module Extensions
# Directory Indexes extension # 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 resources.each do |resource|
class << self # 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 # Check if frontmatter turns directory_index off
def registered(app) next if resource.data[:directory_index] == false
app.after_configuration do
sitemap.register_resource_list_manipulator(
:directory_indexes,
DirectoryIndexManager.new(self)
)
end
end
alias :included :registered # Check if file metadata (options set by "page" in config.rb) turns directory_index off
end next if resource.metadata[:options][:directory_index] == false
# Central class for managing the directory indexes extension resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path
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
end end
end end
end end