From 0e5ce50787642c3ca98926927cf86f4bd08a577c Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 18 Nov 2011 13:38:18 -0800 Subject: [PATCH] rename old padrino tests, remove sinatra tests, use metadata hooks for frontmatter and liquid --- .gitignore | 3 +- ...feature => former_padrino_helpers.feature} | 2 +- features/sinatra.feature | 6 -- features/step_definitions/asset_host_steps.rb | 13 ++-- features/step_definitions/middleman_steps.rb | 40 ++++++----- .../step_definitions/page_layout_steps.rb | 18 ++--- fixtures/glob-app/build/stylesheets/site.css | 46 ------------- ...tml.haml => former_padrino_test.html.haml} | 0 lib/middleman/base.rb | 61 +++++++++-------- lib/middleman/builder.rb | 7 +- lib/middleman/core_extensions/front_matter.rb | 2 +- lib/middleman/features/directory_indexes.rb | 66 ++++++++++--------- lib/middleman/renderers/liquid.rb | 9 +-- 13 files changed, 117 insertions(+), 156 deletions(-) rename features/{padrino_helpers.feature => former_padrino_helpers.feature} (88%) delete mode 100644 features/sinatra.feature delete mode 100644 fixtures/glob-app/build/stylesheets/site.css rename fixtures/test-app/source/{padrino_test.html.haml => former_padrino_test.html.haml} (100%) diff --git a/.gitignore b/.gitignore index e017d939..ae325653 100755 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ docs .rvmrc .rbenv-version fixtures/test-app/build -.*.swp \ No newline at end of file +.*.swp +build diff --git a/features/padrino_helpers.feature b/features/former_padrino_helpers.feature similarity index 88% rename from features/padrino_helpers.feature rename to features/former_padrino_helpers.feature index c354f2b1..79489d2b 100644 --- a/features/padrino_helpers.feature +++ b/features/former_padrino_helpers.feature @@ -3,7 +3,7 @@ Feature: Built-in macro view helpers Scenario: Using the link_to helper Given the Server is running at "test-app" - When I go to "/padrino_test.html" + When I go to "/former_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"' diff --git a/features/sinatra.feature b/features/sinatra.feature deleted file mode 100644 index 651a7fcd..00000000 --- a/features/sinatra.feature +++ /dev/null @@ -1,6 +0,0 @@ -Feature: Sinatra Routes - - Scenario: Rendering html - Given the Server is running at "test-app" - When I go to "/sinatra_test" - Then I should see "Ratpack" \ 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 30423f9c..4bc752ae 100644 --- a/features/step_definitions/asset_host_steps.rb +++ b/features/step_definitions/asset_host_steps.rb @@ -1,8 +1,9 @@ Given /^I am using an asset host$/ do - @server ||= Middleman.server - @server.set :show_exceptions, false - @server.activate :asset_host - @server.set :asset_host do |asset| - "http://assets%d.example.com" % (asset.hash % 4) - end + @initialize_commands ||= [] + @initialize_commands << lambda { + activate :asset_host + set :asset_host do |asset| + "http://assets%d.example.com" % (asset.hash % 4) + end + } end \ No newline at end of file diff --git a/features/step_definitions/middleman_steps.rb b/features/step_definitions/middleman_steps.rb index 18855c78..933a06ad 100644 --- a/features/step_definitions/middleman_steps.rb +++ b/features/step_definitions/middleman_steps.rb @@ -1,23 +1,18 @@ Given /^a clean server$/ do - @server = Middleman.server - @server.set :show_exceptions, false + @initialize_commands = [] end Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state| - @server = Middleman.server - @server.set :show_exceptions, false + @initialize_commands ||= [] if state == "enabled" - @server.activate(feature.to_sym) - end - - @server.set :environment, @current_env || :development + @initialize_commands << lambda { activate(feature.to_sym) } + end end Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value| - @server ||= Middleman.server - @server.set :show_exceptions, false - @server.set variable.to_sym, value + @initialize_commands ||= [] + @initialize_commands << lambda { set(variable.to_sym, value) } end Given /^current environment is "([^\"]*)"$/ do |env| @@ -25,13 +20,22 @@ Given /^current environment is "([^\"]*)"$/ do |env| end Given /^the Server is running at "([^\"]*)"$/ do |app_path| - @server ||= Middleman.server - @server.set :show_exceptions, false - root = File.dirname(File.dirname(File.dirname(__FILE__))) - @server.set :root, File.join(root, "fixtures", app_path) - @app = @server.new! - app_rack = @server.build_new(@app) - @browser = Rack::Test::Session.new(Rack::MockSession.new(app_rack)) + root_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + + initialize_commands = @initialize_commands || [] + initialize_commands.unshift lambda { + set :root, File.join(root_dir, "fixtures", app_path) + set :environment, @current_env || :development + } + + @server_inst = Middleman.server.inst do + initialize_commands.each do |p| + instance_exec(&p) + end + end + + app_rack = @server_inst.class.to_rack_app + @browser = ::Rack::Test::Session.new(::Rack::MockSession.new(app_rack)) end When /^I go to "([^\"]*)"$/ do |url| diff --git a/features/step_definitions/page_layout_steps.rb b/features/step_definitions/page_layout_steps.rb index aed2c625..b7eb7b17 100644 --- a/features/step_definitions/page_layout_steps.rb +++ b/features/step_definitions/page_layout_steps.rb @@ -1,13 +1,15 @@ Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout| - @server ||= Middleman.server - @server.set :show_exceptions, false - @server.page(url, :layout => layout.to_sym) + @initialize_commands ||= [] + @initialize_commands << lambda { + page(url, :layout => layout.to_sym) + } end Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout| - @server ||= Middleman.server - @server.set :show_exceptions, false - @server.with_layout(layout.to_sym) do - page(url) - end + @initialize_commands ||= [] + @initialize_commands << lambda { + with_layout(layout.to_sym) do + page(url) + end + } end \ No newline at end of file diff --git a/fixtures/glob-app/build/stylesheets/site.css b/fixtures/glob-app/build/stylesheets/site.css deleted file mode 100644 index 1efc3753..00000000 --- a/fixtures/glob-app/build/stylesheets/site.css +++ /dev/null @@ -1,46 +0,0 @@ -html, body, div, span, applet, object, iframe, -h1, h2, h3, h4, h5, h6, p, blockquote, pre, -a, abbr, acronym, address, big, cite, code, -del, dfn, em, img, ins, kbd, q, s, samp, -small, strike, strong, sub, sup, tt, var, -b, u, i, center, -dl, dt, dd, ol, ul, li, -fieldset, form, label, legend, -table, caption, tbody, tfoot, thead, tr, th, td, -article, aside, canvas, details, embed, -figure, figcaption, footer, header, hgroup, -menu, nav, output, ruby, section, summary, -time, mark, audio, video { - margin: 0; - padding: 0; - border: 0; - font-size: 100%; - font: inherit; - vertical-align: baseline; } - -body { - line-height: 1; } - -ol, ul { - list-style: none; } - -table { - border-collapse: collapse; - border-spacing: 0; } - -caption, th, td { - text-align: left; - font-weight: normal; - vertical-align: middle; } - -q, blockquote { - quotes: none; } - q:before, q:after, blockquote:before, blockquote:after { - content: ""; - content: none; } - -a img { - border: none; } - -article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary { - display: block; } diff --git a/fixtures/test-app/source/padrino_test.html.haml b/fixtures/test-app/source/former_padrino_test.html.haml similarity index 100% rename from fixtures/test-app/source/padrino_test.html.haml rename to fixtures/test-app/source/former_padrino_test.html.haml diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 2dac8af1..01d85fe8 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -9,6 +9,7 @@ require "active_support/core_ext/string/inflections" class Middleman::Base include Hooks + define_hook :before define_hook :build_config define_hook :development_config @@ -44,7 +45,7 @@ class Middleman::Base end def map(map, &block) - app.map(map, &block) + # app.map(map, &block) end def helpers(*extensions, &block) @@ -79,9 +80,10 @@ class Middleman::Base end end - def set(key, value) + def set(key, value=nil, &block) setter = "#{key}=".to_sym self.class.send(:attr_accessor, key) if !respond_to?(setter) + value = block if block_given? send(setter, value) end @@ -159,28 +161,37 @@ class Middleman::Base set(k, v) end + instance_exec(&block) if block_given? + set :source_dir, File.join(root, source) super - instance_exec(&block) if block_given? - run_hook :initialized end + attr :env + attr :req + attr :res + attr :options + attr :locals + def call(env) @env = env @req = Rack::Request.new(env) @res = Rack::Response.new - process_request - end + catch(:halt) do + process_request - # Custom 404 handler (to be styled) - # app.error Sinatra::NotFound do - # content_type 'text/html' - # "

File Not Found

#{request.path_info}

" - # end + res.status = 404 + res.finish + end + end + + def halt(response) + throw :halt, response + end # Convenience methods to check if we're in a mode def development?; environment == :development; end @@ -188,14 +199,15 @@ class Middleman::Base # Internal method to look for templates and evaluate them if found def process_request + run_hook :before + # Normalize the path and add index if we're looking at a directory - original_path = @env["PATH_INFO"].dup - request_path = full_path(@env["PATH_INFO"].gsub("%20", " ")) + original_path = env["PATH_INFO"].dup + request_path = full_path(env["PATH_INFO"].gsub("%20", " ")) return not_found if sitemap.ignored_path?(request_path) if sitemap.path_is_proxy?(request_path) - # request["is_proxy"] = true request_path = "/" + sitemap.path_target(request_path) end @@ -212,12 +224,12 @@ class Middleman::Base context = sitemap.get_context(original_path) || {} - options = context.has_key?(:options) ? context[:options] : {} - # options.merge!(request['custom_options'] || {}) + @options = context.has_key?(:options) ? context[:options] : {} + @locals = context.has_key?(:locals) ? context[:locals] : {} provides_metadata.each do |callback, matcher| next if !matcher.nil? && !path.match(matcher) - options.merge!(instance_exec(path, &callback) || {}) + instance_exec(path, &callback) end local_layout = if options.has_key?(:layout) @@ -231,12 +243,9 @@ class Middleman::Base if context.has_key?(:block) && context[:block] instance_eval(&context[:block]) end - - # locals = request['custom_locals'] || {} - locals = {} # content_type mime_type(File.extname(request_path)) - @res.status = 200 + res.status = 200 output = if local_layout layout_engine = if options.has_key?(:layout_engine) @@ -256,8 +265,8 @@ class Middleman::Base render(path, locals) end - @res.write output - @res.finish + res.write output + halt res.finish end public @@ -318,7 +327,6 @@ public # raw_templates_cache[key] # end -protected def full_path(path) parts = path ? path.split('/') : [] if parts.last.nil? || parts.last.split('.').length == 1 @@ -329,14 +337,13 @@ protected def not_found @res.status == 404 - @res.write "

File Not Found

#{@env["PATH_INFO"]}

" + @res.write "

File Not Found

#{env["PATH_INFO"]}

" @res.finish end def resolve_template(request_path, options={}) request_path = request_path.to_s @_resolved_templates ||= {} - if !@_resolved_templates.has_key?(request_path) relative_path = request_path.sub(%r{^/}, "") on_disk_path = File.expand_path(relative_path, source_dir) @@ -384,7 +391,7 @@ protected file = ::Rack::File.new nil file.path = path - file.serving(@env) + file.serving(env) end def render(path, locals = {}, options = {}, &block) diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 29047688..2f64d40f 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -17,7 +17,7 @@ module Middleman request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "") - # begin + begin # destination, request_path = SHARED_SERVER.reroute_builder(destination, request_path) request_path.gsub!(/\s/, "%20") @@ -26,8 +26,9 @@ module Middleman create_file destination, nil, config do response.body end if response.status == 200 - # rescue - # end + rescue + say_status :error, destination, :red + end end end diff --git a/lib/middleman/core_extensions/front_matter.rb b/lib/middleman/core_extensions/front_matter.rb index 02eb9a1a..184214d6 100644 --- a/lib/middleman/core_extensions/front_matter.rb +++ b/lib/middleman/core_extensions/front_matter.rb @@ -37,7 +37,7 @@ module Middleman::CoreExtensions::FrontMatter end end - data + @options.merge!(data) end super diff --git a/lib/middleman/features/directory_indexes.rb b/lib/middleman/features/directory_indexes.rb index 553fb411..abcececf 100644 --- a/lib/middleman/features/directory_indexes.rb +++ b/lib/middleman/features/directory_indexes.rb @@ -1,53 +1,55 @@ module Middleman::Features::DirectoryIndexes class << self def registered(app) - app.set :ignored_directory_indexes, [] - app.extend ClassMethods - - app.build_reroute do |destination, request_path| - index_ext = File.extname(app.settings.index_file) - new_index_path = "/#{app.settings.index_file}" - - indexed_path = request_path.gsub(/\/$/, "") + index_ext - - if app.settings.ignored_directory_indexes.include?(request_path) - false - elsif request_path =~ /#{new_index_path}$/ - false - else - [ - destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), - request_path - ] - end - end - + app.send :include, InstanceMethods app.before do - indexed_path = request.path_info.gsub(/\/$/, "") + "/" + app.settings.index_file - indexed_exists = resolve_template(indexed_path, :raise_exceptions => false) - - extensioned_path = request.path_info.gsub(/\/$/, "") + File.extname(app.settings.index_file) - is_ingored = settings.ignored_directory_indexes.include?(extensioned_path) + indexed_path = env["PATH_INFO"].sub(/\/$/, "") + "/" + self.index_file + indexed_exists = resolve_template(indexed_path) + + extensioned_path = env["PATH_INFO"].sub(/\/$/, "") + File.extname(self.index_file) + is_ignored = self.ignored_directory_indexes.include?(extensioned_path) - if !indexed_exists && !is_ingored - parts = request.path_info.split("/") - last_part = parts.last + if !indexed_exists && !is_ignored + parts = env["PATH_INFO"].split("/") + last_part = parts.last last_part_ext = File.extname(last_part) if last_part_ext.blank? # This is a folder, redirect to index - request.path_info = extensioned_path + env["PATH_INFO"] = extensioned_path end end end + + # app.build_reroute do |destination, request_path| + # index_ext = File.extname(app.settings.index_file) + # new_index_path = "/#{app.settings.index_file}" + # + # indexed_path = request_path.gsub(/\/$/, "") + index_ext + # + # if app.settings.ignored_directory_indexes.include?(request_path) + # false + # elsif request_path =~ /#{new_index_path}$/ + # false + # else + # [ + # destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), + # request_path + # ] + # end + # end end alias :included :registered end - module ClassMethods + module InstanceMethods + def ignored_directory_indexes + @_ignored_directory_indexes ||= [] + end + def page(url, options={}, &block) if options.has_key?(:directory_index) && !options["directory_index"] - settings.ignored_directory_indexes << url + ignored_directory_indexes << url else super end diff --git a/lib/middleman/renderers/liquid.rb b/lib/middleman/renderers/liquid.rb index 5148e187..cfdfff0b 100644 --- a/lib/middleman/renderers/liquid.rb +++ b/lib/middleman/renderers/liquid.rb @@ -9,13 +9,8 @@ module Middleman::Renderers::Liquid app.after_configuration do Liquid::Template.file_system = Liquid::LocalFileSystem.new(self.source_dir) - app.before_processing(:liquid) do |result| - if result && result[1] == :liquid - request['custom_locals'] ||= {} - request['custom_locals'][:data] = data.to_h - end - - true + provides_metadata %r{\.liquid$} do |path| + @locals.merge!(:data => data.to_h) end end rescue LoadError