2014-01-01 03:21:30 +01:00
|
|
|
require 'middleman-core/sitemap/resource'
|
2014-07-03 04:04:34 +02:00
|
|
|
require 'middleman-core/contracts'
|
2014-01-01 03:21:30 +01:00
|
|
|
|
2013-06-19 20:13:23 +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-12 19:35:12 +01:00
|
|
|
class Redirects < ConfigExtension
|
2015-09-15 02:37:35 +02:00
|
|
|
self.resource_list_manipulator_priority = 0
|
|
|
|
|
2016-01-12 19:35:12 +01:00
|
|
|
# Expose `redirect`
|
|
|
|
expose_to_config :redirect
|
2015-05-02 20:48:21 +02:00
|
|
|
|
2016-01-12 19:35:12 +01:00
|
|
|
RedirectDescriptor = Struct.new(:path, :to, :template) do
|
|
|
|
def execute_descriptor(app, resources)
|
|
|
|
r = RedirectResource.new(
|
|
|
|
app.sitemap,
|
|
|
|
path,
|
|
|
|
to
|
|
|
|
)
|
|
|
|
r.output = template if template
|
2014-07-09 02:02:02 +02:00
|
|
|
|
2016-01-12 19:35:12 +01:00
|
|
|
resources + [r]
|
|
|
|
end
|
2013-06-19 20:13:23 +02:00
|
|
|
end
|
|
|
|
|
2014-01-01 03:21:30 +01:00
|
|
|
# Setup a redirect from a path to a target
|
|
|
|
# @param [String] path
|
|
|
|
# @param [Hash] opts The :to value gives a target path
|
2016-01-14 20:21:42 +01:00
|
|
|
Contract String, { to: Or[String, ::Middleman::Sitemap::Resource] }, Maybe[Proc] => RedirectDescriptor
|
2016-01-12 19:35:12 +01:00
|
|
|
def redirect(path, opts={}, &block)
|
|
|
|
RedirectDescriptor.new(path, opts[:to], block_given? ? block : nil)
|
2013-06-19 20:13:23 +02:00
|
|
|
end
|
2014-01-01 03:21:30 +01:00
|
|
|
end
|
2013-06-19 20:13:23 +02:00
|
|
|
|
2014-01-01 03:21:30 +01:00
|
|
|
class RedirectResource < ::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
|
2013-06-19 20:13:23 +02:00
|
|
|
|
2014-01-01 03:21:30 +01:00
|
|
|
def initialize(store, path, target)
|
|
|
|
@request_path = target
|
2013-06-19 20:13:23 +02:00
|
|
|
|
2014-01-01 03:21:30 +01:00
|
|
|
super(store, path)
|
|
|
|
end
|
2013-06-19 20:13:23 +02:00
|
|
|
|
2015-04-24 19:26:42 +02:00
|
|
|
Contract Bool
|
2014-01-01 03:21:30 +01:00
|
|
|
def template?
|
|
|
|
true
|
|
|
|
end
|
2013-09-24 16:59:52 +02:00
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract Args[Any] => String
|
2014-04-29 20:43:05 +02:00
|
|
|
def render(*)
|
2014-05-28 09:00:36 +02:00
|
|
|
url = ::Middleman::Util.url_for(@store.app, @request_path,
|
2014-04-29 19:50:21 +02:00
|
|
|
relative: false,
|
|
|
|
find_resource: true
|
2015-04-26 20:13:29 +02:00
|
|
|
)
|
2014-01-01 03:21:30 +01:00
|
|
|
|
|
|
|
if output
|
|
|
|
output.call(path, url)
|
|
|
|
else
|
|
|
|
<<-END
|
|
|
|
<html>
|
|
|
|
<head>
|
2015-09-17 22:53:43 +02:00
|
|
|
<link rel="canonical" href="#{url}" />
|
2014-01-01 03:21:30 +01:00
|
|
|
<meta http-equiv=refresh content="0; url=#{url}" />
|
|
|
|
<meta name="robots" content="noindex,follow" />
|
|
|
|
<meta http-equiv="cache-control" content="no-cache" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
END
|
2013-06-19 20:13:23 +02:00
|
|
|
end
|
2014-01-01 03:21:30 +01:00
|
|
|
end
|
2013-06-19 20:13:23 +02:00
|
|
|
|
2015-04-24 19:26:42 +02:00
|
|
|
Contract Bool
|
2014-01-01 03:21:30 +01:00
|
|
|
def ignored?
|
|
|
|
false
|
|
|
|
end
|
2013-06-19 20:13:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|