diff --git a/lib/middleman.rb b/lib/middleman.rb index d9749809..2262304e 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -91,6 +91,9 @@ module Middleman # Extended version of Padrino's rendering autoload :Rendering, "middleman/core_extensions/rendering" + + # Pass custom options to views + autoload :Routing, "middleman/core_extensions/routing" end module Features diff --git a/lib/middleman/core_extensions/routing.rb b/lib/middleman/core_extensions/routing.rb new file mode 100644 index 00000000..73b0443a --- /dev/null +++ b/lib/middleman/core_extensions/routing.rb @@ -0,0 +1,49 @@ +module Middleman::CoreExtensions::Routing + class << self + def registered(app) + app.extend ClassMethods + end + alias :included :registered + end + + module ClassMethods + def current_layout + @layout + end + + # 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) + old_layout = current_layout + + layout(layout_name) + class_eval(&block) if block_given? + ensure + layout(old_layout) + 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) + 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)}" + + options[:layout] = current_layout if options[:layout].nil? + + paths.each do |p| + get(p) do + return yield if block_given? + process_request(options) + end + end + end + end +end \ No newline at end of file diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index a35a1c6b..34d7647b 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -6,7 +6,6 @@ module Middleman # Basic Sinatra config set :app_file, __FILE__ set :root, ENV["MM_DIR"] || Dir.pwd - set :reload, false set :sessions, false set :logging, false set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development @@ -48,6 +47,9 @@ module Middleman # Parse YAML from templates register Middleman::CoreExtensions::FrontMatter + # with_layout and page routing + register Middleman::CoreExtensions::Routing + set :default_extensions, [ :lorem ] @@ -60,55 +62,10 @@ module Middleman end super(option, value, &nil) - end - - # Rack helper for adding mime-types during local preview - def self.mime(ext, type) - ext = ".#{ext}" unless ext.to_s[0] == ?. - ::Rack::Mime::MIME_TYPES[ext.to_s] = type end # Default layout name layout :layout - - def self.current_layout - @layout - end - - # Takes a block which allows many pages to have the same layout - # with_layout :admin do - # page "/admin/" - # page "/admin/login.html" - # end - def self.with_layout(layout_name, &block) - old_layout = current_layout - - layout(layout_name) - class_eval(&block) if block_given? - ensure - layout(old_layout) - end - - # The page method allows the layout to be set on a specific path - # page "/about.html", :layout => false - # page "/", :layout => :homepage_layout - def self.page(url, options={}, &block) - 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)}" - - options[:layout] = current_layout if options[:layout].nil? - - paths.each do |p| - get(p) do - return yield if block_given? - process_request(options) - end - end - end # This will match all requests not overridden in the project's config.rb not_found do