From 7ffd5ccea9ce31f9cc12088d0d97010e61103c3e Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 6 Jul 2011 22:41:12 -0700 Subject: [PATCH] use before hooks to simplify server and move other code into features and extensions --- features/helpers_page_classes.feature | 12 ++-- fixtures/test-app/config.rb | 58 ++++++---------- .../source/auto-css/auto-css.html.haml | 1 + .../test-app/source/auto-css/index.html.haml | 1 + .../source/auto-css/sub/auto-css.html.haml | 1 + .../test-app/source/auto-js/auto-js.html.haml | 1 + .../test-app/source/auto-js/index.html.haml | 1 + .../source/auto-js/sub/auto-js.html.haml | 1 + .../source/sub1/page-classes.html.haml | 1 + .../source/sub1/sub2/page-classes.html.haml | 1 + lib/middleman/core_extensions/assets.rb | 3 + lib/middleman/core_extensions/front_matter.rb | 15 +++++ lib/middleman/core_extensions/routing.rb | 15 ++++- lib/middleman/server.rb | 67 ++++++------------- 14 files changed, 90 insertions(+), 88 deletions(-) create mode 100755 fixtures/test-app/source/auto-css/auto-css.html.haml create mode 100755 fixtures/test-app/source/auto-css/index.html.haml create mode 100755 fixtures/test-app/source/auto-css/sub/auto-css.html.haml create mode 100755 fixtures/test-app/source/auto-js/auto-js.html.haml create mode 100755 fixtures/test-app/source/auto-js/index.html.haml create mode 100755 fixtures/test-app/source/auto-js/sub/auto-js.html.haml create mode 100755 fixtures/test-app/source/sub1/page-classes.html.haml create mode 100755 fixtures/test-app/source/sub1/sub2/page-classes.html.haml diff --git a/features/helpers_page_classes.feature b/features/helpers_page_classes.feature index 888fa564..38c67759 100644 --- a/features/helpers_page_classes.feature +++ b/features/helpers_page_classes.feature @@ -3,15 +3,15 @@ Feature: Built-in page_classes view helper Scenario: Viewing the root path Given the Server is running - When I go to "/page-class.html" - Then I should see "page-class" + When I go to "/page-classes.html" + Then I should see "page-classes" Scenario: Viewing a tier-1 path Given the Server is running - When I go to "/sub1/page-class.html" - Then I should see "sub1 sub1_page-class" + When I go to "/sub1/page-classes.html" + Then I should see "sub1 sub1_page-classes" Scenario: Viewing a tier-2 path Given the Server is running - When I go to "/sub1/sub2/page-class.html" - Then I should see "sub1 sub1_sub2 sub1_sub2_page-class" \ No newline at end of file + When I go to "/sub1/sub2/page-classes.html" + Then I should see "sub1 sub1_sub2 sub1_sub2_page-classes" \ No newline at end of file diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index e794ee59..6925dc58 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -4,41 +4,27 @@ with_layout false do page "/inline-coffeescript.html" page "/slim.html" page "/data.html" -end + page "/page-classes.html" + page "/sub1/page-classes.html" + page "/sub1/sub2/page-classes.html" -get "/page-class.html" do - haml :"page-classes.html", :layout => false -end - -get "/sub1/page-class.html" do - haml :"page-classes.html", :layout => false -end - -get "/sub1/sub2/page-class.html" do - haml :"page-classes.html", :layout => false -end - -%w{ - /auto-css.html - /auto-css - /auto-css/ - /auto-css/auto-css.html - /auto-css/sub/auto-css.html -}.each do |path| - get path do - haml :"auto-css.html", :layout => false - end -end - -%w{ - /auto-js.html - /auto-js - /auto-js/ - /auto-js/auto-js.html - /auto-js/sub/auto-js.html -}.each do |path| - get path do - haml :"auto-js.html", :layout => false - end -end + %w{ + /auto-css.html + /auto-css + /auto-css/ + /auto-css/auto-css.html + /auto-css/sub/auto-css.html + }.each do |path| + page path + end + %w{ + /auto-js.html + /auto-js + /auto-js/ + /auto-js/auto-js.html + /auto-js/sub/auto-js.html + }.each do |path| + page path + end +end \ No newline at end of file diff --git a/fixtures/test-app/source/auto-css/auto-css.html.haml b/fixtures/test-app/source/auto-css/auto-css.html.haml new file mode 100755 index 00000000..36324bb3 --- /dev/null +++ b/fixtures/test-app/source/auto-css/auto-css.html.haml @@ -0,0 +1 @@ += auto_stylesheet_link_tag \ No newline at end of file diff --git a/fixtures/test-app/source/auto-css/index.html.haml b/fixtures/test-app/source/auto-css/index.html.haml new file mode 100755 index 00000000..36324bb3 --- /dev/null +++ b/fixtures/test-app/source/auto-css/index.html.haml @@ -0,0 +1 @@ += auto_stylesheet_link_tag \ No newline at end of file diff --git a/fixtures/test-app/source/auto-css/sub/auto-css.html.haml b/fixtures/test-app/source/auto-css/sub/auto-css.html.haml new file mode 100755 index 00000000..36324bb3 --- /dev/null +++ b/fixtures/test-app/source/auto-css/sub/auto-css.html.haml @@ -0,0 +1 @@ += auto_stylesheet_link_tag \ No newline at end of file diff --git a/fixtures/test-app/source/auto-js/auto-js.html.haml b/fixtures/test-app/source/auto-js/auto-js.html.haml new file mode 100755 index 00000000..3bf0eb1e --- /dev/null +++ b/fixtures/test-app/source/auto-js/auto-js.html.haml @@ -0,0 +1 @@ += auto_javascript_include_tag diff --git a/fixtures/test-app/source/auto-js/index.html.haml b/fixtures/test-app/source/auto-js/index.html.haml new file mode 100755 index 00000000..3bf0eb1e --- /dev/null +++ b/fixtures/test-app/source/auto-js/index.html.haml @@ -0,0 +1 @@ += auto_javascript_include_tag diff --git a/fixtures/test-app/source/auto-js/sub/auto-js.html.haml b/fixtures/test-app/source/auto-js/sub/auto-js.html.haml new file mode 100755 index 00000000..3bf0eb1e --- /dev/null +++ b/fixtures/test-app/source/auto-js/sub/auto-js.html.haml @@ -0,0 +1 @@ += auto_javascript_include_tag diff --git a/fixtures/test-app/source/sub1/page-classes.html.haml b/fixtures/test-app/source/sub1/page-classes.html.haml new file mode 100755 index 00000000..0a933c0a --- /dev/null +++ b/fixtures/test-app/source/sub1/page-classes.html.haml @@ -0,0 +1 @@ += page_classes \ No newline at end of file diff --git a/fixtures/test-app/source/sub1/sub2/page-classes.html.haml b/fixtures/test-app/source/sub1/sub2/page-classes.html.haml new file mode 100755 index 00000000..0a933c0a --- /dev/null +++ b/fixtures/test-app/source/sub1/sub2/page-classes.html.haml @@ -0,0 +1 @@ += page_classes \ No newline at end of file diff --git a/lib/middleman/core_extensions/assets.rb b/lib/middleman/core_extensions/assets.rb index 83295fbe..ac081a43 100644 --- a/lib/middleman/core_extensions/assets.rb +++ b/lib/middleman/core_extensions/assets.rb @@ -1,6 +1,9 @@ module Middleman::CoreExtensions::Assets class << self def registered(app) + # Disable Padrino cache buster until explicitly enabled + app.set :asset_stamp, false + app.extend ClassMethods app.helpers Helpers diff --git a/lib/middleman/core_extensions/front_matter.rb b/lib/middleman/core_extensions/front_matter.rb index 5ef2eeff..d3a80b55 100644 --- a/lib/middleman/core_extensions/front_matter.rb +++ b/lib/middleman/core_extensions/front_matter.rb @@ -10,6 +10,21 @@ module Middleman::CoreExtensions::FrontMatter ::Tilt::register RedClothTemplate, 'textile' ::Tilt::register ERBTemplate, 'erb', 'rhtml' ::Tilt::register ErubisTemplate, 'erb', 'rhtml', 'erubis' + # + # app.before do + # full_file_path = "#{extensionless_path}.#{template_engine}" + # system_path = File.join(settings.views, full_file_path) + # data, content = self.class.parse_front_matter(File.read(system_path)) + # + # %w(layout layout_engine).each do |opt| + # if data.has_key?(opt) + # options[opt.to_sym] = data.delete(opt) + # end + # end + # + # # Forward remaining data to helpers + # self.class.data_content("page", data) + # end end alias :included :registered end diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb index 73b0443a..9587c4bd 100644 --- a/lib/middleman/core_extensions/routing.rb +++ b/lib/middleman/core_extensions/routing.rb @@ -2,6 +2,11 @@ module Middleman::CoreExtensions::Routing class << self def registered(app) app.extend ClassMethods + + # Normalize the path and add index if we're looking at a directory + app.before do + request.path_info = self.class.path_to_index(request.path) + end end alias :included :registered end @@ -10,6 +15,14 @@ module Middleman::CoreExtensions::Routing def current_layout @layout end + + def path_to_index(path) + parts = path ? path.split('/') : [] + if parts.last.nil? || parts.last.split('.').length == 1 + path = File.join(path, settings.index_file) + end + path.gsub(%r{^/}, '') + end # Takes a block which allows many pages to have the same layout # with_layout :admin do @@ -34,7 +47,7 @@ module Middleman::CoreExtensions::Routing paths = [url] paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1 - paths << "/#{path_to_index(url)}" + paths << "#{path_to_index(url)}" options[:layout] = current_layout if options[:layout].nil? diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 34d7647b..db1996a1 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -23,11 +23,10 @@ module Middleman set :build_dir, "build" # Which folder are builds output to set :http_prefix, nil # During build, add a prefix for absolute paths + # Pass all request to Middleman, even "static" files set :static, false - set :views, "source" - # Disable Padrino cache buster until explicitly enabled - set :asset_stamp, false + set :views, "source" # Activate custom features register Middleman::CoreExtensions::Features @@ -72,66 +71,44 @@ module Middleman process_request end - private - def self.path_to_index(path) - parts = path ? path.split('/') : [] - if parts.last.nil? || parts.last.split('.').length == 1 - path = File.join(path, settings.index_file) + # See if Tilt cannot handle this file + before do + result = resolve_template(request.path_info, :raise_exceptions => false) + if result + extensionless_path, template_engine = result + + # Return static files + if !::Tilt.mappings.has_key?(template_engine.to_s) + content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8' + status 200 + send_file File.join(Middleman::Server.views, request.path_info) + end + else + $stderr.puts "File not found: #{request.path_info}" + status 404 end - path.gsub(%r{^/}, '') end - + + private # Internal method to look for templates and evaluate them if found def process_request(options={}) - # Normalize the path and add index if we're looking at a directory - path = self.class.path_to_index(request.path) - - extensionless_path, template_engine = resolve_template(path) - - if !::Tilt.mappings.has_key?(template_engine.to_s) - content_type mime_type(File.extname(path)), :charset => 'utf-8' - status 200 - send_file File.join(Middleman::Server.views, path) - return - end - - full_file_path = "#{extensionless_path}.#{template_engine}" - system_path = File.join(settings.views, full_file_path) - data, content = self.class.parse_front_matter(File.read(system_path)) - - %w(layout layout_engine).each do |opt| - if data.has_key?(opt) - options[opt.to_sym] = data.delete(opt) - end - end - - # Forward remaining data to helpers - self.class.data_content("page", data) - old_layout = settings.current_layout settings.layout(options[:layout]) if !options[:layout].nil? layout = settings.fetch_layout_path.to_sym - layout = false if options[:layout] == false or path =~ /\.(css|js)$/ + layout = false if options[:layout] == false or request.path_info =~ /\.(css|js)$/ render_options = { :layout => layout } render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine - result = render(path, render_options) + result = render(request.path_info, render_options) settings.layout(old_layout) if result - content_type mime_type(File.extname(path)), :charset => 'utf-8' + content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8' status 200 return result end status 404 - rescue Padrino::Rendering::TemplateNotFound - if settings.environment == :development - raise $! - else - $stderr.puts "File not found: #{request.path}" - status 404 - end end end end \ No newline at end of file