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:
parent
4ba90bf1c7
commit
4d0567cf40
33 changed files with 421 additions and 487 deletions
3
Rakefile
3
Rakefile
|
@ -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")
|
||||
|
|
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
0.99.1.pre
|
||||
0.99.2.pre
|
||||
|
|
|
@ -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" />'
|
10
features/padrino_helpers.feature
Normal file
10
features/padrino_helpers.feature
Normal 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"'
|
|
@ -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"
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
= image_tag "test.png"
|
||||
= image_tag "test2.png", :alt => "alt"
|
|
@ -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"
|
|
@ -1,3 +0,0 @@
|
|||
= link_to "No Href"
|
||||
= link_to "Has Href", "test.com"
|
||||
= link_to "Has param", "test2.com", :class => "test"
|
|
@ -1 +0,0 @@
|
|||
# Hello Maruku {.header}
|
5
fixtures/test-app/views/padrino_test.html.haml
Normal file
5
fixtures/test-app/views/padrino_test.html.haml
Normal 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"
|
|
@ -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"
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
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?
|
||||
end
|
||||
|
||||
# Get a list of all features
|
||||
def self.all
|
||||
@@features
|
||||
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
|
||||
|
||||
# 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)
|
||||
|
||||
def enable(feature_name)
|
||||
$stderr.puts "Warning: Feature activation has been renamed from enable to activate"
|
||||
activate(feature_name)
|
||||
super(feature_name)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,7 @@
|
|||
class Middleman::Features::AssetHost
|
||||
def initialize(app, config)
|
||||
Middleman::Server.after_feature_init do
|
||||
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
|
||||
|
@ -16,6 +17,6 @@ class Middleman::Features::AssetHost
|
|||
File.join(asset_prefix, original_output)
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :asset_host, Middleman::Features::AssetHost
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
class Middleman::Features::AutomaticImageSizes
|
||||
def initialize(app, config)
|
||||
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
|
||||
app.helpers Helpers
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module Helpers
|
||||
def image_tag(path, params={})
|
||||
if (!params[:width] || !params[:height]) && !path.include?("://")
|
||||
params[:alt] ||= ""
|
||||
|
@ -19,13 +24,10 @@ class Middleman::Features::AutomaticImageSizes
|
|||
rescue
|
||||
end
|
||||
|
||||
capture_haml { haml_tag(:img, params.merge(:src => asset_url(path, http_prefix))) }
|
||||
super(asset_url(path, http_prefix), params)
|
||||
else
|
||||
pre_automatic_image_tag(path, params)
|
||||
super(path, params)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :automatic_image_sizes, Middleman::Features::AutomaticImageSizes
|
|
@ -1,5 +1,6 @@
|
|||
class Middleman::Features::CacheBuster
|
||||
def initialize(app, config)
|
||||
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)
|
||||
|
||||
|
@ -24,7 +25,7 @@ class Middleman::Features::CacheBuster
|
|||
end
|
||||
end
|
||||
|
||||
Middleman::Server.after_feature_init do
|
||||
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
|
||||
|
@ -38,6 +39,6 @@ class Middleman::Features::CacheBuster
|
|||
end
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :cache_buster, Middleman::Features::CacheBuster
|
|
@ -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>}
|
||||
# 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
|
||||
|
||||
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}/>"
|
||||
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 }
|
|
@ -1,4 +1,4 @@
|
|||
class Middleman::Features::LiveReload
|
||||
module Middleman::Features::LiveReload
|
||||
def initialize(app, config)
|
||||
return unless Middleman::Server.environment == :development
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
class Middleman::Features::MinifyCSS
|
||||
def initialize(app, config)
|
||||
Middleman::Server.after_feature_init do
|
||||
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
|
|
@ -1,10 +1,13 @@
|
|||
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
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
module Haml
|
||||
module Javascript
|
||||
|
@ -16,5 +19,3 @@ class Middleman::Features::MinifyJavascript
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :minify_javascript, Middleman::Features::MinifyJavascript
|
|
@ -1,5 +1,6 @@
|
|||
class Middleman::Features::RelativeAssets
|
||||
def initialize(app, config)
|
||||
module Middleman::Features::RelativeAssets
|
||||
class << self
|
||||
def registered(app)
|
||||
::Compass.configuration.relative_assets = true
|
||||
|
||||
Middleman::Assets.register :relative_assets do |path, prefix, request|
|
||||
|
@ -30,6 +31,6 @@ class Middleman::Features::RelativeAssets
|
|||
end
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :relative_assets, Middleman::Features::RelativeAssets
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
Entry = Struct.new(:dir, :children)
|
||||
|
||||
class Middleman::Features::Slickmap
|
||||
def initialize(app, config)
|
||||
module Middleman::Features::Slickmap
|
||||
class << self
|
||||
def registered(app)
|
||||
require 'slickmap'
|
||||
|
||||
@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"))
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
|
||||
def self.build_sitemap(&block)
|
||||
@@utility = []
|
||||
|
@ -84,5 +87,3 @@ class Middleman::Features::Slickmap
|
|||
entry
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :slickmap, Middleman::Features::Slickmap
|
|
@ -1,9 +1,10 @@
|
|||
class Middleman::Features::SmushPngs
|
||||
def initialize(app, config)
|
||||
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
|
||||
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)
|
||||
|
||||
|
@ -34,5 +35,4 @@ class Middleman::Features::SmushPngs
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
Middleman::Features.register :smush_pngs, Middleman::Features::SmushPngs
|
||||
end
|
|
@ -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
|
|
@ -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}" }
|
|
@ -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)
|
|
@ -1,20 +1,18 @@
|
|||
require "haml"
|
||||
|
||||
module Middleman
|
||||
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 = {})
|
||||
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)
|
||||
end
|
||||
partial(name, options)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -41,7 +39,4 @@ module Middleman
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Middleman::Server
|
||||
helpers Middleman::Haml::Helpers
|
||||
end
|
|
@ -1,13 +1,12 @@
|
|||
require "sass"
|
||||
require "compass"
|
||||
|
||||
class Middleman::Server
|
||||
def scss(template, options={}, locals={})
|
||||
options[:layout] = false
|
||||
render :scss, template, options, locals
|
||||
end
|
||||
|
||||
after_feature_init do
|
||||
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
|
||||
|
@ -31,6 +30,11 @@ class Middleman::Server
|
|||
end
|
||||
end
|
||||
end
|
||||
alias :included :registered
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Tilt::SassPlusCSSFilenameTemplate < Tilt::SassTemplate
|
||||
def sass_options
|
||||
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
if result
|
||||
content_type mime_type(File.extname(path)), :charset => 'utf-8'
|
||||
status 200
|
||||
return send(renderer, path.to_sym, { :layout => layout })
|
||||
end
|
||||
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) }
|
||||
|
|
|
@ -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/"
|
||||
|
|
Loading…
Reference in a new issue