better rack

This commit is contained in:
tdreyno 2010-03-02 17:38:08 -08:00
parent 3f2f6d7150
commit ccbc3cf46f
6 changed files with 38 additions and 55 deletions

View file

@ -23,7 +23,7 @@ module Middleman
set :build_dir, "build" set :build_dir, "build"
set :http_prefix, nil set :http_prefix, nil
use ::Rack::ConditionalGet if environment == :development #use ::Rack::ConditionalGet if environment == :development
helpers Sinatra::ContentFor helpers Sinatra::ContentFor
set :features, [] set :features, []
@ -137,7 +137,6 @@ require "middleman/rack/static"
require "middleman/rack/sprockets" require "middleman/rack/sprockets"
require "middleman/rack/minify_javascript" require "middleman/rack/minify_javascript"
require "middleman/rack/minify_css" require "middleman/rack/minify_css"
require "middleman/rack/downstream"
class Middleman::Base class Middleman::Base
helpers Middleman::Helpers helpers Middleman::Helpers
@ -166,11 +165,14 @@ class Middleman::Base
set :app_file, File.expand_path(local_config) set :app_file, File.expand_path(local_config)
end end
use Middleman::Rack::Static # use Rack::Static, :urls => [/.*/], :root => Middleman::Base.public
use Middleman::Rack::Sprockets # use Rack::Static, :urls => [/.*/], :root => Middleman::Base.views
# use Middleman::Rack::Static
use Middleman::Rack::Sprockets, :root => Middleman::Base.root,
:load_path => [ File.join("public", Middleman::Base.js_dir),
File.join("views", Middleman::Base.js_dir) ]
use Middleman::Rack::MinifyJavascript use Middleman::Rack::MinifyJavascript
use Middleman::Rack::MinifyCSS use Middleman::Rack::MinifyCSS
use Middleman::Rack::Downstream
def self.new(*args, &block) def self.new(*args, &block)
# loop over enabled feature # loop over enabled feature

View file

@ -1,9 +0,0 @@
class Middleman::Rack::Downstream
def initialize(app, options={})
@app = app
end
def call(env)
env["DOWNSTREAM"] || @app.call(env)
end
end

View file

@ -10,19 +10,16 @@ class Middleman::Rack::MinifyCSS
end end
def call(env) def call(env)
if Middleman::Base.enabled?(:minify_css) && status, headers, response = @app.call(env)
env["DOWNSTREAM"] && env["PATH_INFO"].match(/\.css$/)
if Middleman::Base.enabled?(:minify_css) && env["PATH_INFO"].match(/\.css$/)
compressor = ::YUI::CssCompressor.new compressor = ::YUI::CssCompressor.new
source = env["DOWNSTREAM"][2].is_a?(::Rack::File) ? uncompressed_source = response.is_a?(::Rack::File) ? File.read(response.path) : response
File.read(env["DOWNSTREAM"][2].path) : response = compressor.compress(uncompressed_source)
env["DOWNSTREAM"][2] headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
env["DOWNSTREAM"][2] = compressor.compress(source)
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
end end
@app.call(env) [status, headers, response]
end end
end end

View file

@ -10,19 +10,16 @@ class Middleman::Rack::MinifyJavascript
end end
def call(env) def call(env)
if Middleman::Base.enabled?(:minify_javascript) && status, headers, response = @app.call(env)
env["DOWNSTREAM"] && env["PATH_INFO"].match(/\.js$/)
if Middleman::Base.enabled?(:minify_javascript) && env["PATH_INFO"].match(/\.js$/)
compressor = ::YUI::JavaScriptCompressor.new(:munge => true) compressor = ::YUI::JavaScriptCompressor.new(:munge => true)
source = env["DOWNSTREAM"][2].is_a?(::Rack::File) ? uncompressed_source = response.is_a?(::Rack::File) ? File.read(response.path) : response
File.read(env["DOWNSTREAM"][2].path) : response = compressor.compress(uncompressed_source)
env["DOWNSTREAM"][2] headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
env["DOWNSTREAM"][2] = compressor.compress(source)
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
end end
@app.call(env) [status, headers, response]
end end
end end

View file

@ -8,27 +8,23 @@ end
class Middleman::Rack::Sprockets class Middleman::Rack::Sprockets
def initialize(app, options={}) def initialize(app, options={})
@app = app @app = app
@options = options
end end
def call(env) def call(env)
path = env["PATH_INFO"] if env["PATH_INFO"].match(/\.js$/)
public_file_path = File.join(Middleman::Base.public, env["PATH_INFO"])
view_file_path = File.join(Middleman::Base.views, env["PATH_INFO"])
if env["DOWNSTREAM"] && path.match(/\.js$/) source_file = Rack::File.new(Middleman::Base.public) if File.exists?(public_file_path)
source = "public" if File.exists?(File.join(Middleman::Base.views, path)) source_file = Rack::File.new(Middleman::Base.views) if File.exists?(view_file_path)
source = "views" if File.exists?(File.join(Middleman::Base.views, path))
if source if source_file
source_file = env["DOWNSTREAM"][2].is_a?(::Rack::File) ? status, headers, response = source_file.call(env)
env["DOWNSTREAM"][2].path : secretary = ::Sprockets::Secretary.new(@options.merge( :source_files => [ response.path ] ))
env["DOWNSTREAM"][2] response = secretary.concatenation.to_s
headers["Content-Length"] = ::Rack::Utils.bytesize(response).to_s
secretary = ::Sprockets::Secretary.new( :root => Middleman::Base.root, return [status, headers, response]
:source_files => [ source_file ],
:load_path => [ File.join("public", Middleman::Base.js_dir),
File.join("views", Middleman::Base.js_dir) ])
env["DOWNSTREAM"][2] = secretary.concatenation.to_s
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
end end
end end

View file

@ -8,11 +8,11 @@ class Middleman::Rack::Static
view_file_path = File.join(Middleman::Base.views, env["PATH_INFO"]) view_file_path = File.join(Middleman::Base.views, env["PATH_INFO"])
if File.exists?(public_file_path) && !File.directory?(public_file_path) if File.exists?(public_file_path) && !File.directory?(public_file_path)
env["DOWNSTREAM"] = ::Rack::File.new(Middleman::Base.public).call(env) ::Rack::File.new(Middleman::Base.public).call(env)
elsif File.exists?(view_file_path) && !File.directory?(view_file_path) elsif File.exists?(view_file_path) && !File.directory?(view_file_path)
env["DOWNSTREAM"] = ::Rack::File.new(Middleman::Base.views).call(env) ::Rack::File.new(Middleman::Base.views).call(env)
end else
@app.call(env) @app.call(env)
end end
end end
end