middleman/lib/middleman.rb

71 lines
2.3 KiB
Ruby
Raw Normal View History

2009-07-28 01:25:32 +02:00
require 'rubygems'
require 'haml'
require 'compass' #must be loaded before sinatra
require 'sinatra/base'
2009-07-28 17:42:21 +02:00
# Include markaby support
2009-07-28 18:39:06 +02: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-28 01:25:32 +02:00
class Middleman < Sinatra::Base
set :app_file, __FILE__
2009-07-28 18:39:06 +02:00
helpers Sinatra::Markaby
helpers Sinatra::Maruku
2009-07-28 01:25:32 +02: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
config.sass_dir = File.join(File.expand_path(self.views), "stylesheets")
config.output_style = :nested
config.images_dir = File.join(File.expand_path(self.public), "images")
config.http_images_path = "/images/"
end
end
2009-07-28 18:39:06 +02:00
get /(.*)/ do |path|
path << "index.html" if path.match(%r{/$})
path.gsub!(%r{^/}, '')
template = path.gsub(File.extname(path), '').to_sym
2009-07-28 01:25:32 +02:00
if path.match /.html$/
2009-07-28 18:39:06 +02:00
if File.exists? File.join(options.views, "#{template}.haml")
haml(template)
elsif File.exists? File.join(options.views, "#{template}.maruku")
maruku(template)
2009-07-28 20:06:45 +02:00
elsif File.exists? File.join(options.views, "#{template}.mab")
2009-07-28 18:39:06 +02:00
markaby(template)
2009-07-28 20:06:45 +02:00
else
pass
2009-07-28 18:39:06 +02:00
end
2009-07-28 01:25:32 +02:00
elsif path.match /.css$/
content_type 'text/css', :charset => 'utf-8'
2009-07-28 18:39:06 +02:00
sass(template, Compass.sass_engine_options)
else
2009-07-28 01:25:32 +02:00
pass
end
end
end