From 527fa3160f9820e4c0d87ed00f098d68f65c4c50 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 23 Jul 2011 22:21:52 -0700 Subject: [PATCH] allow dynamic file generation. closes #68 --- features/dynamic_pages.feature | 24 ++++++++++++++++ fixtures/test-app/config.rb | 8 ++++++ fixtures/test-app/source/real.html | 1 + fixtures/test-app/source/real/index.html.erb | 5 ++++ lib/middleman/builder.rb | 9 ++++-- lib/middleman/core_extensions/routing.rb | 30 ++++++++++++++------ 6 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 features/dynamic_pages.feature create mode 100644 fixtures/test-app/source/real.html create mode 100644 fixtures/test-app/source/real/index.html.erb diff --git a/features/dynamic_pages.feature b/features/dynamic_pages.feature new file mode 100644 index 00000000..7c69ebfa --- /dev/null +++ b/features/dynamic_pages.feature @@ -0,0 +1,24 @@ +Feature: Dynamic Pages + In order to use a single view to generate multiple output files + + Scenario: Checking built folder for content + Given a built test app + 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" + And cleanup built test app + + Scenario: Preview basic proxy + Given the Server is running + When I go to "/fake.html" + Then I should see "I am real" + + Scenario: Preview proxy with variable one + Given the Server is running + When I go to "/fake/one.html" + Then I should see "I am real: one" + + Scenario: Preview proxy with variable two + Given the Server is running + When I go to "/fake/two.html" + Then I should see "I am real: two" \ No newline at end of file diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index 816b6892..196a080a 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -1,3 +1,11 @@ +page "/fake.html", :proxy => "/real.html", :layout => false + +%w(one two).each do |num| + page "/fake/#{num}.html", :proxy => "/real/index.html" do + @num = num + end +end + with_layout false do page "/inline-css.html" page "/inline-js.html" diff --git a/fixtures/test-app/source/real.html b/fixtures/test-app/source/real.html new file mode 100644 index 00000000..cb312952 --- /dev/null +++ b/fixtures/test-app/source/real.html @@ -0,0 +1 @@ +I am real \ No newline at end of file diff --git a/fixtures/test-app/source/real/index.html.erb b/fixtures/test-app/source/real/index.html.erb new file mode 100644 index 00000000..190d84ec --- /dev/null +++ b/fixtures/test-app/source/real/index.html.erb @@ -0,0 +1,5 @@ +--- +layout: false +--- + +I am real: <%= @num %> \ No newline at end of file diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 62584b49..f5abc1fa 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -10,11 +10,11 @@ module Middleman config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first || source - source = File.expand_path(find_in_source_paths(source.to_s)) + # 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)) - + 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}/, "") @@ -40,7 +40,6 @@ module Middleman end end - def source_paths @source_paths ||= [ SHARED_SERVER.root @@ -49,6 +48,10 @@ module Middleman def build_all_files action Directory.new(self, SHARED_SERVER.views, SHARED_SERVER.build_dir, { :force => true }) + + SHARED_SERVER.proxied_paths.each do |url, proxy| + tilt_template(url.gsub(/^\//, "#{SHARED_SERVER.build_dir}/"), { :force => true }) + end end @@hooks = {} diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb index 0ec88937..3678627d 100644 --- a/lib/middleman/core_extensions/routing.rb +++ b/lib/middleman/core_extensions/routing.rb @@ -3,6 +3,8 @@ module Middleman::CoreExtensions::Routing def registered(app) app.extend ClassMethods + app.set :proxied_paths, {} + # Normalize the path and add index if we're looking at a directory app.before_processing do request.path_info = self.class.path_to_index(request.path) @@ -35,23 +37,33 @@ module Middleman::CoreExtensions::Routing set :layout, old_layout end - # The page method allows the layout to be set on a specific path - # page "/about.html", :layout => false - # page "/", :layout => :homepage_layout - def page(url, options={}, &block) + def paths_for_url(url) url = url.gsub(%r{#{settings.index_file}$}, "") url = url.gsub(%r{(\/)$}, "") if url.length > 1 paths = [url] paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1 paths << "/#{path_to_index(url)}" - - options[:layout] = settings.layout if options[:layout].nil? - + paths + end + + # The page method allows the layout to be set on a specific path + # page "/about.html", :layout => false + # page "/", :layout => :homepage_layout + def page(url, options={}, &block) has_block = block_given? - - paths.each do |p| + options[:layout] = settings.layout if options[:layout].nil? + + if options.has_key?(:proxy) + settings.proxied_paths[url] = options[:proxy] + end + + paths_for_url(url).each do |p| get(p) do + if settings.proxied_paths.has_key?(url) + request.path_info = settings.proxied_paths[url] + end + instance_eval(&block) if has_block process_request(options) end