diff --git a/bin/mm-server b/bin/mm-server index 77c128a6..58b933f8 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -1,9 +1,19 @@ #!/usr/bin/env ruby +require 'optparse' + # Require Middleman require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman') -# Start Middleman -Middleman::Base.set({ :server => %w[thin webrick], - :root => Dir.pwd }) -Middleman::Base.run! \ No newline at end of file +class Middleman::Base + set :root, Dir.pwd + + OptionParser.new { |op| + op.on('-e env') { |val| set :environment, val.to_sym } + op.on('-s server') { |val| set :server, val } + op.on('-p port') { |val| set :port, val.to_i } + }.parse!(ARGV.dup) + + # Start Middleman + run! +end \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 3414440d..b67f992f 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -1,7 +1,10 @@ +# Be nice to other library systems, like the wonderful Rip require 'rubygems' unless ENV['NO_RUBYGEMS'] -require 'sinatra/base' -require 'middleman/helpers' +# We're riding on Sinatra, so let's include it +require 'sinatra/base' + +# Rack helper for adding mime-types during local preview def mime(ext, type) ext = ".#{ext}" unless ext.to_s[0] == ?. Rack::Mime::MIME_TYPES[ext.to_s] = type @@ -20,9 +23,12 @@ module Middleman set :build_dir, "build" set :http_prefix, "/" + # Features enabled by default enable :compass enable :content_for enable :sprockets + + # Features disabled by default disable :slickmap disable :cache_buster disable :minify_css @@ -31,9 +37,6 @@ module Middleman disable :markaby disable :maruku - # include helpers - helpers Middleman::Helpers - # Default build features configure :build do enable :minify_css @@ -41,6 +44,7 @@ module Middleman enable :cache_buster 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 @@ -55,21 +59,25 @@ module Middleman end include StaticRender - # All other files + # Disable static asset handling in Rack, so we can customize it here disable :static + + # This will match all requests not overridden in the project's init.rb not_found do + # Normalize the path and add index if we're looking at a directory path = request.path path << options.index_file if path.match(%r{/$}) path.gsub!(%r{^/}, '') + # If the enabled renderers succeed, return the content, mime-type and an HTTP 200 if content = render_path(path) content_type media_type(File.extname(path)), :charset => 'utf-8' status 200 content else - # Otherwise, send the static file + # If no template handler renders the template, return the static file if it exists path = File.join(options.public, request.path) - if File.exists?(path) + if !File.directory?(path) && File.exists?(path) status 200 send_file(path) else @@ -78,6 +86,8 @@ module Middleman end end + # Copy, pasted & edited version of the setup in Sinatra. + # Basically just changed the app's name and call out feature & configuration init. def self.run!(options={}, &block) init! set options @@ -101,15 +111,21 @@ module Middleman puts "== The Middleman is already standing watch on port #{port}!" end + # Require the features for this project def self.init! - # Check for local config + # Built-in helpers + require 'middleman/helpers' + helpers Middleman::Helpers + + # Haml is required & includes helpers + require "middleman/features/haml" + + # Check for and evaluate local configuration 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 - - require "middleman/features/haml" # loop over enabled feature features_path = File.expand_path("features/*.rb", File.dirname(__FILE__)) diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 52d61d93..720106cb 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -1,7 +1,10 @@ +# Use Rack::Test to access Sinatra without starting up a full server require 'rack/test' +# Placeholder for any methods the builder needs to abstract to allow feature integration module Middleman class Builder + # The default render just requests the page over Rack and writes the response def self.render_file(source, destination) request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "") browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) diff --git a/lib/middleman/features/haml.rb b/lib/middleman/features/haml.rb index 1d831f91..1adaf001 100644 --- a/lib/middleman/features/haml.rb +++ b/lib/middleman/features/haml.rb @@ -72,7 +72,7 @@ module Middleman static_version = options.public + request.path_info send_file(static_version) if File.exists? static_version - location_of_sass_file = options.environment == "build" ? "build" : "views" + location_of_sass_file = options.environment == "build" ? "build" : "public" 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 })) rescue Exception => e diff --git a/lib/middleman/features/relative_assets.rb b/lib/middleman/features/relative_assets.rb index 1039513f..e638cfec 100644 --- a/lib/middleman/features/relative_assets.rb +++ b/lib/middleman/features/relative_assets.rb @@ -14,8 +14,9 @@ class Middleman::Base if path.include?("://") path else + path = path[1,path.length-1] if path[0,1] == '/' request_path = request.path_info.dup - request_path << self.index_file if path.match(%r{/$}) + request_path << options.index_file if path.match(%r{/$}) request_path.gsub!(%r{^/}, '') parts = request_path.split('/') diff --git a/lib/middleman/template/init.rb b/lib/middleman/template/init.rb index 2edbfc9e..f25ea4a5 100644 --- a/lib/middleman/template/init.rb +++ b/lib/middleman/template/init.rb @@ -2,21 +2,17 @@ helpers do end -# Or inject more templating languages -# helpers Sinatra::Markdown +# Generic configuration +# enable :slickmap # Build-specific configuration configure :build do - Compass.configuration do |config| - # For example, change the Compass output style for deployment - # config.output_style = :compressed - - # Or use a different image path - # config.http_images_path = "/Content/images/" - - # Disable cache buster - # config.asset_cache_buster do - # false - # end - end + # For example, change the Compass output style for deployment + # enable :minified_css + + # Or use a different image path + # set :http_path, "/Content/images/" + + # Disable cache buster + # disable :cache_buster end \ No newline at end of file diff --git a/lib/middleman/template/views/index.haml b/lib/middleman/template/views/index.haml index f78f7ca8..7b2bc807 100644 --- a/lib/middleman/template/views/index.haml +++ b/lib/middleman/template/views/index.haml @@ -1 +1,4 @@ +- content_for :head do + %title Custom head title + %h1 The Middleman is watching. \ No newline at end of file diff --git a/lib/middleman/template/views/layout.haml b/lib/middleman/template/views/layout.haml index 257b3d84..f53be024 100644 --- a/lib/middleman/template/views/layout.haml +++ b/lib/middleman/template/views/layout.haml @@ -1,6 +1,7 @@ %html %head %title The Middleman! + = yield_content :head %body = yield \ No newline at end of file