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("templater", "~>1.0.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("yui-compressor", "~>0.9.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
Given the Server is running
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
Middleman::Server.enable :asset_host
Middleman::Server.activate :asset_host
Middleman::Server.set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4)
end

View file

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

View file

@ -1,41 +1,34 @@
module Middleman
module Features
# Top-level method to register a new feature
@@features = {}
def self.register(feature_name, feature_class=nil, options={})
@@features[feature_name] = feature_class
module Middleman::Features
autoload :RelativeAssets, "middleman/features/relative_assets"
autoload :AssetHost, "middleman/features/asset_host"
autoload :CacheBuster, "middleman/features/cache_buster"
autoload :DefaultHelpers, "middleman/features/default_helpers"
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
activate_method = (options.has_key?(:auto_enable) && options[:auto_enable]) ? :enable : :disable
Middleman::Server.send(activate_method, feature_name)
class << self
def registered(app)
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
# 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?
def enable(feature_name)
$stderr.puts "Warning: Feature activation has been renamed from enable to activate"
activate(feature_name)
super(feature_name)
end
# Get a list of all features
def self.all
@@features
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
def initialize(app, config)
Middleman::Server.after_feature_init do
if Middleman::Server.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&Middleman::Server.asset_host)
module Middleman::Features::AssetHost
class << self
def registered(app)
app.after_feature_init do
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
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
alias :included :registered
end
end
Middleman::Features.register :asset_host, Middleman::Features::AssetHost

View file

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

View file

@ -1,43 +1,44 @@
class Middleman::Features::CacheBuster
def initialize(app, config)
Middleman::Assets.register :cache_buster do |path, prefix, request|
http_path = Middleman::Assets.before(:cache_buster, path, prefix, request)
module Middleman::Features::CacheBuster
class << self
def registered(app)
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))
http_path
else
begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
rescue
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
http_path
else
begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
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
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
Middleman::Server.after_feature_init do
::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path|
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)
if File.readable?(real_path)
File.mtime(real_path).strftime("%s")
else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
app.after_feature_init do
::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path|
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)
if File.readable?(real_path)
File.mtime(real_path).strftime("%s")
else
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
end
end
end
end
end
alias :included :registered
end
end
Middleman::Features.register :cache_buster, Middleman::Features::CacheBuster

View file

@ -1,6 +1,9 @@
class Middleman::Features::DefaultHelpers
def initialize(app, config)
Middleman::Server.helpers Helpers
module Middleman::Features::DefaultHelpers
class << self
def registered(app)
app.helpers Middleman::Features::DefaultHelpers::Helpers
end
alias :included :registered
end
module Helpers
@ -14,8 +17,9 @@ class Middleman::Features::DefaultHelpers
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")
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"
end
end
@ -36,43 +40,22 @@ class Middleman::Features::DefaultHelpers
Middleman::Assets.get_url(path, prefix, request)
end
def link_to(title, url="#", params={})
params.merge!(:href => url)
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
%Q{<a #{params}>#{title}</a>}
end
def image_tag(path, params={})
params[:alt] ||= ""
prefix = settings.http_images_path rescue settings.images_dir
params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
params << " " if params.length > 0
"<img src=\"#{asset_url(path, prefix)}\" #{params}/>"
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
# Padrino's asset handling needs to pass through ours
def asset_path(kind, source)
return source if source =~ /^http/
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
source = source.to_s.gsub(/\s/, '')
ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
result_path = source if source =~ %r{^/} # absolute path
result_path ||= asset_url(source, asset_folder)
timestamp = asset_timestamp(result_path)
"#{result_path}#{timestamp}"
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)
return unless Middleman::Server.environment == :development

View file

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

View file

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

View file

@ -1,35 +1,36 @@
class Middleman::Features::RelativeAssets
def initialize(app, config)
::Compass.configuration.relative_assets = true
module Middleman::Features::RelativeAssets
class << self
def registered(app)
::Compass.configuration.relative_assets = true
Middleman::Assets.register :relative_assets do |path, prefix, request|
begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
rescue
end
Middleman::Assets.register :relative_assets do |path, prefix, request|
begin
prefix = Middleman::Server.images_dir if prefix == Middleman::Server.http_images_path
rescue
end
if path.include?("://")
Middleman::Assets.before(:relative_assets, path, prefix, request)
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
if path.include?("://")
Middleman::Assets.before(:relative_assets, path, prefix, request)
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
end
end
end
end
alias :included :registered
end
end
Middleman::Features.register :relative_assets, Middleman::Features::RelativeAssets

View file

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

View file

@ -1,38 +1,38 @@
class Middleman::Features::SmushPngs
def initialize(app, config)
require "middleman/builder"
module Middleman::Features::SmushPNGs
class << self
def registered(app)
require "middleman/builder"
Middleman::Server.alias_method :pre_smush_after_run, :after_run
Middleman::Server.define_method :after_run do
pre_smush_after_run
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
app.alias_method :pre_smush_after_run, :after_run
app.define_method :after_run do
pre_smush_after_run
smush_dir = File.join(Middleman::Server.build_dir, Middleman::Server.images_dir)
# Read cache
cache_file = File.join(Middleman::Server.root, ".smush-cache")
cache_data = if File.exists?(cache_file)
Marshal.restore(File.read(cache_file))
else
{}
end
# Read cache
cache_file = File.join(Middleman::Server.root, ".smush-cache")
cache_data = if File.exists?(cache_file)
Marshal.restore(File.read(cache_file))
else
{}
end
require "smusher"
require "json/pure"
::Smusher.class_eval do
images_in_folder(smush_dir).each do |file|
original_file_size = size(file)
return if original_file_size.zero?
return if cache_data[file] && cache_data[file] == original_file_size
require "smusher"
require "json/pure"
::Smusher.class_eval do
images_in_folder(smush_dir).each do |file|
original_file_size = size(file)
return if original_file_size.zero?
return if cache_data[file] && cache_data[file] == original_file_size
with_logging(file, true) do
write_optimized_data(file)
cache_data[file] = size(file) # Add or update 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+"/", '')
with_logging(file, true) do
write_optimized_data(file)
cache_data[file] = size(file) # Add or update 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+"/", '')
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
def initialize(app, config)
Middleman::Server.set :haml, Middleman::Server.settings.haml.merge({ :ugly_haml => true })
module Middleman::Features::UglyHaml
class << self
def registered(app)
app.set :haml, app.settings.haml.merge({ :ugly_haml => true })
end
alias :included :registered
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
def coffee(template, options={}, locals={})
options[:layout] = false
render :coffee, template, options, locals
module Middleman
module Renderers
module CoffeeScript
class << self
def registered(app)
Tilt.register 'coffee', Tilt::CoffeeTemplate
end
alias :included :registered
end
end
end
end
@ -22,7 +28,4 @@ unless defined? Tilt::CoffeeTemplate
@output ||= ::CoffeeScript::compile(data, options)
end
end
Tilt.register 'coffee', Tilt::CoffeeTemplate
end
Middleman::Renderers.register(:coffee, Tilt::CoffeeTemplate)

View file

@ -1,47 +1,42 @@
require "haml"
module Middleman
module Haml
module Helpers
def haml_partial(name, options = {})
item_name = name.to_sym
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)
module Renderers
module Haml
class << self
def registered(app)
app.helpers Middleman::Renderers::Haml::Helpers
end
alias :included :registered
end
module Helpers
def haml_partial(name, options = {})
partial(name, options)
end
end
end
module Table
include ::Haml::Filters::Base
module Table
include ::Haml::Filters::Base
def render(text)
output = '<div class="table"><table cellspacing="0" cellpadding="0">'
line_num = 0
text.each_line do |line|
line_num += 1
next if line.strip.empty?
output << %Q{<tr class="#{(line_num % 2 == 0) ? "even" : "odd" }#{(line_num == 1) ? " first" : "" }">}
def render(text)
output = '<div class="table"><table cellspacing="0" cellpadding="0">'
line_num = 0
text.each_line do |line|
line_num += 1
next if line.strip.empty?
output << %Q{<tr class="#{(line_num % 2 == 0) ? "even" : "odd" }#{(line_num == 1) ? " first" : "" }">}
columns = line.split("|").map { |p| p.strip }
columns.each_with_index do |col, i|
output << %Q{<td class="col#{i+1}">#{col}</td>}
columns = line.split("|").map { |p| p.strip }
columns.each_with_index do |col, i|
output << %Q{<td class="col#{i+1}">#{col}</td>}
end
output << "</tr>"
end
output << "</tr>"
output + "</table></div>"
end
output + "</table></div>"
end
end
end
end
class Middleman::Server
helpers Middleman::Haml::Helpers
end

View file

@ -1,32 +1,36 @@
require "sass"
require "compass"
class Middleman::Server
def scss(template, options={}, locals={})
options[:layout] = false
render :scss, template, options, locals
end
module Middleman
module Renderers
module Sass
class << self
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
::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 }
config.add_import_path(config.sass_dir)
end
config.add_import_path(config.sass_dir)
end
configure :build do
::Compass.configuration do |config|
config.css_dir = File.join(File.basename(self.build_dir), self.css_dir)
config.images_dir = File.join(File.basename(self.build_dir), self.images_dir)
configure :build do
::Compass.configuration do |config|
config.css_dir = File.join(File.basename(self.build_dir), self.css_dir)
config.images_dir = File.join(File.basename(self.build_dir), self.images_dir)
end
end
end
end
alias :included :registered
end
end
end
@ -45,7 +49,6 @@ class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate
end
end
Tilt.register 'sass', Tilt::SassPlusCSSFilenameTemplate
Middleman::Renderers.register(:sass, Tilt::SassPlusCSSFilenameTemplate)
class Tilt::ScssPlusCSSFilenameTemplate < Tilt::SassPlusCSSFilenameTemplate
def sass_options
@ -53,10 +56,9 @@ class Tilt::ScssPlusCSSFilenameTemplate < Tilt::SassPlusCSSFilenameTemplate
end
end
Tilt.register 'scss', Tilt::ScssPlusCSSFilenameTemplate
Middleman::Renderers.register(:scss, Tilt::ScssPlusCSSFilenameTemplate)
module Middleman::Haml
module Middleman::Renderers::Haml
module Sass
include ::Haml::Filters::Base

View file

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

View file

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