From 8ec42df64166baa0cdf459b619c8e76937ab902f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 15 Sep 2012 23:36:00 -0700 Subject: [PATCH 1/2] Correctly preview directory URLs with .s in them and show the request path on 404 pages. --- .../lib/middleman-core/application.rb | 31 ++++++++++++------- .../middleman-core/core_extensions/request.rb | 30 +++++++----------- .../middleman-core/core_extensions/routing.rb | 5 ++- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 65bc4f0a..a3772cd3 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -183,11 +183,7 @@ module Middleman # Evaluate a passed block if given instance_exec(&block) if block_given? - # Build expanded source path once paths have been parsed - path = root.dup - source_path = ENV["MM_SOURCE"] || self.source - path = File.join(root, source_path) unless source_path.empty? - set :source_dir, path + set :source, ENV["MM_SOURCE"] if ENV["MM_SOURCE"] super end @@ -216,6 +212,13 @@ module Middleman self end + # The full path to the source directory + # + # @return [String] + def source_dir + File.join(root, source) + end + delegate :logger, :instrument, :to => ::Middleman::Util # Work around this bug: http://bugs.ruby-lang.org/issues/4521 @@ -232,12 +235,18 @@ module Middleman # @param [String] path Request path # @return [String] Path with index file if necessary def full_path(path) - cache.fetch(:full_path, path) do - parts = path ? path.split('/') : [] - if parts.last.nil? || parts.last.split('.').length == 1 - path = File.join(path, index_file) - end - "/" + path.sub(%r{^/}, '') + resource = sitemap.find_resource_by_destination_path(path) + + if !resource + # Try it with /index.html at the end + indexed_path = File.join(path.sub(%r{/$}, ''), index_file) + resource = sitemap.find_resource_by_destination_path(indexed_path) + end + + if resource + '/' + resource.destination_path + else + '/' + Middleman::Util.normalize_path(path) end end diff --git a/middleman-core/lib/middleman-core/core_extensions/request.rb b/middleman-core/lib/middleman-core/core_extensions/request.rb index 9f750e16..b88eef57 100644 --- a/middleman-core/lib/middleman-core/core_extensions/request.rb +++ b/middleman-core/lib/middleman-core/core_extensions/request.rb @@ -227,32 +227,26 @@ module Middleman start_time = Time.now @current_path = nil - # Normalize the path and add index if we're looking at a directory - original_path = URI.decode(env["PATH_INFO"].dup) - if original_path.respond_to? :force_encoding - original_path.force_encoding('UTF-8') + request_path = URI.decode(env["PATH_INFO"].dup) + if request_path.respond_to? :force_encoding + request_path.force_encoding('UTF-8') end - request_path = full_path(original_path) + request_path = full_path(request_path) # Run before callbacks run_hook :before - if original_path != request_path - # Get the resource object for this path - resource = sitemap.find_resource_by_destination_path(original_path) - end - - # Get the resource object for this full path - resource ||= sitemap.find_resource_by_destination_path(request_path) + # Get the resource object for this path + resource = sitemap.find_resource_by_destination_path(request_path) # Return 404 if not in sitemap - return not_found(res) unless resource && !resource.ignored? + return not_found(res, request_path) unless resource && !resource.ignored? + + current_path = resource.destination_path # If this path is a static file, send it immediately return send_file(resource.source_file, env, res) unless resource.template? - current_path = request_path.dup - # Set a HTTP content type based on the request's extensions content_type(res, resource.mime_type) @@ -272,7 +266,7 @@ module Middleman end # End the request - logger.debug "== Finishing Request: #{request_path} (#{(Time.now - start_time).round(2)}s)" + logger.debug "== Finishing Request: #{current_path} (#{(Time.now - start_time).round(2)}s)" halt res.finish end @@ -289,9 +283,9 @@ module Middleman end # Halt request and return 404 - def not_found(res) + def not_found(res, path) res.status == 404 - res.write "

File Not Found

#{@request_path}

" + res.write "

File Not Found

#{path}

" res.finish end diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index e78d0361..db7c6e61 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -64,7 +64,10 @@ module Middleman end # Normalized path - url = full_path(url) + url = '/' + Middleman::Util.normalize_path(url) + if url.end_with?('/') || File.directory?(File.join(source_dir, url)) + url = File.join(url, index_file) + end # Setup proxy if opts.has_key?(:proxy) From d181ec5f4f35853304bcb23fe7210716d314cdfb Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 16 Sep 2012 01:56:14 -0700 Subject: [PATCH 2/2] Remove use of full_path and odd options from auto_tag --- .../core_extensions/default_helpers.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index 28b17e6b..85b76d46 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -29,20 +29,18 @@ module Middleman module Helpers # Output a stylesheet link tag based on the current path # - # @param [String] separator How to break up path in parts # @return [String] - def auto_stylesheet_link_tag(separator="/") - auto_tag(:css, separator) do |path| + def auto_stylesheet_link_tag + auto_tag(:css) do |path| stylesheet_link_tag path end end # Output a javascript tag based on the current path # - # @param [String] separator How to break up path in parts # @return [String] - def auto_javascript_include_tag(separator="/") - auto_tag(:js, separator) do |path| + def auto_javascript_include_tag + auto_tag(:js) do |path| javascript_include_tag path end end @@ -53,7 +51,7 @@ module Middleman # @param [String] separator How to break up path in parts # @param [String] asset_dir Where to look for assets # @return [void] - def auto_tag(asset_ext, separator="/", asset_dir=nil) + def auto_tag(asset_ext, asset_dir=nil) if asset_dir.nil? asset_dir = case asset_ext when :js then js_dir @@ -63,12 +61,10 @@ module Middleman # If the basename of the request as no extension, assume we are serving a # directory and join index_file to the path. - path = full_path(current_path.dup) - path = path.sub(%r{^/}, '') + path = File.join(asset_dir, current_path) path = path.gsub(File.extname(path), ".#{asset_ext}") - path = path.gsub("/", separator) - yield path if sitemap.find_resource_by_path(File.join(asset_dir, path)) + yield path if sitemap.find_resource_by_path(path) end # Generate body css classes based on the current path