Merge pull request #601 from bhollis/full_path

Fix previewing directories with '.' in them.
This commit is contained in:
Thomas Reynolds 2012-09-16 14:28:19 -07:00
commit a8a555c101
4 changed files with 43 additions and 41 deletions

View file

@ -183,11 +183,7 @@ module Middleman
# Evaluate a passed block if given # Evaluate a passed block if given
instance_exec(&block) if block_given? instance_exec(&block) if block_given?
# Build expanded source path once paths have been parsed set :source, ENV["MM_SOURCE"] if ENV["MM_SOURCE"]
path = root.dup
source_path = ENV["MM_SOURCE"] || self.source
path = File.join(root, source_path) unless source_path.empty?
set :source_dir, path
super super
end end
@ -216,6 +212,13 @@ module Middleman
self self
end end
# The full path to the source directory
#
# @return [String]
def source_dir
File.join(root, source)
end
delegate :logger, :instrument, :to => ::Middleman::Util delegate :logger, :instrument, :to => ::Middleman::Util
# Work around this bug: http://bugs.ruby-lang.org/issues/4521 # Work around this bug: http://bugs.ruby-lang.org/issues/4521
@ -232,12 +235,18 @@ module Middleman
# @param [String] path Request path # @param [String] path Request path
# @return [String] Path with index file if necessary # @return [String] Path with index file if necessary
def full_path(path) def full_path(path)
cache.fetch(:full_path, path) do resource = sitemap.find_resource_by_destination_path(path)
parts = path ? path.split('/') : []
if parts.last.nil? || parts.last.split('.').length == 1 if !resource
path = File.join(path, index_file) # Try it with /index.html at the end
end indexed_path = File.join(path.sub(%r{/$}, ''), index_file)
"/" + path.sub(%r{^/}, '') resource = sitemap.find_resource_by_destination_path(indexed_path)
end
if resource
'/' + resource.destination_path
else
'/' + Middleman::Util.normalize_path(path)
end end
end end

View file

@ -227,32 +227,26 @@ module Middleman
start_time = Time.now start_time = Time.now
@current_path = nil @current_path = nil
# Normalize the path and add index if we're looking at a directory request_path = URI.decode(env["PATH_INFO"].dup)
original_path = URI.decode(env["PATH_INFO"].dup) if request_path.respond_to? :force_encoding
if original_path.respond_to? :force_encoding request_path.force_encoding('UTF-8')
original_path.force_encoding('UTF-8')
end end
request_path = full_path(original_path) request_path = full_path(request_path)
# Run before callbacks # Run before callbacks
run_hook :before run_hook :before
if original_path != request_path # Get the resource object for this path
# Get the resource object for this path resource = sitemap.find_resource_by_destination_path(request_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)
# Return 404 if not in sitemap # 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 # If this path is a static file, send it immediately
return send_file(resource.source_file, env, res) unless resource.template? 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 # Set a HTTP content type based on the request's extensions
content_type(res, resource.mime_type) content_type(res, resource.mime_type)
@ -272,7 +266,7 @@ module Middleman
end end
# End the request # 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 halt res.finish
end end
@ -289,9 +283,9 @@ module Middleman
end end
# Halt request and return 404 # Halt request and return 404
def not_found(res) def not_found(res, path)
res.status == 404 res.status == 404
res.write "<html><body><h1>File Not Found</h1><p>#{@request_path}</p></body>" res.write "<html><body><h1>File Not Found</h1><p>#{path}</p></body>"
res.finish res.finish
end end

View file

@ -64,7 +64,10 @@ module Middleman
end end
# Normalized path # 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 # Setup proxy
if opts.has_key?(:proxy) if opts.has_key?(:proxy)

View file

@ -29,20 +29,18 @@ module Middleman
module Helpers module Helpers
# Output a stylesheet link tag based on the current path # Output a stylesheet link tag based on the current path
# #
# @param [String] separator How to break up path in parts
# @return [String] # @return [String]
def auto_stylesheet_link_tag(separator="/") def auto_stylesheet_link_tag
auto_tag(:css, separator) do |path| auto_tag(:css) do |path|
stylesheet_link_tag path stylesheet_link_tag path
end end
end end
# Output a javascript tag based on the current path # Output a javascript tag based on the current path
# #
# @param [String] separator How to break up path in parts
# @return [String] # @return [String]
def auto_javascript_include_tag(separator="/") def auto_javascript_include_tag
auto_tag(:js, separator) do |path| auto_tag(:js) do |path|
javascript_include_tag path javascript_include_tag path
end end
end end
@ -53,7 +51,7 @@ module Middleman
# @param [String] separator How to break up path in parts # @param [String] separator How to break up path in parts
# @param [String] asset_dir Where to look for assets # @param [String] asset_dir Where to look for assets
# @return [void] # @return [void]
def auto_tag(asset_ext, separator="/", asset_dir=nil) def auto_tag(asset_ext, asset_dir=nil)
if asset_dir.nil? if asset_dir.nil?
asset_dir = case asset_ext asset_dir = case asset_ext
when :js then js_dir 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 # If the basename of the request as no extension, assume we are serving a
# directory and join index_file to the path. # directory and join index_file to the path.
path = full_path(current_path.dup) path = File.join(asset_dir, current_path)
path = path.sub(%r{^/}, '')
path = path.gsub(File.extname(path), ".#{asset_ext}") 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 end
# Generate body css classes based on the current path # Generate body css classes based on the current path