1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Now riding on Sinatra extensions and using Padrino rendering

This commit is contained in:
tdreyno 2010-09-06 18:48:25 -07:00
parent 4ba90bf1c7
commit 4d0567cf40
33 changed files with 421 additions and 487 deletions

View file

@ -17,7 +17,8 @@ begin
gem.add_dependency("shotgun", "~>0.8.0") gem.add_dependency("shotgun", "~>0.8.0")
gem.add_dependency("templater", "~>1.0.0") gem.add_dependency("templater", "~>1.0.0")
gem.add_dependency("sinatra", "~>1.0") gem.add_dependency("sinatra", "~>1.0")
gem.add_dependency("sinatra-content-for", "~>0.2.0") gem.add_dependency("padrino-core", "~>0.9.0")
gem.add_dependency("padrino-helpers", "~>0.9.0")
gem.add_dependency("rack-test", "~>0.5.0") gem.add_dependency("rack-test", "~>0.5.0")
gem.add_dependency("yui-compressor", "~>0.9.0") gem.add_dependency("yui-compressor", "~>0.9.0")
gem.add_dependency("haml", "~>3.0") gem.add_dependency("haml", "~>3.0")

View file

@ -1 +1 @@
0.99.1.pre 0.99.2.pre

View file

@ -1,31 +0,0 @@
Feature: Built-in macro view helpers
In order to simplify generating HTML
Scenario: Using the link_to helper
Given the Server is running
When I go to "/link_to.html"
Then I should see '<a href="#">No Href</a>'
And I should see '<a href="test.com">Has Href</a>'
And I should see '<a class="test" href="test2.com">Has param</a>'
Scenario: Using the image_tag helper
Given the Server is running
When I go to "/image_tag.html"
Then I should see '<img src="/images/test.png" alt="" />'
And I should see '<img src="/images/test2.png" alt="alt" />'
Scenario: Using the javascript_include_tag helper
Given the Server is running
When I go to "/javascript_include_tag.html"
Then I should see '<script type="text/javascript" src="/javascripts/test1.js"></script>'
Then I should see '<script type="text/javascript" src="/javascripts/test2.js"></script>'
Then I should see '<script type="text/javascript" src="/javascripts/test3.js"></script>'
Then I should see '<script type="text/javascript" src="http://test.com/javascripts/test4.js"></script>'
Scenario: Using the stylesheet_link_tag helper
Given the Server is running
When I go to "/stylesheet_link_tag.html"
Then I should see '<link type="text/css" rel="stylesheet" href="/stylesheets/test1.css" />'
Then I should see '<link type="text/css" rel="stylesheet" href="/stylesheets/test2.css" />'
Then I should see '<link type="text/css" rel="stylesheet" href="/stylesheets/test3.css" />'
Then I should see '<link type="text/css" rel="stylesheet" href="http://test.com/stylesheets/test4.css" />'

View file

@ -0,0 +1,10 @@
Feature: Built-in macro view helpers
In order to simplify generating HTML
Scenario: Using the link_to helper
Given the Server is running
When I go to "/padrino_test.html"
And I should see 'href="test2.com"'
And I should see 'src="/images/test2.png"'
Then I should see 'src="/javascripts/test1.js"'
Then I should see 'href="/stylesheets/test1.css"'

View file

@ -4,4 +4,4 @@ Feature: Support SCSS Syntax
Scenario: Rendering scss Scenario: Rendering scss
Given the Server is running Given the Server is running
When I go to "/stylesheets/site_scss.css" When I go to "/stylesheets/site_scss.css"
Then I should see "html,body,div,span,applet,object,iframe" Then I should see "html"

View file

@ -1,5 +1,5 @@
Given /^I am using an asset host$/ do Given /^I am using an asset host$/ do
Middleman::Server.enable :asset_host Middleman::Server.activate :asset_host
Middleman::Server.set :asset_host do |asset| Middleman::Server.set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4) "http://assets%d.example.com" % (asset.hash % 4)
end end

View file

@ -1,6 +1,7 @@
Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state| Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state|
enable_or_disable = (state == "enabled") ? :enable : :disable if state == "enabled"
Middleman::Server.send(enable_or_disable, feature.to_sym) Middleman::Server.activate(feature.to_sym)
end
@browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new)) @browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Server.new))
end end

View file

@ -1,2 +0,0 @@
= image_tag "test.png"
= image_tag "test2.png", :alt => "alt"

View file

@ -1,4 +0,0 @@
= javascript_include_tag "test1"
= javascript_include_tag :test2
= javascript_include_tag "test3.js"
= javascript_include_tag "http://test.com/javascripts/test4.js"

View file

@ -1,3 +0,0 @@
= link_to "No Href"
= link_to "Has Href", "test.com"
= link_to "Has param", "test2.com", :class => "test"

View file

@ -1 +0,0 @@
# Hello Maruku {.header}

View file

@ -0,0 +1,5 @@
= stylesheet_link_tag "test1"
= javascript_include_tag "test1"
= image_tag "test2.png", :alt => "alt"
= link_to "Has param", "test2.com", :class => "test"

View file

@ -1,4 +0,0 @@
= stylesheet_link_tag "test1"
= stylesheet_link_tag :test2
= stylesheet_link_tag "test3.css"
= stylesheet_link_tag "http://test.com/stylesheets/test4.css"

View file

@ -5,4 +5,12 @@ require 'rubygems'
module Middleman module Middleman
autoload :Server, "middleman/server" autoload :Server, "middleman/server"
module Renderers
autoload :CoffeeScript, "middleman/renderers/coffee_script"
autoload :Haml, "middleman/renderers/haml"
autoload :Sass, "middleman/renderers/sass"
end
autoload :Features, "middleman/features"
end end

View file

@ -1,41 +1,34 @@
module Middleman module Middleman::Features
module Features autoload :RelativeAssets, "middleman/features/relative_assets"
# Top-level method to register a new feature autoload :AssetHost, "middleman/features/asset_host"
@@features = {} autoload :CacheBuster, "middleman/features/cache_buster"
def self.register(feature_name, feature_class=nil, options={}) autoload :DefaultHelpers, "middleman/features/default_helpers"
@@features[feature_name] = feature_class autoload :AutomaticImageSizes, "middleman/features/automatic_image_sizes"
autoload :UglyHaml, "middleman/features/ugly_haml"
autoload :MinifyCss, "middleman/features/minify_css"
autoload :MinifyJavascript, "middleman/features/minify_javascript"
autoload :Slickmap, "middleman/features/slickmap"
autoload :SmushPNGs, "middleman/features/smush_pngs"
# Default to disabled, unless the class asks to auto-enable class << self
activate_method = (options.has_key?(:auto_enable) && options[:auto_enable]) ? :enable : :disable def registered(app)
Middleman::Server.send(activate_method, feature_name) app.extend ClassMethods
end end
alias :included :registered
# Initialize a feature
def self.run(feature_name, feature_config, scope)
feature_class = @@features[feature_name]
feature_class.new(scope, feature_config) unless feature_class.nil?
end
# Get a list of all features
def self.all
@@features
end end
module ClassMethods
def activate(feature_name)
mod_name = feature_name.to_s.camelize
if Middleman::Features.const_defined?(mod_name)
register Middleman::Features.const_get(mod_name)
end end
end end
# livereload def enable(feature_name)
%w(default_helpers $stderr.puts "Warning: Feature activation has been renamed from enable to activate"
asset_host activate(feature_name)
automatic_image_sizes super(feature_name)
cache_buster end
minify_css end
minify_javascript
relative_assets
slickmap
smush_pngs
ugly_haml).each do |feature|
require File.join("middleman/features", feature)
end end

View file

@ -1,6 +1,7 @@
class Middleman::Features::AssetHost module Middleman::Features::AssetHost
def initialize(app, config) class << self
Middleman::Server.after_feature_init do def registered(app)
app.after_feature_init do
if Middleman::Server.asset_host.is_a?(Proc) if Middleman::Server.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&Middleman::Server.asset_host) ::Compass.configuration.asset_host(&Middleman::Server.asset_host)
end end
@ -16,6 +17,6 @@ class Middleman::Features::AssetHost
File.join(asset_prefix, original_output) File.join(asset_prefix, original_output)
end end
end end
alias :included :registered
end
end end
Middleman::Features.register :asset_host, Middleman::Features::AssetHost

View file

@ -1,9 +1,14 @@
class Middleman::Features::AutomaticImageSizes module Middleman::Features::AutomaticImageSizes
def initialize(app, config) class << self
def registered(app)
require "middleman/features/automatic_image_sizes/fastimage" require "middleman/features/automatic_image_sizes/fastimage"
Middleman::Server.helpers do app.helpers Helpers
alias_method :pre_automatic_image_tag, :image_tag end
alias :included :registered
end
module Helpers
def image_tag(path, params={}) def image_tag(path, params={})
if (!params[:width] || !params[:height]) && !path.include?("://") if (!params[:width] || !params[:height]) && !path.include?("://")
params[:alt] ||= "" params[:alt] ||= ""
@ -19,13 +24,10 @@ class Middleman::Features::AutomaticImageSizes
rescue rescue
end end
capture_haml { haml_tag(:img, params.merge(:src => asset_url(path, http_prefix))) } super(asset_url(path, http_prefix), params)
else else
pre_automatic_image_tag(path, params) super(path, params)
end end
end end
end end
end end
end
Middleman::Features.register :automatic_image_sizes, Middleman::Features::AutomaticImageSizes

View file

@ -1,5 +1,6 @@
class Middleman::Features::CacheBuster module Middleman::Features::CacheBuster
def initialize(app, config) class << self
def registered(app)
Middleman::Assets.register :cache_buster do |path, prefix, request| Middleman::Assets.register :cache_buster do |path, prefix, request|
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request) http_path = Middleman::Assets.before(:cache_buster, path, prefix, request)
@ -24,7 +25,7 @@ class Middleman::Features::CacheBuster
end end
end end
Middleman::Server.after_feature_init do app.after_feature_init do
::Compass.configuration do |config| ::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path| config.asset_cache_buster do |path, real_path|
real_path = real_path.path if real_path.is_a? File real_path = real_path.path if real_path.is_a? File
@ -38,6 +39,6 @@ class Middleman::Features::CacheBuster
end end
end end
end end
alias :included :registered
end
end end
Middleman::Features.register :cache_buster, Middleman::Features::CacheBuster

View file

@ -1,6 +1,9 @@
class Middleman::Features::DefaultHelpers module Middleman::Features::DefaultHelpers
def initialize(app, config) class << self
Middleman::Server.helpers Helpers def registered(app)
app.helpers Middleman::Features::DefaultHelpers::Helpers
end
alias :included :registered
end end
module Helpers module Helpers
@ -14,8 +17,9 @@ class Middleman::Features::DefaultHelpers
css_file = File.join(self.class.public, self.class.css_dir, "#{path}.css") css_file = File.join(self.class.public, self.class.css_dir, "#{path}.css")
sass_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.sass") sass_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.sass")
scss_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.scss") scss_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.scss")
less_file = File.join(self.class.views, self.class.css_dir, "#{path}.css.less")
if File.exists?(css_file) || File.exists?(sass_file) || File.exists?(scss_file) if File.exists?(css_file) || File.exists?(sass_file) || File.exists?(scss_file) || File.exists?(less_file)
stylesheet_link_tag "#{path}.css" stylesheet_link_tag "#{path}.css"
end end
end end
@ -36,43 +40,22 @@ class Middleman::Features::DefaultHelpers
Middleman::Assets.get_url(path, prefix, request) Middleman::Assets.get_url(path, prefix, request)
end end
def link_to(title, url="#", params={}) # Padrino's asset handling needs to pass through ours
params.merge!(:href => url) def asset_path(kind, source)
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') return source if source =~ /^http/
%Q{<a #{params}>#{title}</a>} asset_folder = case kind
when :css then settings.css_dir
when :js then settings.js_dir
when :images then settings.images_dir
else kind.to_s
end end
source = source.to_s.gsub(/\s/, '')
def image_tag(path, params={}) ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension
params[:alt] ||= "" source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
prefix = settings.http_images_path rescue settings.images_dir result_path = source if source =~ %r{^/} # absolute path
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') result_path ||= asset_url(source, asset_folder)
params << " " if params.length > 0 timestamp = asset_timestamp(result_path)
"<img src=\"#{asset_url(path, prefix)}\" #{params}/>" "#{result_path}#{timestamp}"
end
def javascript_include_tag(path, params={})
path = path.to_s
path << ".js" unless path =~ /\.js$/
params.delete(:type)
params.delete(:src)
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
params = " " + params if params.length > 0
"<script type=\"text/javascript\" src=\"#{asset_url(path, settings.js_dir)}\"#{params}></script>"
end
def stylesheet_link_tag(path, params={})
path = path.to_s
path << ".css" unless path =~ /\.css$/
params.delete(:type)
params.delete(:rel)
params.delete(:href)
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
params << " " if params.length > 0
"<link type=\"text/css\" rel=\"stylesheet\" href=\"#{asset_url(path, settings.css_dir)}\" #{params}/>"
end end
end end
end end
Middleman::Features.register :default_helpers, Middleman::Features::DefaultHelpers, { :auto_enable => true }

View file

@ -1,4 +1,4 @@
class Middleman::Features::LiveReload module Middleman::Features::LiveReload
def initialize(app, config) def initialize(app, config)
return unless Middleman::Server.environment == :development return unless Middleman::Server.environment == :development

View file

@ -1,9 +1,10 @@
class Middleman::Features::MinifyCSS module Middleman::Features::MinifyCss
def initialize(app, config) class << self
Middleman::Server.after_feature_init do def registered(app)
app.after_feature_init do
::Compass.configuration.output_style = :compressed ::Compass.configuration.output_style = :compressed
end end
end end
alias :included :registered
end
end end
Middleman::Features.register :minify_css, Middleman::Features::MinifyCSS

View file

@ -1,10 +1,13 @@
class Middleman::Features::MinifyJavascript module Middleman::Features::MinifyJavascript
def initialize(app, config) class << self
Haml::Javascript.send :include, ::Haml::Filters::Base def registered(app)
Middleman::Features::MinifyJavascript::Haml::Javascript.send :include, ::Haml::Filters::Base
require "middleman/features/minify_javascript/rack" require "middleman/features/minify_javascript/rack"
app.use Middleman::Rack::MinifyJavascript app.use Middleman::Rack::MinifyJavascript
end end
alias :included :registered
end
module Haml module Haml
module Javascript module Javascript
@ -16,5 +19,3 @@ class Middleman::Features::MinifyJavascript
end end
end end
end end
Middleman::Features.register :minify_javascript, Middleman::Features::MinifyJavascript

View file

@ -1,5 +1,6 @@
class Middleman::Features::RelativeAssets module Middleman::Features::RelativeAssets
def initialize(app, config) class << self
def registered(app)
::Compass.configuration.relative_assets = true ::Compass.configuration.relative_assets = true
Middleman::Assets.register :relative_assets do |path, prefix, request| Middleman::Assets.register :relative_assets do |path, prefix, request|
@ -30,6 +31,6 @@ class Middleman::Features::RelativeAssets
end end
end end
end end
alias :included :registered
end
end end
Middleman::Features.register :relative_assets, Middleman::Features::RelativeAssets

View file

@ -1,7 +1,8 @@
Entry = Struct.new(:dir, :children) Entry = Struct.new(:dir, :children)
class Middleman::Features::Slickmap module Middleman::Features::Slickmap
def initialize(app, config) class << self
def registered(app)
require 'slickmap' require 'slickmap'
@sitemap_url = config[:url] || "sitemap.html" @sitemap_url = config[:url] || "sitemap.html"
@ -53,6 +54,8 @@ class Middleman::Features::Slickmap
haml "template.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "slickmap")) haml "template.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "slickmap"))
end end
end end
alias :included :registered
end
def self.build_sitemap(&block) def self.build_sitemap(&block)
@@utility = [] @@utility = []
@ -84,5 +87,3 @@ class Middleman::Features::Slickmap
entry entry
end end
end end
Middleman::Features.register :slickmap, Middleman::Features::Slickmap

View file

@ -1,9 +1,10 @@
class Middleman::Features::SmushPngs module Middleman::Features::SmushPNGs
def initialize(app, config) class << self
def registered(app)
require "middleman/builder" require "middleman/builder"
Middleman::Server.alias_method :pre_smush_after_run, :after_run app.alias_method :pre_smush_after_run, :after_run
Middleman::Server.define_method :after_run do app.define_method :after_run do
pre_smush_after_run pre_smush_after_run
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir) smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
@ -34,5 +35,4 @@ class Middleman::Features::SmushPngs
end end
end end
end end
end
Middleman::Features.register :smush_pngs, Middleman::Features::SmushPngs

View file

@ -1,7 +1,8 @@
class Middleman::Features::UglyHaml module Middleman::Features::UglyHaml
def initialize(app, config) class << self
Middleman::Server.set :haml, Middleman::Server.settings.haml.merge({ :ugly_haml => true }) def registered(app)
app.set :haml, app.settings.haml.merge({ :ugly_haml => true })
end
alias :included :registered
end end
end end
Middleman::Features.register :ugly_haml, Middleman::Features::UglyHaml

View file

@ -1,26 +0,0 @@
module Middleman
module Renderers
@@render_method_for_template_types = {}
def self.register(method_name, template_type)
@@render_method_for_template_types[template_type.to_s] = method_name
end
def self.get_method(template_path)
template_type = Tilt[template_path].to_s
@@render_method_for_template_types[template_type]
end
end
end
# Types built into Sinatra
Middleman::Renderers.register(:less, Tilt::LessTemplate)
Middleman::Renderers.register(:haml, Tilt::HamlTemplate)
Middleman::Renderers.register(:builder, Tilt::BuilderTemplate)
Middleman::Renderers.register(:erb, Tilt::ERBTemplate)
%w(haml
sass
coffee).each { |renderer| require "middleman/renderers/#{renderer}" }

View file

@ -1,7 +1,13 @@
class Middleman::Server module Middleman
def coffee(template, options={}, locals={}) module Renderers
options[:layout] = false module CoffeeScript
render :coffee, template, options, locals class << self
def registered(app)
Tilt.register 'coffee', Tilt::CoffeeTemplate
end
alias :included :registered
end
end
end end
end end
@ -22,7 +28,4 @@ unless defined? Tilt::CoffeeTemplate
@output ||= ::CoffeeScript::compile(data, options) @output ||= ::CoffeeScript::compile(data, options)
end end
end end
Tilt.register 'coffee', Tilt::CoffeeTemplate
end end
Middleman::Renderers.register(:coffee, Tilt::CoffeeTemplate)

View file

@ -1,20 +1,18 @@
require "haml" require "haml"
module Middleman module Middleman
module Renderers
module Haml module Haml
class << self
def registered(app)
app.helpers Middleman::Renderers::Haml::Helpers
end
alias :included :registered
end
module Helpers module Helpers
def haml_partial(name, options = {}) def haml_partial(name, options = {})
item_name = name.to_sym partial(name, options)
counter_name = "#{name}_counter".to_sym
if collection = options.delete(:collection)
collection.enum_for(:each_with_index).collect do |item,index|
haml_partial name, options.merge(:locals => {item_name => item, counter_name => index+1})
end.join
elsif object = options.delete(:object)
haml_partial name, options.merge(:locals => {item_name => object, counter_name => nil})
else
haml "_#{name}".to_sym, options.merge(:layout => false)
end
end end
end end
@ -41,7 +39,4 @@ module Middleman
end end
end end
end end
class Middleman::Server
helpers Middleman::Haml::Helpers
end end

View file

@ -1,13 +1,12 @@
require "sass" require "sass"
require "compass" require "compass"
class Middleman::Server module Middleman
def scss(template, options={}, locals={}) module Renderers
options[:layout] = false module Sass
render :scss, template, options, locals class << self
end def registered(app)
app.after_feature_init do
after_feature_init do
::Compass.configuration do |config| ::Compass.configuration do |config|
config.cache_path = File.join(self.root, ".sass-cache") # For sassc files config.cache_path = File.join(self.root, ".sass-cache") # For sassc files
config.project_path = self.root config.project_path = self.root
@ -31,6 +30,11 @@ class Middleman::Server
end end
end end
end end
alias :included :registered
end
end
end
end
class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate
def sass_options def sass_options
@ -45,7 +49,6 @@ class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate
end end
end end
Tilt.register 'sass', Tilt::SassPlusCSSFilenameTemplate Tilt.register 'sass', Tilt::SassPlusCSSFilenameTemplate
Middleman::Renderers.register(:sass, Tilt::SassPlusCSSFilenameTemplate)
class Tilt::ScssPlusCSSFilenameTemplate < Tilt::SassPlusCSSFilenameTemplate class Tilt::ScssPlusCSSFilenameTemplate < Tilt::SassPlusCSSFilenameTemplate
def sass_options def sass_options
@ -53,10 +56,9 @@ class Tilt::ScssPlusCSSFilenameTemplate < Tilt::SassPlusCSSFilenameTemplate
end end
end end
Tilt.register 'scss', Tilt::ScssPlusCSSFilenameTemplate Tilt.register 'scss', Tilt::ScssPlusCSSFilenameTemplate
Middleman::Renderers.register(:scss, Tilt::ScssPlusCSSFilenameTemplate)
module Middleman::Haml module Middleman::Renderers::Haml
module Sass module Sass
include ::Haml::Filters::Base include ::Haml::Filters::Base

View file

@ -1,14 +1,9 @@
# We're riding on Sinatra, so let's include it. # We're riding on Sinatra, so let's include it.
require "sinatra/base" require "sinatra/base"
# The content_for plugin allows Sinatra to use the throw/yield block # Use the padrino project's helpers
# system similar to Rails views. require "padrino-core/application/rendering"
require "sinatra/content_for" require "padrino-helpers"
# Monkey-patch Sinatra to expose the layout parameter
class Sinatra::Request
attr_accessor :layout
end
module Middleman module Middleman
class Server < Sinatra::Base class Server < Sinatra::Base
@ -20,8 +15,7 @@ module Middleman
set :logging, false set :logging, false
set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development set :environment, (ENV['MM_ENV'] && ENV['MM_ENV'].to_sym) || :development
# Import content_for methods # Import padrino helper methods
helpers Sinatra::ContentFor
# Middleman-specific options # Middleman-specific options
set :index_file, "index.html" # What file responds to folder requests set :index_file, "index.html" # What file responds to folder requests
@ -36,20 +30,18 @@ module Middleman
set :build_dir, "build" # Which folder are builds output to set :build_dir, "build" # Which folder are builds output to
set :http_prefix, nil # During build, add a prefix for absolute paths set :http_prefix, nil # During build, add a prefix for absolute paths
# A hash of enabled features # Use Padrino Helpers
@@enabled_features = {} register Padrino::Helpers
set :asset_stamp, false # Disable Padrino cache buster until explicitly enabled
# Override Sinatra's enable to keep track of enabled features # Activate custom features
def self.enable(feature_name, config={}) register Middleman::Features
@@enabled_features[feature_name] = config
super(feature_name)
end
# Disable a feature, then pass to Sinatra's method # Activate built-in helpers
def self.disable(feature_name) register Middleman::Features::DefaultHelpers
@@enabled_features.delete(feature_name)
super(feature_name) # Tilt-aware renderer
end register Padrino::Rendering
# Override Sinatra's set to accept a block # Override Sinatra's set to accept a block
def self.set(option, value=self, &block) def self.set(option, value=self, &block)
@ -68,37 +60,48 @@ module Middleman
@@run_after_features << block @@run_after_features << block
end end
# Activate custom renderers
register Middleman::Renderers::CoffeeScript
register Middleman::Renderers::Haml
register Middleman::Renderers::Sass
# Rack helper for adding mime-types during local preview # Rack helper for adding mime-types during local preview
def self.mime(ext, type) def self.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
end end
# Keep track of a block-specific layout # Default layout name
@@layout = nil layout :layout
def self.current_layout
@layout
end
# Takes a block which allows many pages to have the same layout # Takes a block which allows many pages to have the same layout
# with_layout :admin do # with_layout :admin do
# page "/admin/" # page "/admin/"
# page "/admin/login.html" # page "/admin/login.html"
# end # end
def self.with_layout(layout, &block) def self.with_layout(layout_name, &block)
@@layout = layout old_layout = current_layout
layout(layout_name)
class_eval(&block) if block_given? class_eval(&block) if block_given?
ensure ensure
@@layout = nil layout(old_layout)
end end
# The page method allows the layout to be set on a specific path # The page method allows the layout to be set on a specific path
# page "/about.html", :layout => false # page "/about.html", :layout => false
# page "/", :layout => :homepage_layout # page "/", :layout => :homepage_layout
def self.page(url, options={}, &block) def self.page(url, options={}, &block)
layout = @@layout url << settings.index_file if url.match(%r{/$})
layout = options[:layout] if !options[:layout].nil?
options[:layout] ||= current_layout
get(url) do get(url) do
return yield if block_given? return yield if block_given?
process_request(layout) process_request(options)
end end
end end
@ -109,20 +112,21 @@ module Middleman
private private
# Internal method to look for templates and evaluate them if found # Internal method to look for templates and evaluate them if found
def process_request(layout = :layout) def process_request(options={})
# Normalize the path and add index if we're looking at a directory # Normalize the path and add index if we're looking at a directory
path = request.path path = request.path
path << settings.index_file if path.match(%r{/$}) path << settings.index_file if path.match(%r{/$})
path.gsub!(%r{^/}, '') path.gsub!(%r{^/}, '')
if template_path = Dir.glob(File.join(settings.views, "#{path}.*")).first old_layout = settings.current_layout
content_type mime_type(File.extname(path)), :charset => 'utf-8' settings.layout(options[:layout]) if !options[:layout].nil?
result = render(path, :layout => settings.fetch_layout_path.to_sym)
settings.layout(old_layout)
renderer = Middleman::Renderers.get_method(template_path) if result
if respond_to? renderer content_type mime_type(File.extname(path)), :charset => 'utf-8'
status 200 status 200
return send(renderer, path.to_sym, { :layout => layout }) return result
end
end end
status 404 status 404
@ -131,8 +135,6 @@ module Middleman
end end
require "middleman/assets" require "middleman/assets"
require "middleman/renderers"
require "middleman/features"
# The Rack App # The Rack App
class Middleman::Server class Middleman::Server
@ -152,13 +154,6 @@ class Middleman::Server
set :app_file, File.expand_path(local_config) set :app_file, File.expand_path(local_config)
end end
# loop over enabled feature
@@enabled_features.each do |feature_name, feature_config|
next unless send(:"#{feature_name}?")
$stderr.puts "== Enabling: #{feature_name.to_s.capitalize}" if logging?
Middleman::Features.run(feature_name, feature_config, self)
end
use ::Rack::ConditionalGet if environment == :development use ::Rack::ConditionalGet if environment == :development
@@run_after_features.each { |block| class_eval(&block) } @@run_after_features.each { |block| class_eval(&block) }

View file

@ -1,8 +1,8 @@
# Automatic sitemaps # Automatic sitemaps
# enable :slickmap # activate :slickmap
# Automatic image dimension calculations # Automatic image dimension calculations
# enable :automatic_image_sizes # activate :automatic_image_sizes
# Per-page layout changes # Per-page layout changes
# With no layout # With no layout
@ -41,16 +41,16 @@ set :images_dir, "<%= images_dir -%>"
# Build-specific configuration # Build-specific configuration
configure :build do configure :build do
# For example, change the Compass output style for deployment # For example, change the Compass output style for deployment
# enable :minify_css # activate :minify_css
# Minify Javascript on build # Minify Javascript on build
# enable :minify_javascript # activate :minify_javascript
# Shrink/smush PNG/JPEGs on build # Shrink/smush PNG/JPEGs on build
# enable :smush_pngs # activate :smush_pngs
# Enable cache buster # Enable cache buster
# enable :cache_buster # activate :cache_buster
# Or use a different image path # Or use a different image path
# set :http_path, "/Content/images/" # set :http_path, "/Content/images/"