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

@ -12,6 +12,12 @@ Feature: Custom layouts
And the Server is running at "custom-layout-app2"
When I go to "/custom-layout.html"
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
Given page "/custom-layout-dir/" has layout "custom"

View file

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

View file

@ -1,2 +1,4 @@
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
@app.ignore do |path|
p = @app.sitemap.page(path)
file_path = p.relative_path
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
if @app.sitemap.exists?(path)
p = @app.sitemap.page(path)
!p.proxy? && p.data && p.data["ignored"] == true
else
false
end

View file

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

View file

@ -9,63 +9,49 @@ module Middleman::Extensions
# Once registered
def registered(app)
# Include methods
app.send :include, InstanceMethods
app.after_configuration do
# Register a reroute transform that turns regular paths into indexed paths
sitemap.reroute do |destination, page|
new_index_path = "/#{index_file}"
frontmatter_ignore = false
# Check for file and frontmatter
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
# Check if it would be pointless to reroute
path = page.path
if ignored_directory_indexes.include? page
destination
elsif path == index_file || path.end_with?(new_index_path)
destination
elsif frontmatter_ignore
destination
elsif index_ext != File.extname(path)
destination
else
destination.chomp(File.extname(index_file)) + new_index_path
page_already_index = path == index_file || path.end_with?(new_index_path)
if page_already_index || File.extname(index_file) != File.extname(path)
next destination
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
alias :included :registered
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
# Register the extension

View file

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