2009-07-27 19:25:32 -04:00
|
|
|
require 'haml'
|
|
|
|
require 'compass' #must be loaded before sinatra
|
|
|
|
require 'sinatra/base'
|
2009-07-28 11:42:21 -04:00
|
|
|
|
2009-09-17 13:18:13 -04:00
|
|
|
require 'sprockets'
|
2009-09-24 19:35:03 -04:00
|
|
|
# Sprockets ruby 1.9 hack
|
|
|
|
require 'middleman/sprockets+ruby19'
|
2009-09-17 12:40:29 -04:00
|
|
|
|
2009-09-21 13:09:41 -04:00
|
|
|
require "yui/compressor"
|
|
|
|
|
2009-07-29 14:58:28 -04:00
|
|
|
# Include content_for support
|
2009-09-24 19:35:03 -04:00
|
|
|
require 'sinatra-content-for'
|
2009-07-29 14:58:28 -04:00
|
|
|
|
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-09-18 12:59:07 -04:00
|
|
|
set :default_ext, 'html'
|
2009-08-12 13:00:06 -04:00
|
|
|
set :supported_formats, %w(haml erb builder)
|
|
|
|
|
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-08-11 19:26:08 -04:00
|
|
|
config.css_dir = File.join(File.basename(self.public), "stylesheets")
|
2009-07-30 17:57:59 -04:00
|
|
|
config.images_dir = File.join(File.basename(self.public), "images")
|
2009-08-11 19:26:08 -04:00
|
|
|
config.http_images_path = "/images"
|
|
|
|
config.http_stylesheets_path = "/stylesheets"
|
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
|
|
|
|
|
2009-08-04 19:40:33 -04:00
|
|
|
# include helpers
|
|
|
|
class_eval File.read(File.join(File.dirname(__FILE__), 'middleman', 'helpers.rb'))
|
|
|
|
|
2009-08-04 18:28:25 -04: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 15:18:05 -04:00
|
|
|
Compass.configure_sass_plugin!
|
2009-07-29 13:39:00 -04:00
|
|
|
end
|
|
|
|
|
2009-09-21 13:09:41 -04:00
|
|
|
configure :build do
|
2009-09-21 13:16:16 -04:00
|
|
|
Compass.configuration do |config|
|
|
|
|
config.output_style = :compressed
|
|
|
|
end
|
|
|
|
|
2009-09-21 13:09:41 -04:00
|
|
|
module Minified
|
|
|
|
module Javascript
|
|
|
|
include ::Haml::Filters::Base
|
|
|
|
def render_with_options(text, options)
|
|
|
|
compressor = ::YUI::JavaScriptCompressor.new(:munge => true)
|
2009-09-24 19:24:41 -04:00
|
|
|
data = compressor.compress(text)
|
|
|
|
<<END
|
|
|
|
<script type=#{options[:attr_wrapper]}text/javascript#{options[:attr_wrapper]}>#{data.chomp}</script>
|
|
|
|
END
|
2009-09-21 13:09:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-09 16:10:09 -04:00
|
|
|
# CSS files
|
|
|
|
get %r{/(.*).css} do |path|
|
|
|
|
content_type 'text/css', :charset => 'utf-8'
|
|
|
|
begin
|
2009-09-19 16:06:37 -04:00
|
|
|
static_version = File.join(Dir.pwd, 'public') + request.path_info
|
|
|
|
send_file(static_version) if File.exists? static_version
|
|
|
|
|
2009-08-11 19:26:08 -04: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 16:10:09 -04:00
|
|
|
rescue Exception => e
|
|
|
|
sass_exception_string(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-12 13:00:06 -04:00
|
|
|
# All other files
|
2009-07-28 12:39:06 -04:00
|
|
|
get /(.*)/ do |path|
|
2009-09-18 12:59:07 -04:00
|
|
|
path << "index.#{options.default_ext}" if path.match(%r{/$})
|
2009-07-28 12:39:06 -04:00
|
|
|
path.gsub!(%r{^/}, '')
|
2009-09-18 12:59:07 -04:00
|
|
|
path_without_ext = path.gsub(File.extname(path), '')
|
2009-07-28 16:44:56 -04:00
|
|
|
|
|
|
|
result = nil
|
2009-08-09 16:10:09 -04:00
|
|
|
begin
|
2009-08-12 13:00:06 -04:00
|
|
|
options.supported_formats.detect do |renderer|
|
2009-09-18 12:59:07 -04:00
|
|
|
if File.exists?(File.join(options.views, "#{path}.#{renderer}"))
|
|
|
|
result = send(renderer.to_sym, path.to_sym)
|
|
|
|
elsif File.exists?(File.join(options.views, "#{path_without_ext}.#{renderer}"))
|
|
|
|
result = send(renderer.to_sym, path_without_ext.to_sym)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2009-07-28 12:39:06 -04:00
|
|
|
end
|
2009-08-09 16:10:09 -04:00
|
|
|
rescue Haml::Error => e
|
|
|
|
result = "Haml Error: #{e}"
|
2009-09-17 12:40:29 -04:00
|
|
|
result << "<pre>Backtrace: #{e.backtrace.join("\n")}</pre>"
|
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-09-18 12:59:07 -04:00
|
|
|
|
|
|
|
get %r{/(.*\.xml)} do |path|
|
|
|
|
content_type 'text/xml', :charset => 'utf-8'
|
|
|
|
haml(path.to_sym, :layout => false)
|
|
|
|
end
|
2009-08-12 13:00:06 -04:00
|
|
|
end
|