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

93 lines
2.5 KiB
Ruby
Raw Normal View History

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
class Redirects < Extension
def initialize(app, config={}, &block)
super
2014-07-16 03:01:45 +02:00
@app.add_to_config_context(:redirect, &method(:create_redirect))
2013-06-19 20:13:23 +02:00
2014-01-01 03:21:30 +01:00
@redirects = {}
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
2014-07-03 04:04:34 +02:00
Contract String, ({ to: Or[String, IsA['Middleman::Sitemap::Resource']] }), Proc => Any
2014-01-01 03:21:30 +01:00
def create_redirect(path, opts={}, &block)
2014-04-29 20:43:05 +02:00
opts[:template] = block if block_given?
2013-06-19 20:13:23 +02:00
2014-01-01 03:21:30 +01:00
@redirects[path] = opts
2013-06-19 20:13:23 +02:00
2014-01-01 03:21:30 +01:00
@app.sitemap.rebuild_resource_list!(:added_redirect)
end
2013-06-19 20:13:23 +02:00
2014-01-01 03:21:30 +01:00
# Update the main sitemap resource list
2014-07-03 04:04:34 +02:00
# @return Array<Middleman::Sitemap::Resource>
Contract ResourceList => ResourceList
2014-01-01 03:21:30 +01:00
def manipulate_resource_list(resources)
resources + @redirects.map do |path, opts|
r = RedirectResource.new(
@app.sitemap,
path,
opts[:to]
)
r.output = opts[:template] if opts[:template]
r
2013-06-19 20:13:23 +02:00
end
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
2014-07-03 04:04:34 +02:00
Contract None => 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
2014-07-03 04:04:34 +02:00
Contract None => 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-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
)
2014-01-01 03:21:30 +01:00
if output
output.call(path, url)
else
<<-END
<html>
<head>
<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
2014-07-03 04:04:34 +02:00
Contract None => 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