2010-09-05 22:28:38 -04:00
|
|
|
# We're riding on Sinatra, so let's include it.
|
|
|
|
require "sinatra/base"
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Use the padrino project's helpers
|
|
|
|
require "padrino-core/application/rendering"
|
|
|
|
require "padrino-helpers"
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
module Middleman
|
|
|
|
class Server < Sinatra::Base
|
|
|
|
# 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
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Import padrino helper methods
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
# Middleman-specific options
|
|
|
|
set :index_file, "index.html" # What file responds to folder requests
|
|
|
|
# Such as the homepage (/) or subfolders (/about/)
|
|
|
|
|
|
|
|
# These directories are passed directly to Compass
|
|
|
|
set :js_dir, "javascripts" # Where to look for javascript files
|
|
|
|
set :css_dir, "stylesheets" # Where to look for CSS files
|
|
|
|
set :images_dir, "images" # Where to look for images
|
|
|
|
set :fonts_dir, "fonts" # Where to look for fonts
|
|
|
|
|
|
|
|
set :build_dir, "build" # Which folder are builds output to
|
|
|
|
set :http_prefix, nil # During build, add a prefix for absolute paths
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Use Padrino Helpers
|
|
|
|
register Padrino::Helpers
|
|
|
|
set :asset_stamp, false # Disable Padrino cache buster until explicitly enabled
|
2010-09-05 22:28:38 -04:00
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Activate custom features
|
|
|
|
register Middleman::Features
|
2010-09-05 22:28:38 -04:00
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Activate built-in helpers
|
|
|
|
register Middleman::Features::DefaultHelpers
|
|
|
|
|
2011-01-18 18:01:17 -05:00
|
|
|
# Activate Lorem helpers
|
|
|
|
register Middleman::Features::Lorem
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Tilt-aware renderer
|
|
|
|
register Padrino::Rendering
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# An array of callback procs to run after all features have been setup
|
|
|
|
@@run_after_features = []
|
|
|
|
|
|
|
|
# Add a block/proc to be run after features have been setup
|
|
|
|
def self.after_feature_init(&block)
|
|
|
|
@@run_after_features << block
|
|
|
|
end
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Activate custom renderers
|
|
|
|
register Middleman::Renderers::Haml
|
|
|
|
register Middleman::Renderers::Sass
|
|
|
|
|
2010-09-05 22:28:38 -04:00
|
|
|
# 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
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Default layout name
|
|
|
|
layout :layout
|
|
|
|
|
|
|
|
def self.current_layout
|
|
|
|
@layout
|
|
|
|
end
|
2010-09-05 22:28:38 -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
|
2010-09-06 21:48:25 -04:00
|
|
|
def self.with_layout(layout_name, &block)
|
|
|
|
old_layout = current_layout
|
|
|
|
|
|
|
|
layout(layout_name)
|
2010-09-05 22:28:38 -04:00
|
|
|
class_eval(&block) if block_given?
|
|
|
|
ensure
|
2010-09-06 21:48:25 -04:00
|
|
|
layout(old_layout)
|
2010-09-05 22:28:38 -04:00
|
|
|
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)
|
2010-09-06 21:48:25 -04:00
|
|
|
url << settings.index_file if url.match(%r{/$})
|
|
|
|
|
2011-01-01 17:00:09 -05:00
|
|
|
options[:layout] = current_layout if options[:layout].nil?
|
2010-09-05 22:28:38 -04:00
|
|
|
get(url) do
|
|
|
|
return yield if block_given?
|
2010-09-06 21:48:25 -04:00
|
|
|
process_request(options)
|
2010-09-05 22:28:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This will match all requests not overridden in the project's config.rb
|
|
|
|
not_found do
|
|
|
|
process_request
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# Internal method to look for templates and evaluate them if found
|
2010-09-06 21:48:25 -04:00
|
|
|
def process_request(options={})
|
2010-09-05 22:28:38 -04:00
|
|
|
# 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{^/}, '')
|
2010-09-06 21:48:25 -04:00
|
|
|
|
|
|
|
old_layout = settings.current_layout
|
|
|
|
settings.layout(options[:layout]) if !options[:layout].nil?
|
2011-01-01 17:00:09 -05:00
|
|
|
layout = settings.fetch_layout_path.to_sym
|
|
|
|
layout = false if options[:layout] == false or path =~ /\.(css|js)$/
|
|
|
|
result = render(path, :layout => layout, :layout_engine => options[:layout_engine])
|
2010-09-06 21:48:25 -04:00
|
|
|
settings.layout(old_layout)
|
|
|
|
|
|
|
|
if result
|
2010-09-05 22:28:38 -04:00
|
|
|
content_type mime_type(File.extname(path)), :charset => 'utf-8'
|
2010-09-06 21:48:25 -04:00
|
|
|
status 200
|
|
|
|
return result
|
2010-09-05 22:28:38 -04:00
|
|
|
end
|
|
|
|
|
2010-09-21 21:30:17 -04:00
|
|
|
status 404
|
|
|
|
rescue Padrino::Rendering::TemplateNotFound
|
|
|
|
$stderr.puts "File not found: #{request.path}"
|
2010-09-05 22:28:38 -04:00
|
|
|
status 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
require "middleman/assets"
|
|
|
|
|
|
|
|
# The Rack App
|
|
|
|
class Middleman::Server
|
|
|
|
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
|
|
|
|
|
2011-02-06 13:57:28 -05:00
|
|
|
use ::Rack::ConditionalGet
|
|
|
|
use ::Rack::Static, :urls => ["/#{self.images_dir}"], :root => "public"
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
@@run_after_features.each { |block| class_eval(&block) }
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|