Convert Gzip extension to new-style extension class

This commit is contained in:
Ben Hollis 2013-04-13 18:39:33 -07:00
parent 6a5ecb17a3
commit 6eb7ff72d3

View file

@ -15,34 +15,33 @@ module Middleman::Extensions
# Pass the :exts options to customize which file extensions get zipped (defaults # Pass the :exts options to customize which file extensions get zipped (defaults
# to .html, .htm, .js and .css. # to .html, .htm, .js and .css.
# #
module Gzip class Gzip < ::Middleman::Extension
class << self option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.'
def registered(app, options={})
exts = options[:exts] || %w(.js .css .html .htm)
app.after_build do |builder| def initialize(app, options_hash={})
super
paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir) gzip_ext = self
paths.each do |path|
next unless exts.include? path.extname
output_filename, old_size, new_size = Middleman::Extensions::Gzip.gzip_file(path.to_s) app.after_build do |builder|
paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir)
paths.each do |path|
next unless gzip_ext.options.exts.include? path.extname
if output_filename output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s)
size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
old_locale = I18n.locale if output_filename
I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})" old_locale = I18n.locale
I18n.locale = old_locale I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
end builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
I18n.locale = old_locale
end end
end end
end end
alias :included :registered
end end
def self.gzip_file(path) def gzip_file(path)
input_file = File.open(path, 'rb').read input_file = File.open(path, 'rb').read
output_filename = path + '.gz' output_filename = path + '.gz'
input_file_time = File.mtime(path) input_file_time = File.mtime(path)