middleman/middleman-core/lib/middleman-core/core_extensions/sitemap.rb

104 lines
3 KiB
Ruby
Raw Normal View History

# Core Sitemap Extensions
2011-11-08 07:34:02 +01:00
module Middleman::CoreExtensions::Sitemap
2012-01-14 21:18:39 +01:00
# Setup Extension
2011-11-08 07:34:02 +01:00
class << self
2012-01-14 21:18:39 +01:00
# Once registered
2011-11-08 07:34:02 +01:00
def registered(app)
# Setup callbacks which can exclude paths from the sitemap
app.set :ignored_sitemap_matchers, {
# dotfiles and folders in the root
:root_dotfiles => proc { |file, path| file.match(/^\./) },
# Files starting with an dot, but not .htaccess
:source_dotfiles => proc { |file, path|
(file.match(/\/\./) && !file.match(/\/\.htaccess/))
},
# Files starting with an underscore, but not a double-underscore
:partials => proc { |file, path| (file.match(/\/_/) && !file.match(/\/__/)) },
:layout => proc { |file, path|
file.match(/^source\/layout\./) || file.match(/^source\/layouts\//)
},
# Files without any output extension (layouts, partials)
# :extensionless => proc { |file, path| !path.match(/\./) },
}
# Include instance methods
2011-11-18 09:34:56 +01:00
app.send :include, InstanceMethods
2011-11-08 02:58:07 +01:00
end
2011-11-08 07:34:02 +01:00
alias :included :registered
end
# Sitemap instance methods
2011-11-18 09:34:56 +01:00
module InstanceMethods
# Extend initialize to listen for change events
2011-11-18 09:34:56 +01:00
def initialize
2011-11-28 07:04:19 +01:00
super
# Cleanup paths
static_path = source_dir.sub(root, "").sub(/^\//, "")
2011-12-29 00:29:19 +01:00
sitemap_regex = static_path.empty? ? // : (%r{^#{static_path + "/"}})
# Register file change callback
files.changed sitemap_regex do |file|
sitemap.touch_file(file)
2011-11-18 09:34:56 +01:00
end
# Register file delete callback
files.deleted sitemap_regex do |file|
sitemap.remove_file(file)
2011-11-18 09:34:56 +01:00
end
2011-11-10 06:19:11 +01:00
end
2011-11-18 09:34:56 +01:00
# Get the sitemap class instance
# @return [Middleman::Sitemap::Store]
2011-11-10 06:19:11 +01:00
def sitemap
@_sitemap ||= ::Middleman::Sitemap::Store.new(self)
2011-11-10 06:19:11 +01:00
end
# Get the page object for the current path
# @return [Middleman::Sitemap::Page]
2011-12-23 21:04:38 +01:00
def current_page
2012-03-05 07:56:07 +01:00
sitemap.page_by_destination(current_path)
2011-12-23 21:04:38 +01:00
end
# Ignore a path, regex or callback
# @param [String, Regexp]
# @return [void]
def ignore(*args, &block)
sitemap.ignore(*args, &block)
end
# Proxy one path to another
# @param [String] url
# @param [String] target
# @return [void]
def proxy(*args)
sitemap.proxy(*args)
2011-11-09 00:38:15 +01:00
end
# Register a handler to provide metadata on a file path
# @param [Regexp] matcher
# @return [Array<Array<Proc, Regexp>>]
2011-11-18 09:34:56 +01:00
def provides_metadata(matcher=nil, &block)
@_provides_metadata ||= []
@_provides_metadata << [block, matcher] if block_given?
@_provides_metadata
end
# Register a handler to provide metadata on a url path
# @param [Regexp] matcher
# @return [Array<Array<Proc, Regexp>>]
def provides_metadata_for_path(matcher=nil, &block)
@_provides_metadata_for_path ||= []
@_provides_metadata_for_path << [block, matcher] if block_given?
@_provides_metadata_for_path
end
2011-11-09 00:38:15 +01:00
end
end