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
alias :included :registered
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
# Initialize a feature def enable(feature_name)
def self.run(feature_name, feature_config, scope) $stderr.puts "Warning: Feature activation has been renamed from enable to activate"
feature_class = @@features[feature_name] activate(feature_name)
feature_class.new(scope, feature_config) unless feature_class.nil? super(feature_name)
end end
# Get a list of all features
def self.all
@@features
end
end end
end end
# livereload
%w(default_helpers
asset_host
automatic_image_sizes
cache_buster
minify_css
minify_javascript
relative_assets
slickmap
smush_pngs
ugly_haml).each do |feature|
require File.join("middleman/features", feature)
end

View file

@ -1,21 +1,22 @@
class Middleman::Features::AssetHost module Middleman::Features::AssetHost
def initialize(app, config) class << self
Middleman::Server.after_feature_init do def registered(app)
if Middleman::Server.asset_host.is_a?(Proc) app.after_feature_init do
::Compass.configuration.asset_host(&Middleman::Server.asset_host) if Middleman::Server.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&Middleman::Server.asset_host)
end
end
Middleman::Assets.register :asset_host do |path, prefix, request|
original_output = Middleman::Assets.before(:asset_host, path, prefix, request)
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
asset_prefix = Middleman::Server.asset_host.call(original_output)
File.join(asset_prefix, original_output)
end end
end end
alias :included :registered
Middleman::Assets.register :asset_host do |path, prefix, request|
original_output = Middleman::Assets.before(:asset_host, path, prefix, request)
valid_extensions = %w(.png .gif .jpg .jpeg .js .css)
asset_prefix = Middleman::Server.asset_host.call(original_output)
File.join(asset_prefix, original_output)
end
end end
end end
Middleman::Features.register :asset_host, Middleman::Features::AssetHost

View file

@ -1,31 +1,33 @@
class Middleman::Features::AutomaticImageSizes module Middleman::Features::AutomaticImageSizes
def initialize(app, config) class << self
require "middleman/features/automatic_image_sizes/fastimage" def registered(app)
require "middleman/features/automatic_image_sizes/fastimage"
Middleman::Server.helpers do app.helpers Helpers
alias_method :pre_automatic_image_tag, :image_tag end
def image_tag(path, params={}) alias :included :registered
if (!params[:width] || !params[:height]) && !path.include?("://") end
params[:alt] ||= ""
http_prefix = settings.http_images_path rescue settings.images_dir
begin module Helpers
real_path = File.join(settings.public, settings.images_dir, path) def image_tag(path, params={})
if File.exists? real_path if (!params[:width] || !params[:height]) && !path.include?("://")
dimensions = ::FastImage.size(real_path, :raise_on_failure => true) params[:alt] ||= ""
params[:width] ||= dimensions[0] http_prefix = settings.http_images_path rescue settings.images_dir
params[:height] ||= dimensions[1]
end begin
rescue real_path = File.join(settings.public, settings.images_dir, path)
if File.exists? real_path
dimensions = ::FastImage.size(real_path, :raise_on_failure => true)
params[:width] ||= dimensions[0]
params[:height] ||= dimensions[1]
end end
rescue
capture_haml { haml_tag(:img, params.merge(:src => asset_url(path, http_prefix))) }
else
pre_automatic_image_tag(path, params)
end end
super(asset_url(path, http_prefix), params)
else
super(path, params)
end end
end end
end end
end end
Middleman::Features.register :automatic_image_sizes, Middleman::Features::AutomaticImageSizes

View file

@ -1,43 +1,44 @@
class Middleman::Features::CacheBuster module Middleman::Features::CacheBuster
def initialize(app, config) class << self
Middleman::Assets.register :cache_buster do |path, prefix, request| def registered(app)
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request) Middleman::Assets.register :cache_buster do |path, prefix, request|
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request)
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path)) if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
http_path http_path
else else
begin begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
rescue rescue
end
real_path_static = File.join(Middleman::Server.public, prefix, path)
if File.readable?(real_path_static)
http_path << "?" + File.mtime(real_path_static).strftime("%s")
elsif Middleman::Server.environment == :build
real_path_dynamic = File.join(Middleman::Server.root, Middleman::Server.build_dir, prefix, path)
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
end
http_path
end end
real_path_static = File.join(Middleman::Server.public, prefix, path)
if File.readable?(real_path_static)
http_path << "?" + File.mtime(real_path_static).strftime("%s")
elsif Middleman::Server.environment == :build
real_path_dynamic = File.join(Middleman::Server.root, Middleman::Server.build_dir, prefix, path)
http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
end
http_path
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
real_path = real_path.gsub(File.join(Middleman::Server.root, Middleman::Server.build_dir), Middleman::Server.public) real_path = real_path.gsub(File.join(Middleman::Server.root, Middleman::Server.build_dir), Middleman::Server.public)
if File.readable?(real_path) if File.readable?(real_path)
File.mtime(real_path).strftime("%s") File.mtime(real_path).strftime("%s")
else else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
end
end end
end end
end end
end end
alias :included :registered
end 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
end when :css then settings.css_dir
when :js then settings.js_dir
def image_tag(path, params={}) when :images then settings.images_dir
params[:alt] ||= "" else kind.to_s
prefix = settings.http_images_path rescue settings.images_dir end
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') source = source.to_s.gsub(/\s/, '')
params << " " if params.length > 0 ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension
"<img src=\"#{asset_url(path, prefix)}\" #{params}/>" source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
end result_path = source if source =~ %r{^/} # absolute path
result_path ||= asset_url(source, asset_folder)
def javascript_include_tag(path, params={}) timestamp = asset_timestamp(result_path)
path = path.to_s "#{result_path}#{timestamp}"
path << ".js" unless path =~ /\.js$/ end
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
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)
::Compass.configuration.output_style = :compressed app.after_feature_init do
::Compass.configuration.output_style = :compressed
end
end end
alias :included :registered
end end
end end
Middleman::Features.register :minify_css, Middleman::Features::MinifyCSS

View file

@ -1,9 +1,12 @@
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
alias :included :registered
end end
module Haml module Haml
@ -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,35 +1,36 @@
class Middleman::Features::RelativeAssets module Middleman::Features::RelativeAssets
def initialize(app, config) class << self
::Compass.configuration.relative_assets = true def registered(app)
::Compass.configuration.relative_assets = true
Middleman::Assets.register :relative_assets do |path, prefix, request| Middleman::Assets.register :relative_assets do |path, prefix, request|
begin begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
rescue rescue
end end
if path.include?("://") if path.include?("://")
Middleman::Assets.before(:relative_assets, path, prefix, request) Middleman::Assets.before(:relative_assets, path, prefix, request)
elsif path[0,1] == "/" elsif path[0,1] == "/"
path
else
path = File.join(prefix, path) if prefix.length > 0
request_path = request.path_info.dup
request_path << Middleman::Server.index_file if path.match(%r{/$})
request_path.gsub!(%r{^/}, '')
parts = request_path.split('/')
if parts.length > 1
arry = []
(parts.length - 1).times { arry << ".." }
arry << path
File.join(*arry)
else
path path
else
path = File.join(prefix, path) if prefix.length > 0
request_path = request.path_info.dup
request_path << Middleman::Server.index_file if path.match(%r{/$})
request_path.gsub!(%r{^/}, '')
parts = request_path.split('/')
if parts.length > 1
arry = []
(parts.length - 1).times { arry << ".." }
arry << path
File.join(*arry)
else
path
end
end end
end end
end end
alias :included :registered
end end
end end
Middleman::Features.register :relative_assets, Middleman::Features::RelativeAssets

View file

@ -1,57 +1,60 @@
Entry = Struct.new(:dir, :children) Entry = Struct.new(:dir, :children)
class Middleman::Features::Slickmap module Middleman::Features::Slickmap
def initialize(app, config) class << self
require 'slickmap' def registered(app)
require 'slickmap'
@sitemap_url = config[:url] || "sitemap.html" @sitemap_url = config[:url] || "sitemap.html"
if Middleman::Server.environment == :build if Middleman::Server.environment == :build
Middleman::Builder.template :slickmap, @sitemap_url, @sitemap_url Middleman::Builder.template :slickmap, @sitemap_url, @sitemap_url
end end
Middleman::Server.helpers do Middleman::Server.helpers do
def sitemap_node(n, first=false) def sitemap_node(n, first=false)
if n.children.length < 1 if n.children.length < 1
if !first && File.extname(n.dir).length > 0 if !first && File.extname(n.dir).length > 0
haml_tag :li do haml_tag :li do
path = n.dir.gsub(self.class.views, '') path = n.dir.gsub(self.class.views, '')
haml_concat link_to(File.basename(path), path) haml_concat link_to(File.basename(path), path)
end
end end
end else
else haml_tag(:li, :id => first ? "home" : nil) do
haml_tag(:li, :id => first ? "home" : nil) do
if first
haml_concat link_to("Homepage", "/" + self.class.index_file)
else
# we are a dir
index = n.children.find { |c| c.dir.include?(self.class.index_file) }
haml_concat link_to(index.dir.gsub(self.class.views + "/", '').gsub("/" + File.basename(index.dir), '').capitalize, index.dir.gsub(self.class.views, ''))
end
other_children = n.children.select { |c| !c.dir.include?(self.class.index_file) }
if other_children.length > 0
if first if first
other_children.each { |i| sitemap_node(i) } haml_concat link_to("Homepage", "/" + self.class.index_file)
else else
haml_tag :ul do # we are a dir
index = n.children.find { |c| c.dir.include?(self.class.index_file) }
haml_concat link_to(index.dir.gsub(self.class.views + "/", '').gsub("/" + File.basename(index.dir), '').capitalize, index.dir.gsub(self.class.views, ''))
end
other_children = n.children.select { |c| !c.dir.include?(self.class.index_file) }
if other_children.length > 0
if first
other_children.each { |i| sitemap_node(i) } other_children.each { |i| sitemap_node(i) }
else
haml_tag :ul do
other_children.each { |i| sitemap_node(i) }
end
end end
end end
end end
end end
end end
end end
end
Middleman::Server.get "/#{@sitemap_url}" do Middleman::Server.get "/#{@sitemap_url}" do
# Return :utility to put it util top menu. False to ignore # Return :utility to put it util top menu. False to ignore
@tree, @utility = Middleman::Features::Slickmap.build_sitemap do |file_name| @tree, @utility = Middleman::Features::Slickmap.build_sitemap do |file_name|
:valid :valid
end
haml "template.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "slickmap"))
end end
haml "template.html".to_sym, :layout => false, :views => File.expand_path(File.join(File.dirname(__FILE__), "slickmap"))
end end
alias :included :registered
end end
def self.build_sitemap(&block) def self.build_sitemap(&block)
@ -84,5 +87,3 @@ class Middleman::Features::Slickmap
entry entry
end end
end end
Middleman::Features.register :slickmap, Middleman::Features::Slickmap

View file

@ -1,38 +1,38 @@
class Middleman::Features::SmushPngs module Middleman::Features::SmushPNGs
def initialize(app, config) class << self
require "middleman/builder" def registered(app)
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)
# Read cache # Read cache
cache_file = File.join(Middleman::Server.root, ".smush-cache") cache_file = File.join(Middleman::Server.root, ".smush-cache")
cache_data = if File.exists?(cache_file) cache_data = if File.exists?(cache_file)
Marshal.restore(File.read(cache_file)) Marshal.restore(File.read(cache_file))
else else
{} {}
end end
require "smusher" require "smusher"
require "json/pure" require "json/pure"
::Smusher.class_eval do ::Smusher.class_eval do
images_in_folder(smush_dir).each do |file| images_in_folder(smush_dir).each do |file|
original_file_size = size(file) original_file_size = size(file)
return if original_file_size.zero? return if original_file_size.zero?
return if cache_data[file] && cache_data[file] == original_file_size return if cache_data[file] && cache_data[file] == original_file_size
with_logging(file, true) do with_logging(file, true) do
write_optimized_data(file) write_optimized_data(file)
cache_data[file] = size(file) # Add or update cache cache_data[file] = size(file) # Add or update cache
File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache File.open(cache_file, "w") { |f| f.write Marshal.dump(cache_data) } # Write cache
say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Server.build_dir+"/", '') say "<%= color('#{"[SMUSHED]".rjust(12)}', :yellow) %> " + file.gsub(Middleman::Server.build_dir+"/", '')
end
end end
end end
end 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,47 +1,42 @@
require "haml" require "haml"
module Middleman module Middleman
module Haml module Renderers
module Helpers module Haml
def haml_partial(name, options = {}) class << self
item_name = name.to_sym def registered(app)
counter_name = "#{name}_counter".to_sym app.helpers Middleman::Renderers::Haml::Helpers
if collection = options.delete(:collection) end
collection.enum_for(:each_with_index).collect do |item,index| alias :included :registered
haml_partial name, options.merge(:locals => {item_name => item, counter_name => index+1}) end
end.join
elsif object = options.delete(:object) module Helpers
haml_partial name, options.merge(:locals => {item_name => object, counter_name => nil}) def haml_partial(name, options = {})
else partial(name, options)
haml "_#{name}".to_sym, options.merge(:layout => false)
end end
end end
end
module Table module Table
include ::Haml::Filters::Base include ::Haml::Filters::Base
def render(text) def render(text)
output = '<div class="table"><table cellspacing="0" cellpadding="0">' output = '<div class="table"><table cellspacing="0" cellpadding="0">'
line_num = 0 line_num = 0
text.each_line do |line| text.each_line do |line|
line_num += 1 line_num += 1
next if line.strip.empty? next if line.strip.empty?
output << %Q{<tr class="#{(line_num % 2 == 0) ? "even" : "odd" }#{(line_num == 1) ? " first" : "" }">} output << %Q{<tr class="#{(line_num % 2 == 0) ? "even" : "odd" }#{(line_num == 1) ? " first" : "" }">}
columns = line.split("|").map { |p| p.strip } columns = line.split("|").map { |p| p.strip }
columns.each_with_index do |col, i| columns.each_with_index do |col, i|
output << %Q{<td class="col#{i+1}">#{col}</td>} output << %Q{<td class="col#{i+1}">#{col}</td>}
end
output << "</tr>"
end end
output + "</table></div>"
output << "</tr>"
end end
output + "</table></div>"
end end
end end
end end
end end
class Middleman::Server
helpers Middleman::Haml::Helpers
end

View file

@ -1,32 +1,36 @@
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
::Compass.configuration do |config|
config.cache_path = File.join(self.root, ".sass-cache") # For sassc files
config.project_path = self.root
config.sass_dir = File.join(File.basename(self.views), self.css_dir)
config.output_style = :nested
config.fonts_dir = File.join(File.basename(self.public), self.fonts_dir)
config.css_dir = File.join(File.basename(self.public), self.css_dir)
config.images_dir = File.join(File.basename(self.public), self.images_dir)
config.http_images_path = self.http_images_path rescue File.join(self.http_prefix || "/", self.images_dir)
config.http_stylesheets_path = self.http_css_path rescue File.join(self.http_prefix || "/", self.css_dir)
config.asset_cache_buster { false }
after_feature_init do config.add_import_path(config.sass_dir)
::Compass.configuration do |config| end
config.cache_path = File.join(self.root, ".sass-cache") # For sassc files
config.project_path = self.root
config.sass_dir = File.join(File.basename(self.views), self.css_dir)
config.output_style = :nested
config.fonts_dir = File.join(File.basename(self.public), self.fonts_dir)
config.css_dir = File.join(File.basename(self.public), self.css_dir)
config.images_dir = File.join(File.basename(self.public), self.images_dir)
config.http_images_path = self.http_images_path rescue File.join(self.http_prefix || "/", self.images_dir)
config.http_stylesheets_path = self.http_css_path rescue File.join(self.http_prefix || "/", self.css_dir)
config.asset_cache_buster { false }
config.add_import_path(config.sass_dir) configure :build do
end ::Compass.configuration do |config|
config.css_dir = File.join(File.basename(self.build_dir), self.css_dir)
configure :build do config.images_dir = File.join(File.basename(self.build_dir), self.images_dir)
::Compass.configuration do |config| end
config.css_dir = File.join(File.basename(self.build_dir), self.css_dir) end
config.images_dir = File.join(File.basename(self.build_dir), self.images_dir) end
end
alias :included :registered
end end
end end
end end
@ -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/"