From af8f1c4f26deacc98e4c62fae6a48047d349f631 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Tue, 2 Feb 2010 17:03:43 -0800 Subject: [PATCH] Sinatra 1.0 based prerelease --- VERSION | 2 +- features/asset_host.feature | 12 ++++++++ features/step_definitions/asset_host_steps.rb | 6 ++++ lib/middleman/base.rb | 8 ++++++ lib/middleman/features/asset_host.rb | 28 +++++++++++++++++++ .../sample/views/asset_host.html.haml | 1 + .../views/stylesheets/asset_host.css.sass | 3 ++ 7 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 features/asset_host.feature create mode 100644 features/step_definitions/asset_host_steps.rb create mode 100644 lib/middleman/features/asset_host.rb create mode 100755 spec/fixtures/sample/views/asset_host.html.haml create mode 100755 spec/fixtures/sample/views/stylesheets/asset_host.css.sass diff --git a/VERSION b/VERSION index 26acbf08..51de3305 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.12.2 +0.13.0 \ No newline at end of file diff --git a/features/asset_host.feature b/features/asset_host.feature new file mode 100644 index 00000000..beb1bc5b --- /dev/null +++ b/features/asset_host.feature @@ -0,0 +1,12 @@ +Feature: Alternate between multiple asset hosts + In order to speed up page loading + + Scenario: Rendering css with the feature enabled + Given I am using an asset host + When I go to "/stylesheets/asset_host.css" + Then I should see "http://assets" + + Scenario: Rendering html with the feature enabled + Given I am using an asset host + When I go to "/asset_host.html" + Then I should see "http://assets" \ No newline at end of file diff --git a/features/step_definitions/asset_host_steps.rb b/features/step_definitions/asset_host_steps.rb new file mode 100644 index 00000000..1a12a49f --- /dev/null +++ b/features/step_definitions/asset_host_steps.rb @@ -0,0 +1,6 @@ +Given /^I am using an asset host$/ do + Middleman::Base.set :asset_host do |asset| + "http://assets%d.example.com" % (asset.hash % 4) + end + @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base.new)) +end \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 2569285c..cc162845 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -39,6 +39,13 @@ module Middleman super end + def self.set(option, value=self, &block) + if block_given? + value = Proc.new { block } + end + super(option, value) + end + @@afters = [] def self.after_feature_init(&block) @@afters << block @@ -134,6 +141,7 @@ class Middleman::Base helpers Middleman::Helpers # Features disabled by default + enable :asset_host disable :slickmap disable :cache_buster disable :minify_css diff --git a/lib/middleman/features/asset_host.rb b/lib/middleman/features/asset_host.rb new file mode 100644 index 00000000..4ac03506 --- /dev/null +++ b/lib/middleman/features/asset_host.rb @@ -0,0 +1,28 @@ +class Middleman::Base + after_feature_init do + ::Compass.configuration do |config| + config.asset_host(&self.asset_host) + end + end if self.enabled?(:asset_host) +end + +class << Middleman::Base + alias_method :pre_asset_host_asset_url, :asset_url + def asset_url(path, prefix="", request=nil) + original_output = pre_asset_host_asset_url(path, prefix, request) + + valid_extensions = %w(.png .gif .jpg .jpeg .js .css) + + if !self.enabled?(:asset_host) || path.include?("://") || !valid_extensions.include?(File.extname(path)) || !self.asset_host + return original_output + end + + asset_prefix = if self.asset_host.is_a?(Proc) || self.asset_host.respond_to?(:call) + self.asset_host.call(original_output) + else + (self.asset_host =~ /%d/) ? self.asset_host % (original_output.hash % 4) : self.asset_host + end + + return File.join(asset_prefix, original_output) + end +end diff --git a/spec/fixtures/sample/views/asset_host.html.haml b/spec/fixtures/sample/views/asset_host.html.haml new file mode 100755 index 00000000..7588118c --- /dev/null +++ b/spec/fixtures/sample/views/asset_host.html.haml @@ -0,0 +1 @@ += image_tag "blank.gif" \ No newline at end of file diff --git a/spec/fixtures/sample/views/stylesheets/asset_host.css.sass b/spec/fixtures/sample/views/stylesheets/asset_host.css.sass new file mode 100755 index 00000000..6d131005 --- /dev/null +++ b/spec/fixtures/sample/views/stylesheets/asset_host.css.sass @@ -0,0 +1,3 @@ +@import compass.sass +h1 + background= image_url("blank.gif") \ No newline at end of file