class Middleman::Server

def self.after_feature_init(&block)

Add a block/proc to be run after features have been setup
def self.after_feature_init(&block)
  @@run_after_features << block
end

def self.current_layout

def self.current_layout
  @layout
end

def self.mime(ext, type)

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

def self.new(*args, &block)

def self.new(*args, &block)
  # If the old init.rb exists, use it, but issue warning
  old_config = File.join(self.root, "init.rb")
  if File.exists? old_config
    $stderr.puts "== Warning: The init.rb file has been renamed to config.rb"
    local_config = old_config
  end
  
  # Check for and evaluate local configuration
  local_config ||= File.join(self.root, "config.rb")
  if File.exists? local_config
    $stderr.puts "== Reading:  Local config" if logging?
    Middleman::Server.class_eval File.read(local_config)
    set :app_file, File.expand_path(local_config)
  end
  
  use ::Rack::ConditionalGet if environment == :development
  
  @@run_after_features.each { |block| class_eval(&block) }
  
  super
end

def self.page(url, options={}, &block)

page "/", :layout => :homepage_layout
page "/about.html", :layout => false
The page method allows the layout to be set on a specific path
def self.page(url, options={}, &block)
  url << settings.index_file if url.match(%r{/$})
  options[:layout] ||= current_layout
  get(url) do
    return yield if block_given?
    process_request(options)
  end
end

def self.set(option, value=self, &block)

Override Sinatra's set to accept a block
def self.set(option, value=self, &block)
  if block_given?
    value = Proc.new { block }
  end
  
  super(option, value, &nil)
end

def self.with_layout(layout_name, &block)

end
page "/admin/login.html"
page "/admin/"
with_layout :admin do
Takes a block which allows many pages to have the same layout
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

def process_request(options={})

Internal method to look for templates and evaluate them if found
def process_request(options={})
  # Normalize the path and add index if we're looking at a directory
  path = request.path
  path << settings.index_file if path.match(%r{/$})
  path.gsub!(%r{^/}, '')
  
  old_layout = settings.current_layout
  settings.layout(options[:layout]) if !options[:layout].nil?
  result = render(path, :layout => settings.fetch_layout_path.to_sym)
  settings.layout(old_layout)
  
  if result
    content_type mime_type(File.extname(path)), :charset => 'utf-8'
    status 200
    return result
  end
  
  status 404
rescue Padrino::Rendering::TemplateNotFound
  $stderr.puts "File not found: #{request.path}"
  status 404
end