relative assets working better

This commit is contained in:
tdreyno 2009-10-01 14:26:39 -07:00
parent 087f48c98d
commit dbe067ee15
8 changed files with 61 additions and 31 deletions

View file

@ -1,9 +1,19 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'optparse'
# Require Middleman # Require Middleman
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman') require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
# Start Middleman class Middleman::Base
Middleman::Base.set({ :server => %w[thin webrick], set :root, Dir.pwd
:root => Dir.pwd })
Middleman::Base.run! 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

View file

@ -1,7 +1,10 @@
# Be nice to other library systems, like the wonderful Rip
require 'rubygems' unless ENV['NO_RUBYGEMS'] 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) def mime(ext, type)
ext = ".#{ext}" unless ext.to_s[0] == ?. ext = ".#{ext}" unless ext.to_s[0] == ?.
Rack::Mime::MIME_TYPES[ext.to_s] = type Rack::Mime::MIME_TYPES[ext.to_s] = type
@ -20,9 +23,12 @@ module Middleman
set :build_dir, "build" set :build_dir, "build"
set :http_prefix, "/" set :http_prefix, "/"
# Features enabled by default
enable :compass enable :compass
enable :content_for enable :content_for
enable :sprockets enable :sprockets
# Features disabled by default
disable :slickmap disable :slickmap
disable :cache_buster disable :cache_buster
disable :minify_css disable :minify_css
@ -31,9 +37,6 @@ module Middleman
disable :markaby disable :markaby
disable :maruku disable :maruku
# include helpers
helpers Middleman::Helpers
# Default build features # Default build features
configure :build do configure :build do
enable :minify_css enable :minify_css
@ -41,6 +44,7 @@ module Middleman
enable :cache_buster enable :cache_buster
end end
# Convenience function to discover if a tempalte exists for the requested renderer (haml, sass, etc)
def template_exists?(path, renderer=nil) def template_exists?(path, renderer=nil)
template_path = path.dup template_path = path.dup
template_path << ".#{renderer}" if renderer template_path << ".#{renderer}" if renderer
@ -55,21 +59,25 @@ module Middleman
end end
include StaticRender include StaticRender
# All other files # Disable static asset handling in Rack, so we can customize it here
disable :static disable :static
# This will match all requests not overridden in the project's init.rb
not_found do not_found do
# Normalize the path and add index if we're looking at a directory
path = request.path path = request.path
path << options.index_file if path.match(%r{/$}) path << options.index_file if path.match(%r{/$})
path.gsub!(%r{^/}, '') path.gsub!(%r{^/}, '')
# If the enabled renderers succeed, return the content, mime-type and an HTTP 200
if content = render_path(path) if content = render_path(path)
content_type media_type(File.extname(path)), :charset => 'utf-8' content_type media_type(File.extname(path)), :charset => 'utf-8'
status 200 status 200
content content
else 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) path = File.join(options.public, request.path)
if File.exists?(path) if !File.directory?(path) && File.exists?(path)
status 200 status 200
send_file(path) send_file(path)
else else
@ -78,6 +86,8 @@ module Middleman
end end
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) def self.run!(options={}, &block)
init! init!
set options set options
@ -101,15 +111,21 @@ module Middleman
puts "== The Middleman is already standing watch on port #{port}!" puts "== The Middleman is already standing watch on port #{port}!"
end end
# Require the features for this project
def self.init! 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") local_config = File.join(self.root, "init.rb")
if File.exists? local_config if File.exists? local_config
puts "== Local config at: #{local_config}" puts "== Local config at: #{local_config}"
class_eval File.read(local_config) class_eval File.read(local_config)
end end
require "middleman/features/haml"
# loop over enabled feature # loop over enabled feature
features_path = File.expand_path("features/*.rb", File.dirname(__FILE__)) features_path = File.expand_path("features/*.rb", File.dirname(__FILE__))

View file

@ -1,7 +1,10 @@
# Use Rack::Test to access Sinatra without starting up a full server
require 'rack/test' require 'rack/test'
# Placeholder for any methods the builder needs to abstract to allow feature integration
module Middleman module Middleman
class Builder class Builder
# The default render just requests the page over Rack and writes the response
def self.render_file(source, destination) def self.render_file(source, destination)
request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "") request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "")
browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base))

View file

@ -72,7 +72,7 @@ module Middleman
static_version = options.public + request.path_info static_version = options.public + request.path_info
send_file(static_version) if File.exists? static_version 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 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 })) sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename }))
rescue Exception => e rescue Exception => e

View file

@ -14,8 +14,9 @@ class Middleman::Base
if path.include?("://") if path.include?("://")
path path
else else
path = path[1,path.length-1] if path[0,1] == '/'
request_path = request.path_info.dup 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{^/}, '') request_path.gsub!(%r{^/}, '')
parts = request_path.split('/') parts = request_path.split('/')

View file

@ -2,21 +2,17 @@
helpers do helpers do
end end
# Or inject more templating languages # Generic configuration
# helpers Sinatra::Markdown # enable :slickmap
# Build-specific configuration # Build-specific configuration
configure :build do configure :build do
Compass.configuration do |config| # For example, change the Compass output style for deployment
# For example, change the Compass output style for deployment # enable :minified_css
# config.output_style = :compressed
# Or use a different image path
# Or use a different image path # set :http_path, "/Content/images/"
# config.http_images_path = "/Content/images/"
# Disable cache buster
# Disable cache buster # disable :cache_buster
# config.asset_cache_buster do
# false
# end
end
end end

View file

@ -1 +1,4 @@
- content_for :head do
%title Custom head title
%h1 The Middleman is watching. %h1 The Middleman is watching.

View file

@ -1,6 +1,7 @@
%html %html
%head %head
%title The Middleman! %title The Middleman!
= yield_content :head
%body %body
= yield = yield