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}>"
end
alias_method :inspect, :to_s # Ruby 2.0 calls inspect for NoMethodError instead of to_s
end
end

View file

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

View file

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

View file

@ -3,30 +3,17 @@ require 'set'
module Middleman
module Sitemap
module Extensions
class OnDisk
attr_accessor :sitemap
class OnDisk < Extension
attr_accessor :waiting_for_ready
def initialize(app, sitemap)
@sitemap = sitemap
@app = app
def initialize(app, config={}, &block)
super
@file_paths_on_disk = Set.new
scoped_self = self
@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
scoped_self.waiting_for_ready = false
# Make sure the sitemap is ready for the first request
@ -34,13 +21,18 @@ module Middleman
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
# @param [String] file
# @return [Boolean]
def touch_file(file)
return false if File.directory?(file)
path = @sitemap.file_to_path(file)
path = @app.sitemap.file_to_path(file)
return false unless path
ignored = @app.config[:ignored_sitemap_matchers].any? do |_, callback|
@ -57,11 +49,11 @@ module Middleman
# in case one of the other manipulators
# (like asset_hash) cares about the contents of this file,
# 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.
# 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
# Remove a file from the store
@ -70,11 +62,11 @@ module Middleman
def remove_file(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.
# 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
# Update the main sitemap resource list
@ -82,8 +74,8 @@ module Middleman
def manipulate_resource_list(resources)
resources + @file_paths_on_disk.map do |file|
::Middleman::Sitemap::Resource.new(
@sitemap,
@sitemap.file_to_path(file),
@app.sitemap,
@app.sitemap.file_to_path(file),
File.join(@app.root, file)
)
end

View file

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

View file

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

View file

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

View file

@ -2,12 +2,35 @@
require 'active_support/core_ext/hash/deep_merge'
require 'monitor'
# Extensions
require 'middleman-core/sitemap/extensions/on_disk'
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'
# Ignores
Middleman::Extensions.register :sitemap_ignore, auto_activate: :before_configuration do
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
# Sitemap namespace
@ -34,26 +57,6 @@ module Middleman
@lock = Monitor.new
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
end