class Middleman::Extensions::Gzip

def after_build(builder)

def after_build(builder)
  num_threads = 4
  paths = ::Middleman::Util.all_files_under(app.config[:build_dir])
  total_savings = 0
  # Fill a queue with inputs
  in_queue = Queue.new
  paths.each do |path|
    in_queue << path if should_gzip?(path)
  end
  num_paths = in_queue.size
  # Farm out gzip tasks to threads and put the results in in_queue
  out_queue = Queue.new
  num_threads.times.each do
    Thread.new do
      while path = in_queue.pop
        out_queue << gzip_file(path.to_s)
      end
    end
  end
  # Insert a nil for each thread to stop it
  num_threads.times do
    in_queue << nil
  end
  old_locale = I18n.locale
  I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
  num_paths.times do
    output_filename, old_size, new_size = out_queue.pop
    next unless output_filename
    total_savings += (old_size - new_size)
    size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
    builder.trigger :created, "#{output_filename} (#{NumberHelpers.new.number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
  end
  builder.trigger :gzip, '', "Total gzip savings: #{NumberHelpers.new.number_to_human_size(total_savings)}"
  I18n.locale = old_locale
end