From c35a6fc3694fbab66e462839e91d58a8d6b524a2 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 27 Jul 2011 14:14:22 -0700 Subject: [PATCH] add ignorable paths --- features/dynamic_pages.feature | 4 ++++ .../step_definitions/page_layout_steps.rb | 2 -- fixtures/test-app/config.rb | 4 ++++ .../test-app/source/should_be_ignored.html | 1 + .../test-app/source/should_be_ignored2.html | 1 + .../test-app/source/should_be_ignored3.html | 1 + lib/middleman/builder.rb | 24 +++++++++++++------ lib/middleman/core_extensions/routing.rb | 17 ++++++++++++- 8 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 fixtures/test-app/source/should_be_ignored.html create mode 100644 fixtures/test-app/source/should_be_ignored2.html create mode 100644 fixtures/test-app/source/should_be_ignored3.html diff --git a/features/dynamic_pages.feature b/features/dynamic_pages.feature index 7c69ebfa..dd9217bb 100644 --- a/features/dynamic_pages.feature +++ b/features/dynamic_pages.feature @@ -6,6 +6,10 @@ Feature: Dynamic Pages Then "fake.html" should exist and include "I am real" Then "fake/one.html" should exist and include "I am real: one" Then "fake/two.html" should exist and include "I am real: two" + Then "target_ignore.html" should exist and include "Ignore me" + Then "should_be_ignored.html" should not exist + Then "should_be_ignored2.html" should not exist + Then "should_be_ignored3.html" should not exist And cleanup built test app Scenario: Preview basic proxy diff --git a/features/step_definitions/page_layout_steps.rb b/features/step_definitions/page_layout_steps.rb index 0595e0bd..2a6276ad 100644 --- a/features/step_definitions/page_layout_steps.rb +++ b/features/step_definitions/page_layout_steps.rb @@ -1,12 +1,10 @@ Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout| @server ||= Middleman.server - @server.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", "test-app") @server.page(url, :layout => layout.to_sym) end Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout| @server ||= Middleman.server - @server.set :root, File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", "test-app") @server.with_layout(layout.to_sym) do page(url) end diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index aa15121d..07134cb1 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -1,5 +1,9 @@ page "/fake.html", :proxy => "/real.html", :layout => false +ignore "/should_be_ignored.html" +page "/should_be_ignored2.html", :ignore => true +page "/target_ignore.html", :proxy => "/should_be_ignored3.html", :ignore => true + %w(one two).each do |num| page "/fake/#{num}.html", :proxy => "/real/index.html" do @num = num diff --git a/fixtures/test-app/source/should_be_ignored.html b/fixtures/test-app/source/should_be_ignored.html new file mode 100644 index 00000000..fb81d5c0 --- /dev/null +++ b/fixtures/test-app/source/should_be_ignored.html @@ -0,0 +1 @@ +

Ignore me!

\ No newline at end of file diff --git a/fixtures/test-app/source/should_be_ignored2.html b/fixtures/test-app/source/should_be_ignored2.html new file mode 100644 index 00000000..0940fd7c --- /dev/null +++ b/fixtures/test-app/source/should_be_ignored2.html @@ -0,0 +1 @@ +

Ignore me! 2

\ No newline at end of file diff --git a/fixtures/test-app/source/should_be_ignored3.html b/fixtures/test-app/source/should_be_ignored3.html new file mode 100644 index 00000000..98007c81 --- /dev/null +++ b/fixtures/test-app/source/should_be_ignored3.html @@ -0,0 +1 @@ +

Ignore me! 3

\ No newline at end of file diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 91664057..28224273 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -5,7 +5,7 @@ require 'rack/test' SHARED_SERVER = Middleman.server SHARED_SERVER.set :environment, :build -module Middleman +module Middleman module ThorActions def tilt_template(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} @@ -13,14 +13,13 @@ module Middleman # source = File.expand_path(find_in_source_paths(source.to_s)) context = instance_eval('binding') - - @@rack_test ||= ::Rack::Test::Session.new(::Rack::MockSession.new(SHARED_SERVER)) + + request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "") + return if SHARED_SERVER.excluded_paths.include?(request_path) create_file destination, nil, config do - # The default render just requests the page over Rack and writes the response - request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "") - @@rack_test.get(request_path.gsub(/\s/, "%20")) - @@rack_test.last_response.body + Middleman::Builder.shared_rack.get(request_path.gsub(/\s/, "%20")) + Middleman::Builder.shared_rack.last_response.body end end end @@ -29,6 +28,15 @@ module Middleman include Thor::Actions include Middleman::ThorActions + def self.shared_rack + @shared_rack ||= begin + mock = ::Rack::MockSession.new(SHARED_SERVER) + sess = ::Rack::Test::Session.new(mock) + sess.get("/") + sess + end + end + class_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls' def initialize(*args) @@ -46,6 +54,8 @@ module Middleman end def build_all_files + self.class.shared_rack + action Directory.new(self, SHARED_SERVER.views, SHARED_SERVER.build_dir, { :force => true }) SHARED_SERVER.proxied_paths.each do |url, proxy| diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb index 3678627d..03253306 100644 --- a/lib/middleman/core_extensions/routing.rb +++ b/lib/middleman/core_extensions/routing.rb @@ -4,6 +4,7 @@ module Middleman::CoreExtensions::Routing app.extend ClassMethods app.set :proxied_paths, {} + app.set :excluded_paths, [] # Normalize the path and add index if we're looking at a directory app.before_processing do @@ -47,6 +48,13 @@ module Middleman::CoreExtensions::Routing paths end + # Keep a path from building + def ignore(path) + settings.excluded_paths << paths_for_url(path) + settings.excluded_paths.flatten! + settings.excluded_paths.uniq! + end + # The page method allows the layout to be set on a specific path # page "/about.html", :layout => false # page "/", :layout => :homepage_layout @@ -56,8 +64,15 @@ module Middleman::CoreExtensions::Routing if options.has_key?(:proxy) settings.proxied_paths[url] = options[:proxy] + if options.has_key?(:ignore) && options[:ignore] + settings.ignore(options[:proxy]) + end + else + if options.has_key?(:ignore) && options[:ignore] + settings.ignore(url) + end end - + paths_for_url(url).each do |p| get(p) do if settings.proxied_paths.has_key?(url)