Upgrade proxy to be able to take :locals and understand :ignore, and store proxy options and metadata with the proxy list.
This allows users to use proxy directly instead of page to create dynamic pages, and improves performance of dynamic pages for sites that create many proxies. It also allows people to use locals instead of instance variables, which are better for partials and reduce the risk of overwriting Middleman settings.
This commit is contained in:
parent
52819cd239
commit
862551c523
13 changed files with 197 additions and 43 deletions
|
@ -45,9 +45,7 @@ module Middleman
|
|||
# @param [Hash] opts
|
||||
# @return [void]
|
||||
def page(url, opts={}, &block)
|
||||
|
||||
blocks = []
|
||||
blocks << block if block_given?
|
||||
blocks = Array(block)
|
||||
|
||||
# Default layout
|
||||
opts[:layout] = layout if opts[:layout].nil?
|
||||
|
@ -70,20 +68,12 @@ module Middleman
|
|||
end
|
||||
|
||||
# Setup proxy
|
||||
if opts.has_key?(:proxy)
|
||||
proxy(url, opts[:proxy])
|
||||
|
||||
if opts.has_key?(:ignore) && opts[:ignore]
|
||||
ignore(opts[:proxy])
|
||||
opts.delete(:ignore)
|
||||
end
|
||||
|
||||
opts.delete(:proxy)
|
||||
else
|
||||
if opts.has_key?(:ignore) && opts[:ignore]
|
||||
ignore(url)
|
||||
opts.delete(:ignore)
|
||||
end
|
||||
if target = opts.delete(:proxy)
|
||||
# TODO: deprecate proxy through page?
|
||||
proxy(url, target, opts, &block) and return
|
||||
elsif opts.delete(:ignore)
|
||||
# TODO: deprecate ignore through page?
|
||||
ignore(url)
|
||||
end
|
||||
|
||||
# Setup a metadata matcher for rendering those options
|
||||
|
|
|
@ -60,8 +60,6 @@ module Middleman
|
|||
# @param [String, Regexp] path Path glob expression, or path regex
|
||||
# @return [void]
|
||||
def ignore(path=nil, &block)
|
||||
original_callback_size = @ignored_callbacks.size
|
||||
|
||||
if path.is_a? Regexp
|
||||
@ignored_callbacks << Proc.new {|p| p =~ path }
|
||||
elsif path.is_a? String
|
||||
|
|
|
@ -74,40 +74,87 @@ module Middleman
|
|||
@_proxy_manager ||= ProxyManager.new(self)
|
||||
end
|
||||
|
||||
def proxy(*args)
|
||||
proxy_manager.proxy(*args)
|
||||
def proxy(*args, &block)
|
||||
proxy_manager.proxy(*args, &block)
|
||||
end
|
||||
end
|
||||
|
||||
# Manages the list of proxy configurations and manipulates the sitemap
|
||||
# to include new resources based on those configurations
|
||||
class ProxyManager
|
||||
def initialize(app)
|
||||
@app = app
|
||||
|
||||
@proxy_paths = {}
|
||||
@proxy_configs = Set.new
|
||||
end
|
||||
|
||||
# Setup a proxy from a path to a target
|
||||
# @param [String] path
|
||||
# @param [String] target
|
||||
# @param [Hash] opts options to apply to the proxy, including things like
|
||||
# :locals, :ignore to hide the proxy target, :layout, and :directory_indexes.
|
||||
# @return [void]
|
||||
def proxy(path, target)
|
||||
@proxy_paths[::Middleman::Util.normalize_path(path)] = ::Middleman::Util.normalize_path(target)
|
||||
def proxy(path, target, opts={}, &block)
|
||||
metadata = { :options => {}, :locals => {}, :blocks => [] }
|
||||
metadata[:blocks] << block if block_given?
|
||||
metadata[:locals] = opts.delete(:locals) || {}
|
||||
|
||||
@app.ignore(target) if opts.delete(:ignore)
|
||||
metadata[:options] = opts
|
||||
|
||||
@proxy_configs << ProxyConfiguration.new(:path => path, :target => target, :metadata => metadata)
|
||||
|
||||
@app.sitemap.rebuild_resource_list!(:added_proxy)
|
||||
end
|
||||
|
||||
# Update the main sitemap resource list
|
||||
# @return [void]
|
||||
def manipulate_resource_list(resources)
|
||||
resources + @proxy_paths.map do |key, value|
|
||||
resources + @proxy_configs.map do |config|
|
||||
p = ::Middleman::Sitemap::Resource.new(
|
||||
@app.sitemap,
|
||||
key
|
||||
config.path
|
||||
)
|
||||
p.proxy_to(value)
|
||||
p.proxy_to(config.target)
|
||||
p.add_metadata(config.metadata)
|
||||
p
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Configuration for a proxy instance
|
||||
class ProxyConfiguration
|
||||
# The path that this proxy will appear at in the sitemap
|
||||
attr_reader :path
|
||||
def path=(p)
|
||||
@path = ::Middleman::Util.normalize_path(p)
|
||||
end
|
||||
|
||||
# The existing sitemap path that this will proxy to
|
||||
attr_reader :target
|
||||
def target=(t)
|
||||
@target = ::Middleman::Util.normalize_path(t)
|
||||
end
|
||||
|
||||
# Additional metadata like blocks and locals to apply to the proxy
|
||||
attr_accessor :metadata
|
||||
|
||||
# Create a new proxy configuration from hash options
|
||||
def initialize(options={})
|
||||
options.each do |key, value|
|
||||
send "#{key}=", value
|
||||
end
|
||||
end
|
||||
|
||||
# Two configurations are equal if they reference the same path
|
||||
def eql?(other)
|
||||
other.path == path
|
||||
end
|
||||
|
||||
# Two configurations are equal if they reference the same path
|
||||
def hash
|
||||
path.hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -115,19 +115,10 @@ module Middleman
|
|||
# @param [Symbol] origin an indicator of where this metadata came from - only one
|
||||
# block per [matcher, origin] pair may exist.
|
||||
# @return [Array<Array<Proc, Regexp>>]
|
||||
def provides_metadata_for_path(matcher=nil, origin=nil, &block)
|
||||
def provides_metadata_for_path(matcher=nil, &block)
|
||||
@_provides_metadata_for_path ||= []
|
||||
if block_given?
|
||||
if origin
|
||||
existing_provider = @_provides_metadata_for_path.find {|b,m,o| o == origin && m == matcher}
|
||||
end
|
||||
|
||||
if existing_provider
|
||||
existing_provider[0] = block
|
||||
else
|
||||
@_provides_metadata_for_path << [block, matcher, origin]
|
||||
end
|
||||
|
||||
@_provides_metadata_for_path << [block, matcher]
|
||||
@_cached_metadata = {}
|
||||
end
|
||||
@_provides_metadata_for_path
|
||||
|
@ -151,10 +142,7 @@ module Middleman
|
|||
|
||||
metadata = callback.call(request_path)
|
||||
|
||||
if metadata.has_key?(:blocks)
|
||||
result[:blocks] << metadata[:blocks]
|
||||
metadata.delete(:blocks)
|
||||
end
|
||||
result[:blocks] += Array(metadata.delete(:blocks))
|
||||
|
||||
result.deep_merge(metadata)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue