2010-09-05 22:28:38 -04:00
|
|
|
# We're riding on Sinatra, so let's include it.
|
|
|
|
require "sinatra/base"
|
|
|
|
|
|
|
|
module Middleman
|
|
|
|
class Server < Sinatra::Base
|
|
|
|
# Basic Sinatra config
|
|
|
|
set :app_file, __FILE__
|
2011-04-12 23:32:13 -04:00
|
|
|
set :root, ENV["MM_DIR"] || Dir.pwd
|
2010-09-05 22:28:38 -04:00
|
|
|
set :sessions, false
|
|
|
|
set :logging, false
|
|
|
|
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2011-07-07 01:41:12 -04:00
|
|
|
# Pass all request to Middleman, even "static" files
|
2011-05-31 01:33:11 -04:00
|
|
|
set :static, false
|
|
|
|
|
2011-07-07 01:41:12 -04:00
|
|
|
set :views, "source"
|
2011-05-31 01:33:11 -04:00
|
|
|
|
2011-07-06 13:15:31 -04:00
|
|
|
# Activate custom features
|
|
|
|
register Middleman::CoreExtensions::Features
|
|
|
|
|
|
|
|
# Setup custom rendering
|
|
|
|
register Middleman::CoreExtensions::Rendering
|
|
|
|
|
2011-07-06 13:40:17 -04:00
|
|
|
# Setup asset path pipeline
|
|
|
|
register Middleman::CoreExtensions::Assets
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Activate built-in helpers
|
2011-07-06 12:50:34 -04:00
|
|
|
register Middleman::CoreExtensions::DefaultHelpers
|
2010-09-06 21:48:25 -04:00
|
|
|
|
2011-04-14 18:35:41 -04:00
|
|
|
# Activate Yaml Data package
|
2011-07-06 12:50:34 -04:00
|
|
|
register Middleman::CoreExtensions::Data
|
|
|
|
|
2011-07-06 13:46:06 -04:00
|
|
|
# with_layout and page routing
|
|
|
|
register Middleman::CoreExtensions::Routing
|
|
|
|
|
2011-07-07 01:56:20 -04:00
|
|
|
# Parse YAML from templates
|
|
|
|
register Middleman::CoreExtensions::FrontMatter
|
|
|
|
|
2011-07-06 13:15:31 -04:00
|
|
|
set :default_extensions, [
|
|
|
|
:lorem
|
|
|
|
]
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
# Override Sinatra's set to accept a block
|
2011-07-06 13:15:31 -04:00
|
|
|
# Specifically for the asset_host feature
|
2010-09-05 22:28:38 -04:00
|
|
|
def self.set(option, value=self, &block)
|
|
|
|
if block_given?
|
|
|
|
value = Proc.new { block }
|
|
|
|
end
|
|
|
|
|
|
|
|
super(option, value, &nil)
|
|
|
|
end
|
|
|
|
|
2010-09-06 21:48:25 -04:00
|
|
|
# Default layout name
|
2011-07-08 18:01:57 -04:00
|
|
|
set :layout, :layout
|
2010-09-05 22:28:38 -04:00
|
|
|
|
|
|
|
# This will match all requests not overridden in the project's config.rb
|
|
|
|
not_found do
|
|
|
|
process_request
|
|
|
|
end
|
|
|
|
|
2011-07-07 01:41:12 -04:00
|
|
|
# See if Tilt cannot handle this file
|
|
|
|
before do
|
|
|
|
result = resolve_template(request.path_info, :raise_exceptions => false)
|
|
|
|
if result
|
|
|
|
extensionless_path, template_engine = result
|
|
|
|
|
|
|
|
# Return static files
|
|
|
|
if !::Tilt.mappings.has_key?(template_engine.to_s)
|
|
|
|
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
|
|
|
|
status 200
|
|
|
|
send_file File.join(Middleman::Server.views, request.path_info)
|
2011-07-07 19:30:30 -04:00
|
|
|
request["already_sent"] = true
|
2011-07-07 01:41:12 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
$stderr.puts "File not found: #{request.path_info}"
|
|
|
|
status 404
|
2011-07-07 19:30:30 -04:00
|
|
|
request["already_sent"] = true
|
2011-04-10 18:27:18 -04:00
|
|
|
end
|
2011-04-22 14:48:48 -04:00
|
|
|
end
|
2011-07-07 01:41:12 -04:00
|
|
|
|
|
|
|
private
|
2011-04-22 14:48:48 -04:00
|
|
|
# Internal method to look for templates and evaluate them if found
|
|
|
|
def process_request(options={})
|
2011-07-07 19:30:30 -04:00
|
|
|
return if request["already_sent"]
|
|
|
|
|
2011-07-07 01:56:20 -04:00
|
|
|
options.merge!(request['custom_options'] || {})
|
|
|
|
|
2011-07-08 18:01:57 -04:00
|
|
|
old_layout = settings.layout
|
|
|
|
settings.set :layout, options[:layout] if !options[:layout].nil?
|
|
|
|
|
|
|
|
layout = if settings.layout
|
|
|
|
if options[:layout] == false || request.path_info =~ /\.(css|js)$/
|
|
|
|
false
|
|
|
|
else
|
|
|
|
settings.fetch_layout_path(settings.layout).to_sym
|
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2011-04-26 14:49:48 -04:00
|
|
|
|
|
|
|
render_options = { :layout => layout }
|
|
|
|
render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine
|
2011-07-07 01:41:12 -04:00
|
|
|
result = render(request.path_info, render_options)
|
2011-07-08 18:01:57 -04:00
|
|
|
settings.set :layout, old_layout
|
2010-09-06 21:48:25 -04:00
|
|
|
|
|
|
|
if result
|
2011-07-07 01:41:12 -04:00
|
|
|
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
|
2010-09-06 21:48:25 -04:00
|
|
|
status 200
|
2011-07-07 19:30:30 -04:00
|
|
|
body result
|
|
|
|
else
|
|
|
|
status 404
|
2010-09-05 22:28:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-06 13:40:17 -04:00
|
|
|
end
|