Convert Sitemap::Extensions into actual Middleman::Extension

This commit is contained in:
Thomas Reynolds 2014-07-08 17:02:02 -07:00
parent 52dcf37f24
commit 928eb82d65
8 changed files with 67 additions and 66 deletions

View file

@ -332,6 +332,5 @@ module Middleman
"#<Middleman::Application:0x#{object_id}>" "#<Middleman::Application:0x#{object_id}>"
end end
alias_method :inspect, :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s alias_method :inspect, :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s
end end
end end

View file

@ -1,6 +1,5 @@
module Middleman module Middleman
module Configuration module Configuration
# A class that manages a collection of documented settings. # A class that manages a collection of documented settings.
# Can be used by extensions as well as the main Middleman # Can be used by extensions as well as the main Middleman
# application. Extensions should probably finalize their instance # application. Extensions should probably finalize their instance

View file

@ -2,16 +2,17 @@ module Middleman
module Sitemap module Sitemap
module Extensions module Extensions
# Class to handle managing ignores # Class to handle managing ignores
class Ignores class Ignores < Extension
def initialize(app, sitemap) def initialize(app, config={}, &block)
@app = app super
@app.add_to_config_context :ignore, &method(:create_ignore) @app.add_to_config_context :ignore, &method(:create_ignore)
@app.define_singleton_method :ignore, &method(:create_ignore) @app.define_singleton_method :ignore, &method(:create_ignore)
# Array of callbacks which can ass ignored # Array of callbacks which can ass ignored
@ignored_callbacks = [] @ignored_callbacks = []
sitemap.define_singleton_method :ignored?, &method(:ignored?) @app.sitemap.define_singleton_method :ignored?, &method(:ignored?)
end end
# Ignore a path or add an ignore callback # Ignore a path or add an ignore callback

View file

@ -3,30 +3,17 @@ require 'set'
module Middleman module Middleman
module Sitemap module Sitemap
module Extensions module Extensions
class OnDisk class OnDisk < Extension
attr_accessor :sitemap
attr_accessor :waiting_for_ready attr_accessor :waiting_for_ready
def initialize(app, sitemap) def initialize(app, config={}, &block)
@sitemap = sitemap super
@app = app
@file_paths_on_disk = Set.new @file_paths_on_disk = Set.new
scoped_self = self scoped_self = self
@waiting_for_ready = true @waiting_for_ready = true
@app.before_configuration do
# Register file change callback
extensions[:file_watcher].api.changed do |file|
scoped_self.touch_file(file)
end
# Register file delete callback
extensions[:file_watcher].api.deleted do |file|
scoped_self.remove_file(file)
end
end
@app.ready do @app.ready do
scoped_self.waiting_for_ready = false scoped_self.waiting_for_ready = false
# Make sure the sitemap is ready for the first request # Make sure the sitemap is ready for the first request
@ -34,13 +21,18 @@ module Middleman
end end
end end
def before_configuration
file_watcher.changed(&method(:touch_file))
file_watcher.deleted(&method(:remove_file))
end
# Update or add an on-disk file path # Update or add an on-disk file path
# @param [String] file # @param [String] file
# @return [Boolean] # @return [Boolean]
def touch_file(file) def touch_file(file)
return false if File.directory?(file) return false if File.directory?(file)
path = @sitemap.file_to_path(file) path = @app.sitemap.file_to_path(file)
return false unless path return false unless path
ignored = @app.config[:ignored_sitemap_matchers].any? do |_, callback| ignored = @app.config[:ignored_sitemap_matchers].any? do |_, callback|
@ -57,11 +49,11 @@ module Middleman
# in case one of the other manipulators # in case one of the other manipulators
# (like asset_hash) cares about the contents of this file, # (like asset_hash) cares about the contents of this file,
# whether or not it belongs in the sitemap (like a partial) # whether or not it belongs in the sitemap (like a partial)
@sitemap.rebuild_resource_list!(:touched_file) @app.sitemap.rebuild_resource_list!(:touched_file)
# Force sitemap rebuild so the next request is ready to go. # Force sitemap rebuild so the next request is ready to go.
# Skip this during build because the builder will control sitemap refresh. # Skip this during build because the builder will control sitemap refresh.
@sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? @app.sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build?
end end
# Remove a file from the store # Remove a file from the store
@ -70,11 +62,11 @@ module Middleman
def remove_file(file) def remove_file(file)
return unless @file_paths_on_disk.delete?(file) return unless @file_paths_on_disk.delete?(file)
@sitemap.rebuild_resource_list!(:removed_file) @app.sitemap.rebuild_resource_list!(:removed_file)
# Force sitemap rebuild so the next request is ready to go. # Force sitemap rebuild so the next request is ready to go.
# Skip this during build because the builder will control sitemap refresh. # Skip this during build because the builder will control sitemap refresh.
@sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build? @app.sitemap.ensure_resource_list_updated! unless waiting_for_ready || @app.build?
end end
# Update the main sitemap resource list # Update the main sitemap resource list
@ -82,8 +74,8 @@ module Middleman
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
resources + @file_paths_on_disk.map do |file| resources + @file_paths_on_disk.map do |file|
::Middleman::Sitemap::Resource.new( ::Middleman::Sitemap::Resource.new(
@sitemap, @app.sitemap,
@sitemap.file_to_path(file), @app.sitemap.file_to_path(file),
File.join(@app.root, file) File.join(@app.root, file)
) )
end end

View file

@ -1,11 +1,14 @@
require 'middleman-core/sitemap/resource'
module Middleman module Middleman
module Sitemap module Sitemap
module Extensions module Extensions
# Manages the list of proxy configurations and manipulates the sitemap # Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations # to include new resources based on those configurations
class Proxies class Proxies < Extension
def initialize(app) def initialize(app, config={}, &block)
@app = app super
@app.add_to_config_context :proxy, &method(:create_proxy) @app.add_to_config_context :proxy, &method(:create_proxy)
@app.define_singleton_method(:proxy, &method(:create_proxy)) @app.define_singleton_method(:proxy, &method(:create_proxy))

View file

@ -5,9 +5,10 @@ module Middleman
module Extensions module Extensions
# Manages the list of proxy configurations and manipulates the sitemap # Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations # to include new resources based on those configurations
class Redirects class Redirects < Extension
def initialize(app) def initialize(app, config={}, &block)
@app = app super
@app.add_to_config_context :redirect, &method(:create_redirect) @app.add_to_config_context :redirect, &method(:create_redirect)
@redirects = {} @redirects = {}

View file

@ -1,11 +1,14 @@
require 'middleman-core/sitemap/resource'
module Middleman module Middleman
module Sitemap module Sitemap
module Extensions module Extensions
class RequestEndpoints class RequestEndpoints < Extension
# Manages the list of proxy configurations and manipulates the sitemap # Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations # to include new resources based on those configurations
def initialize(app) def initialize(app, config={}, &block)
@app = app super
@app.add_to_config_context :endpoint, &method(:create_endpoint) @app.add_to_config_context :endpoint, &method(:create_endpoint)
@endpoints = {} @endpoints = {}

View file

@ -2,12 +2,35 @@
require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/deep_merge'
require 'monitor' require 'monitor'
# Extensions # Ignores
require 'middleman-core/sitemap/extensions/on_disk' Middleman::Extensions.register :sitemap_ignore, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/redirects'
require 'middleman-core/sitemap/extensions/request_endpoints'
require 'middleman-core/sitemap/extensions/proxies'
require 'middleman-core/sitemap/extensions/ignores' require 'middleman-core/sitemap/extensions/ignores'
Middleman::Sitemap::Extensions::Ignores
end
# Files on Disk
Middleman::Extensions.register :sitemap_ondisk, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/on_disk'
Middleman::Sitemap::Extensions::OnDisk
end
# Endpoints
Middleman::Extensions.register :sitemap_endpoint, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/request_endpoints'
Middleman::Sitemap::Extensions::RequestEndpoints
end
# Proxies
Middleman::Extensions.register :sitemap_proxies, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/proxies'
Middleman::Sitemap::Extensions::Proxies
end
# Redirects
Middleman::Extensions.register :sitemap_redirects, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/redirects'
Middleman::Sitemap::Extensions::Redirects
end
module Middleman module Middleman
# Sitemap namespace # Sitemap namespace
@ -34,26 +57,6 @@ module Middleman
@lock = Monitor.new @lock = Monitor.new
reset_lookup_cache! reset_lookup_cache!
# Handle ignore commands
Middleman::Sitemap::Extensions::Ignores.new(@app, self)
# Extensions
{
# Register classes which can manipulate the main site map list
on_disk: Middleman::Sitemap::Extensions::OnDisk.new(@app, self),
# Request Endpoints
request_endpoints: Middleman::Sitemap::Extensions::RequestEndpoints.new(@app),
# Proxies
proxies: Middleman::Sitemap::Extensions::Proxies.new(@app),
# Redirects
redirects: Middleman::Sitemap::Extensions::Redirects.new(@app)
}.each do |k, m|
register_resource_list_manipulator(k, m)
end
@app.config_context.class.send :def_delegator, :app, :sitemap @app.config_context.class.send :def_delegator, :app, :sitemap
end end