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
|
|
|
|
2009-07-29 20:58:28 +02:00
|
|
|
# Include content_for support
|
|
|
|
require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-content-for', 'lib', 'sinatra', 'content_for')
|
|
|
|
|
2009-07-28 01:25:32 +02:00
|
|
|
class Middleman < Sinatra::Base
|
|
|
|
set :app_file, __FILE__
|
2009-07-29 19:39:00 +02:00
|
|
|
set :static, true
|
|
|
|
set :root, Dir.pwd
|
|
|
|
set :environment, defined?(MIDDLEMAN_BUILDER) ? :build : :development
|
2009-07-31 18:35:12 +02:00
|
|
|
|
2009-07-28 18:39:06 +02:00
|
|
|
helpers Sinatra::Markaby
|
|
|
|
helpers Sinatra::Maruku
|
2009-07-29 20:58:28 +02:00
|
|
|
helpers Sinatra::ContentFor
|
2009-07-31 18:35:12 +02:00
|
|
|
|
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
|
2009-08-04 21:18:05 +02:00
|
|
|
config.sass_dir = File.join(File.basename(self.views), "stylesheets")
|
2009-07-28 01:25:32 +02:00
|
|
|
config.output_style = :nested
|
2009-08-12 01:26:08 +02:00
|
|
|
config.css_dir = File.join(File.basename(self.public), "stylesheets")
|
2009-07-30 23:57:59 +02:00
|
|
|
config.images_dir = File.join(File.basename(self.public), "images")
|
2009-08-12 01:26:08 +02:00
|
|
|
config.http_path = "/"
|
|
|
|
config.http_images_path = "/images"
|
|
|
|
config.http_stylesheets_path = "/stylesheets"
|
2009-08-04 21:18:05 +02:00
|
|
|
config.add_import_path(config.sass_dir)
|
2009-07-28 01:25:32 +02:00
|
|
|
end
|
2009-08-05 00:28:25 +02:00
|
|
|
end
|
|
|
|
|
2009-08-05 01:40:33 +02:00
|
|
|
# include helpers
|
|
|
|
class_eval File.read(File.join(File.dirname(__FILE__), 'middleman', 'helpers.rb'))
|
|
|
|
|
2009-08-05 00:28:25 +02:00
|
|
|
# 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 21:18:05 +02:00
|
|
|
Compass.configure_sass_plugin!
|
2009-07-29 19:39:00 +02:00
|
|
|
end
|
|
|
|
|
2009-08-09 22:10:09 +02:00
|
|
|
# CSS files
|
|
|
|
get %r{/(.*).css} do |path|
|
|
|
|
content_type 'text/css', :charset => 'utf-8'
|
|
|
|
begin
|
2009-08-12 01:26:08 +02:00
|
|
|
location_of_sass_file = defined?(MIDDLEMAN_BUILDER) ? "build" : "views"
|
|
|
|
css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info
|
|
|
|
sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename }))
|
2009-08-09 22:10:09 +02:00
|
|
|
rescue Exception => e
|
|
|
|
sass_exception_string(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-28 18:39:06 +02:00
|
|
|
get /(.*)/ do |path|
|
|
|
|
path << "index.html" if path.match(%r{/$})
|
|
|
|
path.gsub!(%r{^/}, '')
|
2009-08-11 20:21:49 +02:00
|
|
|
path.gsub!(File.extname(path), '')
|
2009-07-28 22:44:56 +02:00
|
|
|
|
|
|
|
result = nil
|
2009-08-09 22:10:09 +02:00
|
|
|
begin
|
|
|
|
%w(haml erb builder maruku mab).detect do |renderer|
|
2009-08-11 20:21:49 +02:00
|
|
|
next false if !File.exists?(File.join(options.views, "#{path}.#{renderer}"))
|
2009-08-09 22:10:09 +02:00
|
|
|
renderer = "markaby" if renderer == "mab"
|
2009-08-11 20:21:49 +02:00
|
|
|
result = send(renderer.to_sym, path.to_sym)
|
2009-07-28 18:39:06 +02:00
|
|
|
end
|
2009-08-09 22:10:09 +02:00
|
|
|
rescue Haml::Error => e
|
|
|
|
result = "Haml Error: #{e}"
|
|
|
|
#result << "<pre>Backtrace: #{e.backtrace.join("\n")}</pre>"
|
2009-07-28 01:25:32 +02:00
|
|
|
end
|
2009-07-28 22:44:56 +02:00
|
|
|
|
|
|
|
result || pass
|
2009-07-28 01:25:32 +02:00
|
|
|
end
|
2009-07-31 18:35:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
require File.join(File.dirname(__FILE__), 'middleman', 'helpers')
|