Merge pull request #313 from bhollis/sitemap_globs

Sitemap/frontmatter/globbing fixes
This commit is contained in:
Thomas Reynolds 2012-03-15 11:32:03 -07:00
commit d36077f47b
8 changed files with 64 additions and 72 deletions

View file

@ -13,6 +13,12 @@ Feature: Custom layouts
When I go to "/custom-layout.html" When I go to "/custom-layout.html"
Then I should see "Custom Layout" Then I should see "Custom Layout"
Scenario: Using with_layout block with globs
Given "/custom-*" with_layout block has layout "custom"
And the Server is running at "custom-layout-app2"
When I go to "/custom-layout.html"
Then I should see "Custom Layout"
Scenario: Using custom :layout attribute with folders Scenario: Using custom :layout attribute with folders
Given page "/custom-layout-dir/" has layout "custom" Given page "/custom-layout-dir/" has layout "custom"
And the Server is running at "custom-layout-app2" And the Server is running at "custom-layout-app2"

View file

@ -8,6 +8,7 @@ Feature: Directory Index
| needs_index/index.html | | needs_index/index.html |
| a_folder/needs_index/index.html | | a_folder/needs_index/index.html |
| leave_me_alone.html | | leave_me_alone.html |
| wildcard_leave_me_alone.html |
| regular/index.html | | regular/index.html |
| .htaccess | | .htaccess |
Then the following files should not exist: Then the following files should not exist:
@ -15,6 +16,7 @@ Feature: Directory Index
| needs_index.html | | needs_index.html |
| a_folder/needs_index.html | | a_folder/needs_index.html |
| leave_me_alone/index.html | | leave_me_alone/index.html |
| wildcard_leave_me_alone/index.html |
And the file "needs_index/index.html" should contain "Indexable" And the file "needs_index/index.html" should contain "Indexable"
And the file "a_folder/needs_index/index.html" should contain "Indexable" And the file "a_folder/needs_index/index.html" should contain "Indexable"
And the file "leave_me_alone.html" should contain "Stay away" And the file "leave_me_alone.html" should contain "Stay away"

View file

@ -1,2 +1,4 @@
activate :directory_indexes activate :directory_indexes
page "/leave_me_alone.html", :directory_index => false page "/leave_me_alone.html", :directory_index => false
page "/wildcard*", :directory_index => false

View file

@ -0,0 +1 @@
Stay away, wildcards!

View file

@ -109,16 +109,9 @@ module Middleman::CoreExtensions::FrontMatter
# Setup ignore callback # Setup ignore callback
@app.ignore do |path| @app.ignore do |path|
p = @app.sitemap.page(path) if @app.sitemap.exists?(path)
file_path = p.relative_path p = @app.sitemap.page(path)
!p.proxy? && p.data && p.data["ignored"] == true
if !p.proxy? && has_data?(file_path)
d = data(file_path)
if d && d[0]
d[0].has_key?("ignored") && d[0]["ignored"] == true
else
false
end
else else
false false
end end

View file

@ -45,6 +45,9 @@ module Middleman::CoreExtensions::Routing
def page(url, opts={}, &block) def page(url, opts={}, &block)
a_block = block_given? ? block : nil a_block = block_given? ? block : nil
# Default layout
opts[:layout] = layout if opts[:layout].nil?
# If the url is a regexp # If the url is a regexp
if url.is_a?(Regexp) || url.include?("*") if url.is_a?(Regexp) || url.include?("*")
@ -56,9 +59,6 @@ module Middleman::CoreExtensions::Routing
return return
end end
# Default layout
opts[:layout] = layout if opts[:layout].nil?
# Normalized path # Normalized path
url = full_path(url) url = full_path(url)
@ -79,13 +79,9 @@ module Middleman::CoreExtensions::Routing
end end
end end
# If we have a block or opts # Setup a metadata matcher for rendering those options
if a_block || !opts.empty? provides_metadata_for_path url do |url|
{ :options => opts, :blocks => [a_block] }
# Setup a metadata matcher for rendering those options
provides_metadata_for_path url do |url|
{ :options => opts, :blocks => [a_block] }
end
end end
end end
end end

View file

@ -9,63 +9,49 @@ module Middleman::Extensions
# Once registered # Once registered
def registered(app) def registered(app)
# Include methods
app.send :include, InstanceMethods
app.after_configuration do app.after_configuration do
# Register a reroute transform that turns regular paths into indexed paths # Register a reroute transform that turns regular paths into indexed paths
sitemap.reroute do |destination, page| sitemap.reroute do |destination, page|
new_index_path = "/#{index_file}" new_index_path = "/#{index_file}"
frontmatter_ignore = false
# Check for file and frontmatter # Check if it would be pointless to reroute
d = page.data
if !page.data.nil?
frontmatter_ignore = d.has_key?("directory_index") && d["directory_index"] == false
end
index_ext = File.extname(index_file)
# Only reroute if not ignored
path = page.path path = page.path
if ignored_directory_indexes.include? page page_already_index = path == index_file || path.end_with?(new_index_path)
destination if page_already_index || File.extname(index_file) != File.extname(path)
elsif path == index_file || path.end_with?(new_index_path) next destination
destination
elsif frontmatter_ignore
destination
elsif index_ext != File.extname(path)
destination
else
destination.chomp(File.extname(index_file)) + new_index_path
end end
# Check if frontmatter turns directory_index off
d = page.data
next destination if d && d["directory_index"] == false
# Check if file metadata (options set by "page" in config.rb) turns directory_index off
# TODO: This is crazy - we need a generic way to get metadata for paths
metadata_ignore = false
provides_metadata_for_path.each do |callback, matcher|
if matcher.is_a? Regexp
next if !path.match(matcher)
elsif matcher.is_a? String
next if !File.fnmatch("/" + matcher.sub(%r{^/}, ''), "/#{path}")
end
result = instance_exec(path, &callback)
if result[:options] && result[:options][:directory_index] == false
metadata_ignore = true
break
end
end
next destination if metadata_ignore
# Not ignored, so reroute
destination.chomp(File.extname(index_file)) + new_index_path
end end
end end
end end
alias :included :registered alias :included :registered
end end
module InstanceMethods
# A list of pages which will not use directory indexes
# @return [Array<Middleman::Sitemap::Page>]
def ignored_directory_indexes
@_ignored_directory_indexes ||= []
end
# Override the page helper to accept a :directory_index option
#
# @param [String] url
# @param [Hash] options
# @return [void]
def page(url, options={}, &block)
if options.has_key?(:directory_index) && !options["directory_index"]
ignored_directory_indexes << sitemap.page(url)
else
super
end
end
end
end end
# Register the extension # Register the extension

View file

@ -72,16 +72,24 @@ module Middleman::Sitemap
# @param [String] target # @param [String] target
# @return [void] # @return [void]
def proxy(path, target) def proxy(path, target)
page(path).proxy_to(normalize_path(target)) add(path).proxy_to(normalize_path(target))
app.cache.remove(:proxied_paths) app.cache.remove(:proxied_paths)
end end
# Get a page instance for a given path # Add a new page to the sitemap
# @param [String] path
# @return [Middleman::Sitemap::Page]
def add(path)
path = normalize_path(path)
@pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) }
end
# Get a page instance for a given path, or nil if that page doesn't exist in the sitemap
# @param [String] path # @param [String] path
# @return [Middleman::Sitemap::Page] # @return [Middleman::Sitemap::Page]
def page(path) def page(path)
path = normalize_path(path) path = normalize_path(path)
@pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) } @pages[path]
end end
# Find a page given its destination path # Find a page given its destination path
@ -134,9 +142,7 @@ module Middleman::Sitemap
return false unless file.include?(prefix) return false unless file.include?(prefix)
path = file.sub(prefix, "") path = file.sub(prefix, "")
path = extensionless_path(path) extensionless_path(path)
path
end end
# Update or add an on-disk file path # Update or add an on-disk file path
@ -153,7 +159,7 @@ module Middleman::Sitemap
end end
# Add generic path # Add generic path
p = page(path) p = add(path)
p.source_file = File.expand_path(file, @app.root) p.source_file = File.expand_path(file, @app.root)
p.touch p.touch