From 81a8192d14ba99af991c276f39ca9995e4e5fe83 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Thu, 29 Oct 2009 09:17:15 -0700 Subject: [PATCH] enable smush cache --- lib/middleman/features/smush_pngs.rb | 79 ++++++++------------- lib/middleman/templater+dynamic_renderer.rb | 7 +- 2 files changed, 32 insertions(+), 54 deletions(-) diff --git a/lib/middleman/features/smush_pngs.rb b/lib/middleman/features/smush_pngs.rb index 9c4c9b71..a5907407 100644 --- a/lib/middleman/features/smush_pngs.rb +++ b/lib/middleman/features/smush_pngs.rb @@ -1,58 +1,35 @@ -require 'json' -require 'open-uri' - -begin - require 'httpclient' -rescue LoadError - puts "httpclient not available. Install it with: gem install httpclient" -end +require "smusher" +require "middleman/builder" module Middleman - module SmushPngs - def self.included(base) - base.supported_formats << "png" - end - - def render_path(file) - if File.extname(file) == ".png" - file = File.join(options.public, file) - optimized = optimized_image_data_for(file) - - begin - raise "Error: got larger" if size(file) < optimized.size - raise "Error: empty file downloaded" if optimized.size < 20 - - optimized - rescue - File.read(file) - end + class Builder + alias_method :pre_smush_after_run, :after_run + def after_run + pre_smush_after_run + smush_dir = File.join(Middleman::Base.build_dir, Middleman::Base.images_dir) + + # Read cache + cache_file = File.join(Middleman::Base.root, ".smush-cache") + cache_data = if File.exists?(cache_file) + Marshal.restore(File.read(cache_file)) else - super + {} + end + + ::Smusher.class_eval do + images_in_folder(smush_dir).each do |file| + original_file_size = size(file) + return if original_file_size.zero? + return if cache_data[file] && cache_data[file] == original_file_size + + with_logging(file, true) do + write_optimized_data(file) + cache_data[file] = size(file) # Add or update cache + File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache + say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Base.build_dir+"/", '') + end + end end end - - protected - def size(file) - File.exist?(file) ? File.size(file) : 0 - end - - def optimized_image_data_for(file) - # I leave these urls here, just in case it stops working again... - # url = "http://smush.it/ws.php" # original, redirects to somewhere else.. - url = 'http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php' - # url = "http://developer.yahoo.com/yslow/smushit/ws.php" # official but does not work - # url = "http://smushit.com/ysmush.it/ws.php" # used at the new page but does not hande uploads - # url = "http://smushit.eperf.vip.ac4.yahoo.com/ysmush.it/ws.php" # used at the new page but does not hande uploads - response = HTTPClient.post url, { 'files[]' => File.new(file) } - response = JSON.parse(response.body.content) - raise "smush.it: #{response['error']}" if response['error'] - image_url = response['dest'] - raise "no dest path found" unless image_url - open(image_url) { |source| source.read() } - end - end - - class Base - include Middleman::SmushPngs end end \ No newline at end of file diff --git a/lib/middleman/templater+dynamic_renderer.rb b/lib/middleman/templater+dynamic_renderer.rb index 4f1882a4..257d255f 100644 --- a/lib/middleman/templater+dynamic_renderer.rb +++ b/lib/middleman/templater+dynamic_renderer.rb @@ -14,12 +14,13 @@ end class Templater::Actions::Template def render + @@rack_test ||= Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) + @render_cache ||= begin # The default render just requests the page over Rack and writes the response request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "") - browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) - browser.get(request_path) - browser.last_response.body + @@rack_test.get(request_path) + @@rack_test.last_response.body end end end \ No newline at end of file