2011-07-06 13:46:06 -04:00
|
|
|
module Middleman::CoreExtensions::Routing
|
|
|
|
class << self
|
|
|
|
def registered(app)
|
|
|
|
app.extend ClassMethods
|
|
|
|
end
|
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
2011-11-08 18:38:15 -05:00
|
|
|
module ClassMethods
|
2011-07-07 01:41:12 -04:00
|
|
|
def path_to_index(path)
|
|
|
|
parts = path ? path.split('/') : []
|
|
|
|
if parts.last.nil? || parts.last.split('.').length == 1
|
2011-11-08 18:38:15 -05:00
|
|
|
path = File.join(path, index_file)
|
2011-07-07 01:41:12 -04:00
|
|
|
end
|
|
|
|
path.gsub(%r{^/}, '')
|
|
|
|
end
|
2011-07-06 13:46:06 -04:00
|
|
|
|
|
|
|
# Takes a block which allows many pages to have the same layout
|
|
|
|
# with_layout :admin do
|
|
|
|
# page "/admin/"
|
|
|
|
# page "/admin/login.html"
|
|
|
|
# end
|
|
|
|
def with_layout(layout_name, &block)
|
2011-11-08 18:38:15 -05:00
|
|
|
old_layout = layout
|
2011-07-06 13:46:06 -04:00
|
|
|
|
2011-07-08 18:01:57 -04:00
|
|
|
set :layout, layout_name
|
2011-07-06 13:46:06 -04:00
|
|
|
class_eval(&block) if block_given?
|
|
|
|
ensure
|
2011-07-08 18:01:57 -04:00
|
|
|
set :layout, old_layout
|
2011-07-06 13:46:06 -04:00
|
|
|
end
|
|
|
|
|
2011-07-24 01:21:52 -04:00
|
|
|
def paths_for_url(url)
|
2011-11-08 18:38:15 -05:00
|
|
|
url = url.gsub(%r{\/#{index_file}$}, "")
|
2011-07-06 13:46:06 -04:00
|
|
|
url = url.gsub(%r{(\/)$}, "") if url.length > 1
|
|
|
|
|
|
|
|
paths = [url]
|
|
|
|
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
|
2011-07-14 15:32:39 -04:00
|
|
|
paths << "/#{path_to_index(url)}"
|
2011-07-24 01:21:52 -04:00
|
|
|
paths
|
|
|
|
end
|
2011-07-27 17:14:22 -04:00
|
|
|
|
2011-07-24 01:21:52 -04:00
|
|
|
# The page method allows the layout to be set on a specific path
|
|
|
|
# page "/about.html", :layout => false
|
|
|
|
# page "/", :layout => :homepage_layout
|
|
|
|
def page(url, options={}, &block)
|
2011-07-21 19:55:18 -04:00
|
|
|
has_block = block_given?
|
2011-11-08 18:38:15 -05:00
|
|
|
options[:layout] = layout if options[:layout].nil?
|
2011-07-24 01:21:52 -04:00
|
|
|
|
|
|
|
if options.has_key?(:proxy)
|
2011-11-08 18:38:15 -05:00
|
|
|
sitemap.set_path(url, options[:proxy])
|
2011-11-07 20:58:07 -05:00
|
|
|
|
2011-07-27 17:14:22 -04:00
|
|
|
if options.has_key?(:ignore) && options[:ignore]
|
2011-11-08 18:38:15 -05:00
|
|
|
ignore(options[:proxy])
|
2011-07-27 17:14:22 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if options.has_key?(:ignore) && options[:ignore]
|
2011-11-08 18:38:15 -05:00
|
|
|
ignore(url)
|
2011-07-27 17:14:22 -04:00
|
|
|
end
|
2011-07-24 01:21:52 -04:00
|
|
|
end
|
2011-09-09 02:06:22 -04:00
|
|
|
|
2011-07-24 01:21:52 -04:00
|
|
|
paths_for_url(url).each do |p|
|
2011-07-06 13:46:06 -04:00
|
|
|
get(p) do
|
2011-11-07 20:58:07 -05:00
|
|
|
if settings.sitemap.path_is_proxy?(url)
|
|
|
|
request["is_proxy"] = true
|
|
|
|
request.path_info = settings.sitemap.path_target(url)
|
|
|
|
end
|
|
|
|
|
2011-07-21 19:55:18 -04:00
|
|
|
instance_eval(&block) if has_block
|
2011-07-06 13:46:06 -04:00
|
|
|
process_request(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|