Skip to content

Commit

Permalink
relative assets working better
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Oct 1, 2009
1 parent 087f48c commit dbe067e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
18 changes: 14 additions & 4 deletions bin/mm-server
Original file line number Diff line number Diff line change
@@ -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!
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
36 changes: 26 additions & 10 deletions lib/middleman/base.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Be nice to other library systems, like the wonderful Rip
require 'rubygems' unless ENV['NO_RUBYGEMS']

# We're riding on Sinatra, so let's include it
require 'sinatra/base'
require 'middleman/helpers'

# 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
Expand All @@ -20,9 +23,12 @@ class Base < Sinatra::Base
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
Expand All @@ -31,16 +37,14 @@ class Base < Sinatra::Base
disable :markaby
disable :maruku

# include helpers
helpers Middleman::Helpers

# Default build features
configure :build do
enable :minify_css
enable :minify_javascript
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
Expand All @@ -55,21 +59,25 @@ def render_path(path)
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
Expand All @@ -78,6 +86,8 @@ def render_path(path)
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
Expand All @@ -101,15 +111,21 @@ def self.run!(options={}, &block)
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__))
Expand Down
3 changes: 3 additions & 0 deletions lib/middleman/builder.rb
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion lib/middleman/features/haml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def render_path(path)
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
Expand Down
3 changes: 2 additions & 1 deletion lib/middleman/features/relative_assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def asset_url(path, prefix="")
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('/')

Expand Down
24 changes: 10 additions & 14 deletions lib/middleman/template/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lib/middleman/template/views/index.haml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
- content_for :head do
%title Custom head title

%h1 The Middleman is watching.
1 change: 1 addition & 0 deletions lib/middleman/template/views/layout.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%html
%head
%title The Middleman!
= yield_content :head

%body
= yield

0 comments on commit dbe067e

Please sign in to comment.