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))
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

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
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

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,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

View file

@ -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