Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Sitemap/frontmatter/globbing fixes #313

Merged
merged 4 commits into from

2 participants

Ben Hollis Thomas Reynolds
Ben Hollis
Owner

These commits came out of a long, deep rabbit hole I went down while trying to get the blog extension's tests passing on Ruby 1.8.7. I don't actually know why they were failing, but somewhere in all the changes I made the blog extension worked again, and I had fixed a few things besides.

  • The frontmatter extension's ignore callback will no longer accidentally create entries in the sitemap.
  • The sitemap will no longer implicitly create pages if it's asked about a page it doesn't already have. Extensions and other code that wants to add things to the sitemap will have to use the new add method. Hopefully this will keep things more sane and reduce a class of bugs I've been tracking down for a while.
  • You can now disable directory indexes via page in config.rb with a regex or glob:
page "no_indexes*", :directory_index => false
  • You can now use regex/glob within with_layout and it'll work. Example:
with_layout :admin do
  page "admin_*.html"
end
Thomas Reynolds
Owner

Love this.

Thomas Reynolds tdreyno merged commit d36077f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2012
  1. Ben Hollis
  2. Ben Hollis

    Don't let people (or messed up code) accidentally create sitemap page…

    bhollis authored
    …s through the page method
  3. Ben Hollis

    Make directory_indexes use provides_metadata_for_path instead of its …

    bhollis authored
    …own list of ignored paths, allowing it to support regexes/globs
  4. Ben Hollis
This page is out of date. Refresh to see the latest.
6 middleman-core/features/custom_layouts.feature
View
@@ -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"
2  middleman-core/features/directory_index.feature
View
@@ -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"
4 middleman-core/fixtures/indexable-app/config.rb
View
@@ -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
1  middleman-core/fixtures/indexable-app/source/wildcard_leave_me_alone.html
View
@@ -0,0 +1 @@
+Stay away, wildcards!
13 middleman-core/lib/middleman-core/core_extensions/front_matter.rb
View
@@ -109,16 +109,9 @@ def initialize(app)
# 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
16 middleman-core/lib/middleman-core/core_extensions/routing.rb
View
@@ -45,6 +45,9 @@ def with_layout(layout_name, &block)
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 @@ def page(url, opts={}, &block)
return
end
- # Default layout
- opts[:layout] = layout if opts[:layout].nil?
-
# Normalized path
url = full_path(url)
@@ -79,13 +79,9 @@ def page(url, opts={}, &block)
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
70 middleman-core/lib/middleman-core/extensions/directory_indexes.rb
View
@@ -9,63 +9,49 @@ class << self
# 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
+ # Check if it would be pointless to reroute
+ path = page.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
- index_ext = File.extname(index_file)
+ # Check if frontmatter turns directory_index off
+ d = page.data
+ next destination if d && d["directory_index"] == false
- # Only reroute if not ignored
- 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
+ # 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
20 middleman-core/lib/middleman-core/sitemap/store.rb
View
@@ -72,16 +72,24 @@ def reroute_callbacks
# @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 @@ def file_to_path(file)
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 @@ def touch_file(file)
end
# Add generic path
- p = page(path)
+ p = add(path)
p.source_file = File.expand_path(file, @app.root)
p.touch
Something went wrong with that request. Please try again.