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/server.rb

128 lines
3.9 KiB
Ruby
Raw Normal View History

# 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__
set :root, ENV["MM_DIR"] || Dir.pwd
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
# Pass all request to Middleman, even "static" files
set :static, false
set :views, "source"
# 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
# Activate built-in helpers
register Middleman::CoreExtensions::DefaultHelpers
2011-04-14 18:35:41 -04:00
# Activate Yaml Data package
register Middleman::CoreExtensions::Data
2011-07-06 13:46:06 -04:00
# with_layout and page routing
register Middleman::CoreExtensions::Routing
# Parse YAML from templates
register Middleman::CoreExtensions::FrontMatter
set :default_extensions, [
:lorem
]
# Override Sinatra's set to accept a block
# Specifically for the asset_host feature
def self.set(option, value=self, &block)
if block_given?
value = Proc.new { block }
end
super(option, value, &nil)
end
# Default layout name
2011-07-08 18:01:57 -04:00
set :layout, :layout
# This will match all requests not overridden in the project's config.rb
not_found do
process_request
end
# 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
end
else
$stderr.puts "File not found: #{request.path_info}"
status 404
2011-07-07 19:30:30 -04:00
request["already_sent"] = true
end
end
private
# 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"]
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
render_options = { :layout => layout }
render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine
result = render(request.path_info, render_options)
2011-07-08 18:01:57 -04:00
settings.set :layout, old_layout
if result
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200
2011-07-07 19:30:30 -04:00
body result
else
status 404
end
end
end
2011-07-06 13:40:17 -04:00
end