more tweaking, better rack
This commit is contained in:
parent
b1d300718f
commit
16b8ba89a8
11 changed files with 79 additions and 88 deletions
|
@ -6,7 +6,7 @@ ENV['MM_ENV'] = "build"
|
|||
require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
|
||||
require 'middleman/builder'
|
||||
|
||||
Middleman::Base.init!
|
||||
#Middleman::Base.init!
|
||||
Middleman::Builder.init!
|
||||
|
||||
Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
require 'optparse'
|
||||
|
||||
# Require Middleman
|
||||
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
|
||||
|
||||
env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development'
|
||||
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
|
||||
|
||||
|
@ -15,23 +18,22 @@ OptionParser.new { |opts|
|
|||
opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
|
||||
env = e
|
||||
}
|
||||
opts.on("--debug", "Debug mode") {
|
||||
::Middleman::Base.set :logging, true
|
||||
}
|
||||
|
||||
opts.parse! ARGV
|
||||
}
|
||||
|
||||
ENV['RACK_ENV'] = env
|
||||
|
||||
# Require Middleman
|
||||
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
|
||||
|
||||
class Middleman::Base
|
||||
set :root, Dir.pwd
|
||||
set :logging, false
|
||||
end
|
||||
|
||||
require 'shotgun'
|
||||
config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru')
|
||||
app = Shotgun.new(config, lambda { |inner_app| Middleman::Base.new })
|
||||
app = Shotgun.new(config, lambda { |inner_app| Middleman::Base })
|
||||
|
||||
require 'thin'
|
||||
Thin::Logging.silent = true
|
||||
|
|
|
@ -8,6 +8,8 @@ module Middleman
|
|||
class Base < Sinatra::Base
|
||||
set :app_file, __FILE__
|
||||
set :root, Dir.pwd
|
||||
set :reload, false
|
||||
set :logging, false
|
||||
set :environment, ENV['MM_ENV'] || :development
|
||||
set :supported_formats, %w(erb)
|
||||
set :index_file, "index.html"
|
||||
|
@ -16,60 +18,34 @@ module Middleman
|
|||
set :images_dir, "images"
|
||||
set :build_dir, "build"
|
||||
set :http_prefix, "/"
|
||||
|
||||
require 'sinatra/content_for'
|
||||
helpers Sinatra::ContentFor
|
||||
|
||||
require 'middleman/helpers'
|
||||
helpers Middleman::Helpers
|
||||
use Rack::ConditionalGet if environment == :development
|
||||
|
||||
# Static files in public/
|
||||
enable :static
|
||||
require 'middleman/rack/static'
|
||||
use Middleman::Rack::Static
|
||||
set :features, [:compass]
|
||||
|
||||
@@features = []
|
||||
def self.enable(*opts)
|
||||
@@features << opts
|
||||
self.features << opts
|
||||
super
|
||||
end
|
||||
|
||||
def self.disable(*opts)
|
||||
@@features -= opts
|
||||
self.features -= opts
|
||||
super
|
||||
end
|
||||
|
||||
# Features enabled by default
|
||||
enable :sprockets
|
||||
|
||||
# Features disabled by default
|
||||
disable :slickmap
|
||||
disable :cache_buster
|
||||
disable :minify_css
|
||||
disable :minify_javascript
|
||||
disable :relative_assets
|
||||
disable :maruku
|
||||
disable :smush_pngs
|
||||
|
||||
# Default build features
|
||||
configure :build do
|
||||
enable :minify_css
|
||||
enable :minify_javascript
|
||||
enable :cache_buster
|
||||
end
|
||||
|
||||
# Rack helper for adding mime-types during local preview
|
||||
def self.mime(ext, type)
|
||||
ext = ".#{ext}" unless ext.to_s[0] == ?.
|
||||
Rack::Mime::MIME_TYPES[ext.to_s] = type
|
||||
end
|
||||
|
||||
|
||||
# Convenience function to discover if a tempalte exists for the requested renderer (haml, sass, etc)
|
||||
def template_exists?(path, renderer=nil)
|
||||
template_path = path.dup
|
||||
template_path << ".#{renderer}" if renderer
|
||||
File.exists? File.join(options.views, template_path)
|
||||
end
|
||||
|
||||
|
||||
# Base case renderer (do nothing), Should be over-ridden
|
||||
module StaticRender
|
||||
def render_path(path)
|
||||
|
@ -81,11 +57,9 @@ module Middleman
|
|||
end
|
||||
end
|
||||
include StaticRender
|
||||
|
||||
|
||||
# This will match all requests not overridden in the project's init.rb
|
||||
not_found do
|
||||
self.class.init!(true, false)
|
||||
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
path = request.path
|
||||
path << options.index_file if path.match(%r{/$})
|
||||
|
@ -100,30 +74,54 @@ module Middleman
|
|||
status 404
|
||||
end
|
||||
end
|
||||
|
||||
@@inited = false
|
||||
# Require the features for this project
|
||||
def self.init!(quiet=false, rerun=true)
|
||||
return if @@inited && !rerun
|
||||
|
||||
# Check for and evaluate local configuration
|
||||
local_config = File.join(self.root, "init.rb")
|
||||
if File.exists? local_config
|
||||
puts "== Reading: Local config" unless quiet
|
||||
class_eval File.read(local_config)
|
||||
end
|
||||
|
||||
# loop over enabled feature
|
||||
@@features.flatten.each do |feature_name|
|
||||
puts "== Enabling: #{feature_name.capitalize}" unless quiet
|
||||
require "middleman/features/#{feature_name}"
|
||||
end
|
||||
|
||||
@@inited = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Haml is required & includes helpers
|
||||
require "middleman/haml"
|
||||
require "middleman/sass"
|
||||
require "middleman/sass"
|
||||
require 'sinatra/content_for'
|
||||
require 'middleman/helpers'
|
||||
require 'middleman/rack/static'
|
||||
require 'middleman/rack/sprockets'
|
||||
|
||||
class Middleman::Base
|
||||
helpers Sinatra::ContentFor
|
||||
helpers Middleman::Helpers
|
||||
|
||||
use Middleman::Rack::Static
|
||||
use Middleman::Rack::Sprockets
|
||||
|
||||
# Features disabled by default
|
||||
disable :slickmap
|
||||
disable :cache_buster
|
||||
disable :minify_css
|
||||
disable :minify_javascript
|
||||
disable :relative_assets
|
||||
disable :maruku
|
||||
disable :smush_pngs
|
||||
|
||||
# Default build features
|
||||
configure :build do
|
||||
enable :minify_css
|
||||
enable :minify_javascript
|
||||
enable :cache_buster
|
||||
end
|
||||
|
||||
def self.new(*args, &bk)
|
||||
# Check for and evaluate local configuration
|
||||
local_config = File.join(self.root, "init.rb")
|
||||
if File.exists? local_config
|
||||
puts "== Reading: Local config" if logging?
|
||||
class_eval File.read(local_config)
|
||||
end
|
||||
|
||||
# loop over enabled feature
|
||||
self.features.flatten.each do |feature_name|
|
||||
puts "== Enabling: #{feature_name.capitalize}" if logging?
|
||||
require "middleman/features/#{feature_name}"
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
class << Middleman::Base
|
||||
alias_method :pre_cache_buster_asset_url, :asset_url
|
||||
def asset_url(path, prefix="")
|
||||
http_path = pre_cache_buster_asset_url(path, prefix)
|
||||
def asset_url(path, prefix="", request=nil)
|
||||
http_path = pre_cache_buster_asset_url(path, prefix, request)
|
||||
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
|
||||
http_path
|
||||
else
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
require 'compass'
|
||||
|
||||
class Middleman::Base
|
||||
configure do
|
||||
::Compass.configuration do |config|
|
|
@ -1,23 +1,17 @@
|
|||
class Middleman::Base
|
||||
if compass?
|
||||
configure do
|
||||
::Compass.configuration do |config|
|
||||
config.relative_assets = true
|
||||
end
|
||||
end
|
||||
end
|
||||
::Compass.configuration do |config|
|
||||
config.relative_assets = true
|
||||
end
|
||||
|
||||
class << Middleman::Base
|
||||
alias_method :pre_relative_asset_url, :asset_url
|
||||
def asset_url(path, prefix="")
|
||||
path = pre_relative_asset_url(path, prefix)
|
||||
def asset_url(path, prefix="", request=nil)
|
||||
path = pre_relative_asset_url(path, prefix, request)
|
||||
if path.include?("://")
|
||||
path
|
||||
else
|
||||
path = path[1,path.length-1] if path[0,1] == '/'
|
||||
request_path = request.path_info.dup
|
||||
request_path << options.index_file if path.match(%r{/$})
|
||||
request_path << self.class.index_file if path.match(%r{/$})
|
||||
request_path.gsub!(%r{^/}, '')
|
||||
parts = request_path.split('/')
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Middleman
|
||||
class Base
|
||||
def self.asset_url(path, prefix="")
|
||||
def self.asset_url(path, prefix="", request=nil)
|
||||
base_url = File.join(self.http_prefix, prefix)
|
||||
path.include?("://") ? path : File.join(base_url, path)
|
||||
end
|
||||
|
@ -21,8 +21,8 @@ module Middleman
|
|||
classes.join(' ')
|
||||
end
|
||||
|
||||
def asset_url(*args)
|
||||
self.class.asset_url(*args)
|
||||
def asset_url(path, prefix="")
|
||||
self.class.asset_url(path, prefix, request)
|
||||
end
|
||||
|
||||
def link_to(title, url="#", params={})
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
begin
|
||||
require 'sprockets'
|
||||
require 'middleman/features/sprockets+ruby19' # Sprockets ruby 1.9 duckpunch
|
||||
require 'middleman/rack/sprockets+ruby19' # Sprockets ruby 1.9 duckpunch
|
||||
rescue LoadError
|
||||
puts "Sprockets not available. Install it with: gem install sprockets"
|
||||
end
|
||||
|
||||
|
||||
module Middleman
|
||||
module Rack
|
||||
class Sprockets
|
||||
|
@ -31,5 +31,4 @@ module Middleman
|
|||
end
|
||||
end
|
||||
|
||||
Middleman::Base.use Middleman::Rack::Sprockets
|
||||
Middleman::Base.supported_formats << "js"
|
|
@ -1,5 +1,5 @@
|
|||
require "sass"
|
||||
require "middleman/compass"
|
||||
require 'compass'
|
||||
|
||||
module Middleman
|
||||
module Sass
|
||||
|
|
|
@ -6,13 +6,13 @@ base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
|
|||
describe "Cache Buster Feature" do
|
||||
it "should not append query string if off" do
|
||||
base.disable :cache_buster
|
||||
base.init!(true, true)
|
||||
base.new
|
||||
base.asset_url("stylesheets/static.css").should_not include("?")
|
||||
end
|
||||
|
||||
it "should append query string if on" do
|
||||
base.enable :cache_buster
|
||||
base.init!(true, true)
|
||||
base.new
|
||||
base.asset_url("stylesheets/static.css").should include("?")
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue