1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/core_extensions/routing.rb

73 lines
2 KiB
Ruby
Raw Normal View History

2011-07-06 13:46:06 -04:00
module Middleman::CoreExtensions::Routing
class << self
def registered(app)
app.extend ClassMethods
app.set :proxied_paths, {}
# Normalize the path and add index if we're looking at a directory
app.before_processing do
request.path_info = self.class.path_to_index(request.path)
true
end
2011-07-06 13:46:06 -04:00
end
alias :included :registered
end
2011-07-08 18:01:57 -04:00
module ClassMethods
def path_to_index(path)
parts = path ? path.split('/') : []
if parts.last.nil? || parts.last.split('.').length == 1
path = File.join(path, settings.index_file)
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-07-08 18:01:57 -04:00
old_layout = settings.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
def paths_for_url(url)
2011-07-06 13:46:06 -04:00
url = url.gsub(%r{#{settings.index_file}$}, "")
url = url.gsub(%r{(\/)$}, "") if url.length > 1
paths = [url]
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
paths << "/#{path_to_index(url)}"
paths
end
# 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?
options[:layout] = settings.layout if options[:layout].nil?
if options.has_key?(:proxy)
settings.proxied_paths[url] = options[:proxy]
end
paths_for_url(url).each do |p|
2011-07-06 13:46:06 -04:00
get(p) do
if settings.proxied_paths.has_key?(url)
request.path_info = settings.proxied_paths[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