fix auto image sizes, make default helpers first feature

This commit is contained in:
tdreyno 2010-09-05 19:33:57 -07:00
parent 8b00e830f9
commit d638fe8ce2
3 changed files with 239 additions and 234 deletions

View file

@ -25,10 +25,10 @@ module Middleman
end end
# livereload # livereload
%w(asset_host %w(default_helpers
asset_host
automatic_image_sizes automatic_image_sizes
cache_buster cache_buster
default_helpers
minify_css minify_css
minify_javascript minify_javascript
relative_assets relative_assets

View file

@ -2,8 +2,8 @@ class Middleman::Features::AutomaticImageSizes
def initialize(app, config) def initialize(app, config)
require "middleman/features/automatic_image_sizes/fastimage" require "middleman/features/automatic_image_sizes/fastimage"
Middleman::Server.send :alias_method, :pre_automatic_image_tag, :image_tag
Middleman::Server.helpers do Middleman::Server.helpers do
alias_method :pre_automatic_image_tag, :image_tag
def image_tag(path, params={}) def image_tag(path, params={})
if (!params[:width] || !params[:height]) && !path.include?("://") if (!params[:width] || !params[:height]) && !path.include?("://")
params[:alt] ||= "" params[:alt] ||= ""
@ -12,7 +12,7 @@ class Middleman::Features::AutomaticImageSizes
begin begin
real_path = File.join(settings.public, settings.images_dir, path) real_path = File.join(settings.public, settings.images_dir, path)
if File.exists? real_path if File.exists? real_path
dimensions = Middleman::FastImage.size(real_path, :raise_on_failure => true) dimensions = ::FastImage.size(real_path, :raise_on_failure => true)
params[:width] ||= dimensions[0] params[:width] ||= dimensions[0]
params[:height] ||= dimensions[1] params[:height] ||= dimensions[1]
end end

View file

@ -32,7 +32,6 @@
require 'net/https' require 'net/https'
require 'open-uri' require 'open-uri'
module Middleman
class FastImage class FastImage
attr_reader :size, :type attr_reader :size, :type
@ -137,16 +136,22 @@ module Middleman
@property = options[:type_only] ? :type : :size @property = options[:type_only] ? :type : :size
@timeout = options[:timeout] || DefaultTimeout @timeout = options[:timeout] || DefaultTimeout
@uri = uri @uri = uri
@parsed_uri = URI.parse(uri.gsub(/\s/, "%20")) begin
@parsed_uri = URI.parse(uri)
rescue URI::InvalidURIError
fetch_using_open_uri
else
if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https" if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https"
fetch_using_http fetch_using_http
else else
fetch_using_open_uri fetch_using_open_uri
end end
end
raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size raise SizeNotFound if options[:raise_on_failure] && @property == :size && !@size
rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, ImageFetchFailure rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET,
ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT
raise ImageFetchFailure if options[:raise_on_failure] raise ImageFetchFailure if options[:raise_on_failure]
rescue Errno::ENOENT rescue NoMethodError # 1.8.7p248 can raise this due to a net/http bug
raise ImageFetchFailure if options[:raise_on_failure] raise ImageFetchFailure if options[:raise_on_failure]
rescue UnknownImageType rescue UnknownImageType
raise UnknownImageType if options[:raise_on_failure] raise UnknownImageType if options[:raise_on_failure]
@ -198,6 +203,7 @@ module Middleman
def parse_size def parse_size
@type = parse_type unless @type @type = parse_type unless @type
@strpos = 0
send("parse_size_for_#{@type}") send("parse_size_for_#{@type}")
end end
@ -237,11 +243,11 @@ module Middleman
end end
def parse_size_for_gif def parse_size_for_gif
get_chars(9)[4..8].unpack('SS') get_chars(11)[6..10].unpack('SS')
end end
def parse_size_for_png def parse_size_for_png
get_chars(23)[14..22].unpack('NN') get_chars(25)[16..24].unpack('NN')
end end
def parse_size_for_jpeg def parse_size_for_jpeg
@ -275,8 +281,7 @@ module Middleman
end end
def parse_size_for_bmp def parse_size_for_bmp
d = get_chars(27)[12..26] d = get_chars(29)[14..28]
d[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS') d.unpack("C")[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
end
end end
end end