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,18 +15,20 @@ 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) def initialize(app, options_hash={})
super
gzip_ext = self
app.after_build do |builder| app.after_build do |builder|
paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir) paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir)
paths.each do |path| paths.each do |path|
next unless exts.include? path.extname next unless gzip_ext.options.exts.include? path.extname
output_filename, old_size, new_size = Middleman::Extensions::Gzip.gzip_file(path.to_s) output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s)
if output_filename if output_filename
size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger' size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
@ -39,10 +41,7 @@ module Middleman::Extensions
end end
end end
alias :included :registered def gzip_file(path)
end
def self.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)