From 4d0567cf40edfc1e11b63b94797efc1a6d5d94d9 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Mon, 6 Sep 2010 18:48:25 -0700 Subject: [PATCH] Now riding on Sinatra extensions and using Padrino rendering --- Rakefile | 3 +- VERSION | 2 +- features/helpers_simple_macros.feature | 31 ------- features/padrino_helpers.feature | 10 ++ features/scss-support.feature | 2 +- features/step_definitions/asset_host_steps.rb | 2 +- features/step_definitions/middleman_steps.rb | 5 +- fixtures/test-app/views/image_tag.html.haml | 2 - .../views/javascript_include_tag.html.haml | 4 - .../test-app/views/{ => layouts}/custom.haml | 0 fixtures/test-app/views/link_to.html.haml | 3 - fixtures/test-app/views/maruku.html.maruku | 1 - .../test-app/views/padrino_test.html.haml | 5 + .../views/stylesheet_link_tag.html.haml | 4 - lib/middleman.rb | 10 +- lib/middleman/features.rb | 69 +++++++------- lib/middleman/features/asset_host.rb | 37 ++++---- .../features/automatic_image_sizes.rb | 50 +++++----- lib/middleman/features/cache_buster.rb | 71 +++++++------- lib/middleman/features/default_helpers.rb | 69 ++++++-------- lib/middleman/features/livereload.rb | 2 +- lib/middleman/features/minify_css.rb | 15 +-- lib/middleman/features/minify_javascript.rb | 21 +++-- lib/middleman/features/relative_assets.rb | 59 ++++++------ lib/middleman/features/slickmap.rb | 91 +++++++++--------- lib/middleman/features/smush_pngs.rb | 58 ++++++------ lib/middleman/features/ugly_haml.rb | 13 +-- lib/middleman/renderers.rb | 26 ------ .../renderers/{coffee.rb => coffee_script.rb} | 19 ++-- lib/middleman/renderers/haml.rb | 61 ++++++------ lib/middleman/renderers/sass.rb | 58 ++++++------ lib/middleman/server.rb | 93 +++++++++---------- lib/middleman/template/config.rbt | 12 +-- 33 files changed, 421 insertions(+), 487 deletions(-) delete mode 100644 features/helpers_simple_macros.feature create mode 100644 features/padrino_helpers.feature delete mode 100644 fixtures/test-app/views/image_tag.html.haml delete mode 100644 fixtures/test-app/views/javascript_include_tag.html.haml rename fixtures/test-app/views/{ => layouts}/custom.haml (100%) delete mode 100755 fixtures/test-app/views/link_to.html.haml delete mode 100755 fixtures/test-app/views/maruku.html.maruku create mode 100644 fixtures/test-app/views/padrino_test.html.haml delete mode 100644 fixtures/test-app/views/stylesheet_link_tag.html.haml delete mode 100644 lib/middleman/renderers.rb rename lib/middleman/renderers/{coffee.rb => coffee_script.rb} (63%) diff --git a/Rakefile b/Rakefile index 12c28f2a..26473143 100644 --- a/Rakefile +++ b/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") diff --git a/VERSION b/VERSION index e2d050ff..e6a7aa94 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.99.1.pre +0.99.2.pre diff --git a/features/helpers_simple_macros.feature b/features/helpers_simple_macros.feature deleted file mode 100644 index 2b5b3f39..00000000 --- a/features/helpers_simple_macros.feature +++ /dev/null @@ -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 'No Href' - And I should see 'Has Href' - And I should see 'Has param' - - Scenario: Using the image_tag helper - Given the Server is running - When I go to "/image_tag.html" - Then I should see '' - And I should see '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 '' - Then I should see '' - Then I should see '' - Then I should see '' - - Scenario: Using the stylesheet_link_tag helper - Given the Server is running - When I go to "/stylesheet_link_tag.html" - Then I should see '' - Then I should see '' - Then I should see '' - Then I should see '' \ No newline at end of file diff --git a/features/padrino_helpers.feature b/features/padrino_helpers.feature new file mode 100644 index 00000000..a8dd3021 --- /dev/null +++ b/features/padrino_helpers.feature @@ -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"' \ No newline at end of file diff --git a/features/scss-support.feature b/features/scss-support.feature index d0a2ca59..02af58bc 100644 --- a/features/scss-support.feature +++ b/features/scss-support.feature @@ -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" \ No newline at end of file + Then I should see "html" \ No newline at end of file diff --git a/features/step_definitions/asset_host_steps.rb b/features/step_definitions/asset_host_steps.rb index e84e3eb2..cfe7b9ca 100644 --- a/features/step_definitions/asset_host_steps.rb +++ b/features/step_definitions/asset_host_steps.rb @@ -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 diff --git a/features/step_definitions/middleman_steps.rb b/features/step_definitions/middleman_steps.rb index 3acb905d..0fcc9096 100644 --- a/features/step_definitions/middleman_steps.rb +++ b/features/step_definitions/middleman_steps.rb @@ -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 diff --git a/fixtures/test-app/views/image_tag.html.haml b/fixtures/test-app/views/image_tag.html.haml deleted file mode 100644 index 6c74d1d4..00000000 --- a/fixtures/test-app/views/image_tag.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -= image_tag "test.png" -= image_tag "test2.png", :alt => "alt" \ No newline at end of file diff --git a/fixtures/test-app/views/javascript_include_tag.html.haml b/fixtures/test-app/views/javascript_include_tag.html.haml deleted file mode 100644 index 2be9fff0..00000000 --- a/fixtures/test-app/views/javascript_include_tag.html.haml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/fixtures/test-app/views/custom.haml b/fixtures/test-app/views/layouts/custom.haml similarity index 100% rename from fixtures/test-app/views/custom.haml rename to fixtures/test-app/views/layouts/custom.haml diff --git a/fixtures/test-app/views/link_to.html.haml b/fixtures/test-app/views/link_to.html.haml deleted file mode 100755 index 43393c6b..00000000 --- a/fixtures/test-app/views/link_to.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -= link_to "No Href" -= link_to "Has Href", "test.com" -= link_to "Has param", "test2.com", :class => "test" \ No newline at end of file diff --git a/fixtures/test-app/views/maruku.html.maruku b/fixtures/test-app/views/maruku.html.maruku deleted file mode 100755 index 9eab2091..00000000 --- a/fixtures/test-app/views/maruku.html.maruku +++ /dev/null @@ -1 +0,0 @@ -# Hello Maruku {.header} \ No newline at end of file diff --git a/fixtures/test-app/views/padrino_test.html.haml b/fixtures/test-app/views/padrino_test.html.haml new file mode 100644 index 00000000..8a9751fe --- /dev/null +++ b/fixtures/test-app/views/padrino_test.html.haml @@ -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" \ No newline at end of file diff --git a/fixtures/test-app/views/stylesheet_link_tag.html.haml b/fixtures/test-app/views/stylesheet_link_tag.html.haml deleted file mode 100644 index 9ff4dff0..00000000 --- a/fixtures/test-app/views/stylesheet_link_tag.html.haml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 64c36f06..5d577881 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -4,5 +4,13 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) require 'rubygems' 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 diff --git a/lib/middleman/features.rb b/lib/middleman/features.rb index abbe0215..f80dd229 100644 --- a/lib/middleman/features.rb +++ b/lib/middleman/features.rb @@ -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 - - # 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) +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" + + 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 + + def enable(feature_name) + $stderr.puts "Warning: Feature activation has been renamed from enable to activate" + activate(feature_name) + super(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 - 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 \ No newline at end of file diff --git a/lib/middleman/features/asset_host.rb b/lib/middleman/features/asset_host.rb index fd422b41..564da7a0 100644 --- a/lib/middleman/features/asset_host.rb +++ b/lib/middleman/features/asset_host.rb @@ -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 +end \ No newline at end of file diff --git a/lib/middleman/features/automatic_image_sizes.rb b/lib/middleman/features/automatic_image_sizes.rb index 3bbd9c14..022569c8 100755 --- a/lib/middleman/features/automatic_image_sizes.rb +++ b/lib/middleman/features/automatic_image_sizes.rb @@ -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 + + 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 - rescue + 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 \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index 5e06af1c..c51ed9e5 100755 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/default_helpers.rb b/lib/middleman/features/default_helpers.rb index 845d429e..26ccba0f 100644 --- a/lib/middleman/features/default_helpers.rb +++ b/lib/middleman/features/default_helpers.rb @@ -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{#{title}} - 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 - "" - 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 - "" - 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 - "" - 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 } \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/livereload.rb b/lib/middleman/features/livereload.rb index 872aaa8f..2c5ec88d 100644 --- a/lib/middleman/features/livereload.rb +++ b/lib/middleman/features/livereload.rb @@ -1,4 +1,4 @@ -class Middleman::Features::LiveReload +module Middleman::Features::LiveReload def initialize(app, config) return unless Middleman::Server.environment == :development diff --git a/lib/middleman/features/minify_css.rb b/lib/middleman/features/minify_css.rb index a4f348a2..df82aae0 100644 --- a/lib/middleman/features/minify_css.rb +++ b/lib/middleman/features/minify_css.rb @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/minify_javascript.rb b/lib/middleman/features/minify_javascript.rb index 0d3ba073..07a16056 100755 --- a/lib/middleman/features/minify_javascript.rb +++ b/lib/middleman/features/minify_javascript.rb @@ -1,11 +1,14 @@ -class Middleman::Features::MinifyJavascript - def initialize(app, config) - Haml::Javascript.send :include, ::Haml::Filters::Base - - require "middleman/features/minify_javascript/rack" - app.use Middleman::Rack::MinifyJavascript +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 def render_with_options(text, options) @@ -15,6 +18,4 @@ class Middleman::Features::MinifyJavascript end end end -end - -Middleman::Features.register :minify_javascript, Middleman::Features::MinifyJavascript \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/relative_assets.rb b/lib/middleman/features/relative_assets.rb index 0a1277a7..568a969f 100755 --- a/lib/middleman/features/relative_assets.rb +++ b/lib/middleman/features/relative_assets.rb @@ -1,35 +1,36 @@ -class Middleman::Features::RelativeAssets - def initialize(app, config) - ::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 - - 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('/') +module Middleman::Features::RelativeAssets + class << self + def registered(app) + ::Compass.configuration.relative_assets = true - if parts.length > 1 - arry = [] - (parts.length - 1).times { arry << ".." } - arry << path - File.join(*arry) - else + 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 + path + end end end end + alias :included :registered end -end - -Middleman::Features.register :relative_assets, Middleman::Features::RelativeAssets +end \ No newline at end of file diff --git a/lib/middleman/features/slickmap.rb b/lib/middleman/features/slickmap.rb index e7ccdaa1..72cc5ccd 100755 --- a/lib/middleman/features/slickmap.rb +++ b/lib/middleman/features/slickmap.rb @@ -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 - - 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 - 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 + if Middleman::Server.environment == :build + Middleman::Builder.template :slickmap, @sitemap_url, @sitemap_url + 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 + 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 + 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) @@ -83,6 +86,4 @@ class Middleman::Features::Slickmap entry end -end - -Middleman::Features.register :slickmap, Middleman::Features::Slickmap \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/smush_pngs.rb b/lib/middleman/features/smush_pngs.rb index 8f293382..aceba22b 100644 --- a/lib/middleman/features/smush_pngs.rb +++ b/lib/middleman/features/smush_pngs.rb @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/features/ugly_haml.rb b/lib/middleman/features/ugly_haml.rb index 31805db0..2771b22d 100644 --- a/lib/middleman/features/ugly_haml.rb +++ b/lib/middleman/features/ugly_haml.rb @@ -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 \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/renderers.rb b/lib/middleman/renderers.rb deleted file mode 100644 index e5580e6f..00000000 --- a/lib/middleman/renderers.rb +++ /dev/null @@ -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}" } \ No newline at end of file diff --git a/lib/middleman/renderers/coffee.rb b/lib/middleman/renderers/coffee_script.rb similarity index 63% rename from lib/middleman/renderers/coffee.rb rename to lib/middleman/renderers/coffee_script.rb index d96b31b9..79e90ec5 100755 --- a/lib/middleman/renderers/coffee.rb +++ b/lib/middleman/renderers/coffee_script.rb @@ -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) \ No newline at end of file +end \ No newline at end of file diff --git a/lib/middleman/renderers/haml.rb b/lib/middleman/renderers/haml.rb index 0e528d60..107404d3 100755 --- a/lib/middleman/renderers/haml.rb +++ b/lib/middleman/renderers/haml.rb @@ -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 = '
' - line_num = 0 - text.each_line do |line| - line_num += 1 - next if line.strip.empty? - output << %Q{} + def render(text) + output = '
' + line_num = 0 + text.each_line do |line| + line_num += 1 + next if line.strip.empty? + output << %Q{} - columns = line.split("|").map { |p| p.strip } - columns.each_with_index do |col, i| - output << %Q{} + columns = line.split("|").map { |p| p.strip } + columns.each_with_index do |col, i| + output << %Q{} + end + + output << "" end - - output << "" + output + "
#{col}#{col}
" end - output + "" end end end -end - -class Middleman::Server - helpers Middleman::Haml::Helpers end \ No newline at end of file diff --git a/lib/middleman/renderers/sass.rb b/lib/middleman/renderers/sass.rb index 752dac23..5bb18606 100644 --- a/lib/middleman/renderers/sass.rb +++ b/lib/middleman/renderers/sass.rb @@ -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 - - 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) + 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) + 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 diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index e686a59e..5ef15c53 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -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 + + 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) + + if result content_type mime_type(File.extname(path)), :charset => 'utf-8' - - renderer = Middleman::Renderers.get_method(template_path) - if respond_to? renderer - status 200 - return send(renderer, path.to_sym, { :layout => layout }) - end + 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) } diff --git a/lib/middleman/template/config.rbt b/lib/middleman/template/config.rbt index f1d50a2a..52abfc46 100755 --- a/lib/middleman/template/config.rbt +++ b/lib/middleman/template/config.rbt @@ -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/"