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

95 lines
2.9 KiB
Ruby
Raw Normal View History

2009-07-27 19:25:32 -04:00
require 'rubygems'
require 'haml'
require 'compass' #must be loaded before sinatra
require 'sinatra/base'
2009-07-28 11:42:21 -04:00
# Include markaby support
2009-07-28 12:39:06 -04:00
require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-markaby', 'lib', 'sinatra', 'markaby')
# Include maruku support
require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-maruku', 'lib', 'sinatra', 'maruku')
2009-07-27 19:25:32 -04:00
2009-07-29 14:58:28 -04:00
# Include content_for support
require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-content-for', 'lib', 'sinatra', 'content_for')
2009-07-27 19:25:32 -04:00
class Middleman < Sinatra::Base
set :app_file, __FILE__
2009-07-29 13:39:00 -04:00
set :static, true
set :root, Dir.pwd
set :environment, defined?(MIDDLEMAN_BUILDER) ? :build : :development
2009-07-31 12:35:12 -04:00
2009-07-28 12:39:06 -04:00
helpers Sinatra::Markaby
helpers Sinatra::Maruku
2009-07-29 14:58:28 -04:00
helpers Sinatra::ContentFor
2009-07-31 12:35:12 -04:00
2009-07-27 19:25:32 -04:00
def self.run!(options={}, &block)
set options
handler = detect_rack_handler
handler_name = handler.name.gsub(/.*::/, '')
puts "== The Middleman is standing watch on port #{port}"
handler.run self, :Host => host, :Port => port do |server|
trap(:INT) do
## Use thins' hard #stop! if available, otherwise just #stop
server.respond_to?(:stop!) ? server.stop! : server.stop
puts "\n== The Middleman has ended his patrol"
end
if block_given?
block.call
## Use thins' hard #stop! if available, otherwise just #stop
server.respond_to?(:stop!) ? server.stop! : server.stop
end
end
rescue Errno::EADDRINUSE => e
puts "== The Middleman is already standing watch on port #{port}!"
end
configure do
Compass.configuration do |config|
config.project_path = Dir.pwd
2009-08-04 15:18:05 -04:00
config.sass_dir = File.join(File.basename(self.views), "stylesheets")
2009-07-27 19:25:32 -04:00
config.output_style = :nested
2009-07-30 17:57:59 -04:00
config.images_dir = File.join(File.basename(self.public), "images")
2009-07-27 19:25:32 -04:00
config.http_images_path = "/images/"
2009-08-04 15:18:05 -04:00
config.add_import_path(config.sass_dir)
2009-07-27 19:25:32 -04:00
end
2009-08-04 18:28:25 -04:00
end
# Check for local config
local_config = File.join(self.root, "init.rb")
if File.exists? local_config
puts "== Local config at: #{local_config}"
class_eval File.read(local_config)
end
configure do
2009-08-04 15:18:05 -04:00
Compass.configure_sass_plugin!
2009-07-29 13:39:00 -04:00
end
2009-07-28 12:39:06 -04:00
get /(.*)/ do |path|
path << "index.html" if path.match(%r{/$})
path.gsub!(%r{^/}, '')
template = path.gsub(File.extname(path), '').to_sym
2009-07-30 17:57:59 -04:00
@full_request_path = path
2009-07-28 16:44:56 -04:00
result = nil
%w(haml erb builder maruku mab sass).each do |renderer|
next if !File.exists?(File.join(options.views, "#{template}.#{renderer}"))
renderer = "markaby" if renderer == "mab"
result = if renderer == "sass"
content_type 'text/css', :charset => 'utf-8'
sass(template, Compass.sass_engine_options)
2009-07-28 14:06:45 -04:00
else
2009-07-28 16:44:56 -04:00
send(renderer.to_sym, template)
2009-07-28 12:39:06 -04:00
end
2009-07-28 16:44:56 -04:00
break
2009-07-27 19:25:32 -04:00
end
2009-07-28 16:44:56 -04:00
result || pass
2009-07-27 19:25:32 -04:00
end
2009-07-31 12:35:12 -04:00
end
require File.join(File.dirname(__FILE__), 'middleman', 'helpers')