2012-01-08 01:57:50 +01:00
|
|
|
# Core Sitemap Extensions
|
2011-11-08 07:34:02 +01:00
|
|
|
module Middleman::CoreExtensions::Sitemap
|
2012-01-14 21:18:39 +01:00
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Setup Extension
|
2011-11-08 07:34:02 +01:00
|
|
|
class << self
|
2012-01-14 21:18:39 +01:00
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Once registered
|
2011-11-08 07:34:02 +01:00
|
|
|
def registered(app)
|
2012-01-08 01:57:50 +01:00
|
|
|
# Setup callbacks which can exclude paths from the sitemap
|
2011-12-28 03:47:03 +01:00
|
|
|
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(/\/__/)) },
|
|
|
|
|
2012-02-12 20:45:42 +01:00
|
|
|
:layout => proc { |file, path|
|
|
|
|
file.match(/^source\/layout\./) || file.match(/^source\/layouts\//)
|
|
|
|
},
|
|
|
|
|
2011-12-28 03:47:03 +01:00
|
|
|
# Files without any output extension (layouts, partials)
|
2012-02-12 20:45:42 +01:00
|
|
|
# :extensionless => proc { |file, path| !path.match(/\./) },
|
2011-12-28 03:47:03 +01:00
|
|
|
}
|
2012-01-08 01:57:50 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Sitemap instance methods
|
2011-11-18 09:34:56 +01:00
|
|
|
module InstanceMethods
|
2012-01-08 01:57:50 +01:00
|
|
|
|
|
|
|
# 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
|
2011-12-01 05:04:23 +01:00
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# 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 + "/"}})
|
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Register file change callback
|
|
|
|
files.changed sitemap_regex do |file|
|
|
|
|
sitemap.touch_file(file)
|
2011-11-18 09:34:56 +01:00
|
|
|
end
|
2012-01-08 01:57:50 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Get the sitemap class instance
|
|
|
|
# @return [Middleman::Sitemap::Store]
|
2011-11-10 06:19:11 +01:00
|
|
|
def sitemap
|
2012-01-08 01:57:50 +01:00
|
|
|
@_sitemap ||= ::Middleman::Sitemap::Store.new(self)
|
2011-11-10 06:19:11 +01:00
|
|
|
end
|
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Get the page object for the current path
|
|
|
|
# @return [Middleman::Sitemap::Page]
|
2011-12-23 21:04:38 +01:00
|
|
|
def current_page
|
|
|
|
sitemap.page(current_path)
|
|
|
|
end
|
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Ignore a path, regex or callback
|
|
|
|
# @param [String, Regexp]
|
|
|
|
# @return [void]
|
2012-01-08 02:21:26 +01:00
|
|
|
def ignore(*args, &block)
|
|
|
|
sitemap.ignore(*args, &block)
|
2011-11-09 01:04:53 +01:00
|
|
|
end
|
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Proxy one path to another
|
|
|
|
# @param [String] url
|
|
|
|
# @param [String] target
|
|
|
|
# @return [void]
|
2012-02-03 06:47:54 +01:00
|
|
|
def proxy(*args)
|
2012-01-08 01:57:50 +01:00
|
|
|
sitemap.proxy(*args)
|
2011-11-09 00:38:15 +01:00
|
|
|
end
|
2012-02-03 06:47:54 +01:00
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# 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
|
2011-12-08 06:00:27 +01:00
|
|
|
|
2012-01-08 01:57:50 +01:00
|
|
|
# Register a handler to provide metadata on a url path
|
|
|
|
# @param [Regexp] matcher
|
|
|
|
# @return [Array<Array<Proc, Regexp>>]
|
2011-12-08 06:00:27 +01:00
|
|
|
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
|
2012-02-03 06:47:54 +01:00
|
|
|
end
|