2014-07-09 02:02:02 +02:00
require 'middleman-core/sitemap/resource'
2014-06-29 00:07:43 +02:00
require 'middleman-core/core_extensions/collections/step_context'
2014-07-09 02:02:02 +02:00
2012-05-07 23:41:39 +02:00
module Middleman
module Sitemap
module Extensions
2014-01-01 03:21:30 +01:00
# Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations
2016-01-03 02:37:11 +01:00
class Proxies < ConfigExtension
2015-09-15 02:37:35 +02:00
self . resource_list_manipulator_priority = 0
2016-01-12 19:35:12 +01:00
# Expose `proxy`
expose_to_config :proxy
2015-05-02 20:48:21 +02:00
2014-01-01 03:21:30 +01:00
# Setup a proxy from a path to a target
2014-06-17 06:44:14 +02:00
# @param [String] path The new, proxied path to create
# @param [String] target The existing path that should be proxied to. This must be a real resource, not another proxy.
# @option opts [Boolean] ignore Ignore the target from the sitemap (so only the new, proxy resource ends up in the output)
# @option opts [Symbol, Boolean, String] layout The layout name to use (e.g. `:article`) or `false` to disable layout.
# @option opts [Boolean] directory_indexes Whether or not the `:directory_indexes` extension applies to these paths.
# @option opts [Hash] locals Local variables for the template. These will be available when the template renders.
# @option opts [Hash] data Extra metadata to add to the page. This is the same as frontmatter, though frontmatter will take precedence over metadata defined here. Available via {Resource#data}.
2016-01-03 02:37:11 +01:00
# @return [ProxyDescriptor]
Contract String , String , Maybe [ Hash ] = > RespondTo [ :execute_descriptor ]
2016-01-12 19:35:12 +01:00
def proxy ( path , target , opts = { } )
2014-06-29 00:07:43 +02:00
ProxyDescriptor . new (
:: Middleman :: Util . normalize_path ( path ) ,
:: Middleman :: Util . normalize_path ( target ) ,
2016-01-03 02:37:11 +01:00
opts . dup
2014-06-29 00:07:43 +02:00
)
end
2014-01-01 03:21:30 +01:00
end
2012-08-14 00:39:06 +02:00
2014-06-29 00:07:43 +02:00
ProxyDescriptor = Struct . new ( :path , :target , :metadata ) do
2016-01-03 02:37:11 +01:00
def execute_descriptor ( app , resources )
md = metadata . dup
should_ignore = md . delete ( :ignore )
2016-02-22 20:17:21 +01:00
page_data = md . delete ( :data ) || { }
page_data [ :id ] = md . delete ( :id ) if md . key? ( :id )
2016-01-03 02:37:11 +01:00
r = ProxyResource . new ( app . sitemap , path , target )
r . add_metadata (
locals : md . delete ( :locals ) || { } ,
2016-02-22 20:17:21 +01:00
page : page_data || { } ,
2016-01-03 02:37:11 +01:00
options : md
)
if should_ignore
2016-08-13 01:33:18 +02:00
d = :: Middleman :: Sitemap :: Extensions :: Ignores :: StringIgnoreDescriptor . new ( target )
2016-01-03 02:37:11 +01:00
d . execute_descriptor ( app , resources )
2012-05-07 23:41:39 +02:00
end
2016-01-03 02:37:11 +01:00
resources + [ r ]
2012-05-07 23:41:39 +02:00
end
2014-01-01 03:21:30 +01:00
end
2014-07-04 19:38:25 +02:00
end
2012-10-12 06:19:15 +02:00
2015-09-15 02:37:35 +02:00
class Resource
2015-09-17 18:41:17 +02:00
def proxy_to ( _path )
throw 'Resource#proxy_to has been removed. Use ProxyResource class instead.'
2015-09-15 02:37:35 +02:00
end
end
2014-07-04 19:38:25 +02:00
class ProxyResource < :: Middleman :: Sitemap :: Resource
2015-09-15 02:37:35 +02:00
Contract String
attr_reader :target
2014-07-04 19:38:25 +02:00
# Initialize resource with parent store and URL
# @param [Middleman::Sitemap::Store] store
# @param [String] path
2015-09-09 20:55:56 +02:00
# @param [String] target
2014-07-04 19:38:25 +02:00
def initialize ( store , path , target )
super ( store , path )
target = :: Middleman :: Util . normalize_path ( target )
raise " You can't proxy #{ path } to itself! " if target == path
@target = target
end
2012-10-12 06:19:15 +02:00
2014-07-04 19:38:25 +02:00
# The resource for the page this page is proxied to. Throws an exception
# if there is no resource.
# @return [Sitemap::Resource]
2015-04-24 19:26:42 +02:00
Contract IsA [ 'Middleman::Sitemap::Resource' ]
2014-07-04 19:38:25 +02:00
def target_resource
resource = @store . find_resource_by_path ( @target )
unless resource
raise " Path #{ path } proxies to unknown file #{ @target } : #{ @store . resources . map ( & :path ) } "
2014-01-01 03:21:30 +01:00
end
2012-04-04 19:26:07 +02:00
2014-07-04 19:38:25 +02:00
if resource . is_a? ProxyResource
raise " You can't proxy #{ path } to #{ @target } which is itself a proxy. "
end
2012-10-12 06:19:15 +02:00
2014-07-04 19:38:25 +02:00
resource
end
2014-01-01 03:21:30 +01:00
2015-04-24 19:26:42 +02:00
Contract IsA [ 'Middleman::SourceFile' ]
2015-09-09 20:55:56 +02:00
def file_descriptor
target_resource . file_descriptor
2014-07-04 19:38:25 +02:00
end
2012-10-12 06:19:15 +02:00
2015-09-15 02:37:35 +02:00
def metadata
target_resource . metadata . deep_merge super
end
2015-04-24 19:26:42 +02:00
Contract Maybe [ String ]
2014-07-04 19:38:25 +02:00
def content_type
mime_type = super
return mime_type if mime_type
2012-10-12 06:19:15 +02:00
2014-07-04 19:38:25 +02:00
target_resource . content_type
end
2015-09-28 23:52:16 +02:00
def to_s
2015-10-01 22:54:54 +02:00
" # < #{ self . class } path= #{ @path } target= #{ @target } > "
2015-09-28 23:52:16 +02:00
end
2016-01-14 20:21:42 +01:00
alias inspect to_s
2012-04-04 19:26:07 +02:00
end
end
2012-07-08 05:36:37 +02:00
end