middleman/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb

69 lines
1.7 KiB
Ruby
Raw Normal View History

require 'middleman-core/sitemap/resource'
module Middleman
module Sitemap
module Extensions
class RequestEndpoints < ConfigExtension
2015-09-15 02:37:35 +02:00
self.resource_list_manipulator_priority = 0
# Expose `endpoint`
expose_to_config :endpoint
EndpointDescriptor = Struct.new(:path, :request_path, :block) do
def execute_descriptor(app, resources)
r = EndpointResource.new(
app.sitemap,
path,
request_path
)
r.output = block if block
resources + [r]
end
2014-01-01 03:21:30 +01:00
end
2014-01-01 03:21:30 +01:00
# Setup a proxy from a path to a target
# @param [String] path
# @param [Hash] opts The :path value gives a request path if it
# differs from the output path
Contract String, Or[{ path: String }, Proc] => EndpointDescriptor
def endpoint(path, opts={}, &block)
2014-01-01 03:21:30 +01:00
if block_given?
EndpointDescriptor.new(path, path, block)
2014-01-01 03:21:30 +01:00
else
EndpointDescriptor.new(path, opts[:path] || path, nil)
end
end
2014-01-01 03:21:30 +01:00
end
2014-01-01 03:21:30 +01:00
class EndpointResource < ::Middleman::Sitemap::Resource
2015-04-24 19:26:42 +02:00
Contract Maybe[Proc]
2014-01-01 03:21:30 +01:00
attr_accessor :output
2014-05-31 07:46:15 +02:00
def initialize(store, path, request_path)
2014-01-01 03:21:30 +01:00
super(store, path)
2014-05-31 07:46:15 +02:00
@request_path = ::Middleman::Util.normalize_path(request_path)
2014-01-01 03:21:30 +01:00
end
2013-11-25 09:12:39 +01:00
2015-04-24 19:26:42 +02:00
Contract String
2014-05-31 07:46:15 +02:00
attr_reader :request_path
2015-04-24 19:26:42 +02:00
Contract Bool
2014-01-01 03:21:30 +01:00
def template?
true
end
2014-07-03 04:04:34 +02:00
Contract Args[Any] => String
2014-04-29 20:43:05 +02:00
def render(*)
2014-04-29 19:50:21 +02:00
return output.call if output
2014-01-01 03:21:30 +01:00
end
2015-04-24 19:26:42 +02:00
Contract Bool
2014-01-01 03:21:30 +01:00
def ignored?
false
end
end
end
end
end