From ecce56d6ae48655f7488f58b38be61a0000fa23d Mon Sep 17 00:00:00 2001 From: tdreyno Date: Tue, 3 Nov 2009 12:34:57 -0800 Subject: [PATCH] new helpers and some specs --- lib/middleman/helpers.rb | 28 +++++++++++++++++++----- lib/middleman/template/views/layout.haml | 2 +- spec/builder_spec.rb | 4 ---- spec/fixtures/sample/init.rb | 5 ++++- spec/minify_javascript_spec.rb | 21 ++++++++++++++++++ spec/relative_assets_spec.rb | 4 +--- 6 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 spec/minify_javascript_spec.rb diff --git a/lib/middleman/helpers.rb b/lib/middleman/helpers.rb index 9071ce2c..2af4b9e1 100644 --- a/lib/middleman/helpers.rb +++ b/lib/middleman/helpers.rb @@ -7,17 +7,33 @@ module Middleman end module Helpers - def page_classes(*additional) - path = request.path_info + def haml_partial(name, options = {}) + haml name.to_sym, options.merge(:layout => false) + end + + def auto_stylesheet_link_tag + path = request.path_info.dup + path << self.class.index_file if path.match(%r{/$}) + path = path.gsub(%r{^/}, '') + path = path.gsub(File.extname(path), '') + path = path.gsub('/', '-') + + css_file = File.join(File.basename(self.class.public), self.class.css_dir, "#{path}.css") + sass_file = File.join(File.basename(self.class.views), self.class.css_dir, "#{path}.css.sass") + if File.exists?(css_file) || File.exists?(sass_file) + stylesheet_link_tag "#{path}.css" + end + end + + def page_classes + path = request.path_info.dup path << options.index_file if path.match(%r{/$}) - path.gsub!(%r{^/}, '') + path = path.gsub(%r{^/}, '') classes = [] parts = path.split('.')[0].split('/') parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') } - - classes << "index" if classes.empty? - classes += additional unless additional.empty? + classes.join(' ') end diff --git a/lib/middleman/template/views/layout.haml b/lib/middleman/template/views/layout.haml index e3e66aca..465eea4f 100644 --- a/lib/middleman/template/views/layout.haml +++ b/lib/middleman/template/views/layout.haml @@ -4,6 +4,6 @@ = stylesheet_link_tag "site.css" = yield_content :head - %body + %body{ :class => page_classes } #frame = yield \ No newline at end of file diff --git a/spec/builder_spec.rb b/spec/builder_spec.rb index fa5dc715..00239eb6 100644 --- a/spec/builder_spec.rb +++ b/spec/builder_spec.rb @@ -49,10 +49,6 @@ describe "Builder" do it "should not build partial files" do File.exists?("#{@root_dir}/build/_partial.html").should be_false end - - it "should minify inline javascript" do - File.readlines("#{@root_dir}/build/inline-js.html").length.should == 9 - end it "should combine javascript" do File.read("#{@root_dir}/build/javascripts/empty-with-include.js").should include("combo") diff --git a/spec/fixtures/sample/init.rb b/spec/fixtures/sample/init.rb index 81e19eb0..8eba2f2e 100644 --- a/spec/fixtures/sample/init.rb +++ b/spec/fixtures/sample/init.rb @@ -1 +1,4 @@ -# enable :maruku \ No newline at end of file +# enable :maruku +get "/inline-js.html" do + haml :"inline-js.html", :layout => false +end \ No newline at end of file diff --git a/spec/minify_javascript_spec.rb b/spec/minify_javascript_spec.rb new file mode 100644 index 00000000..a1a06a91 --- /dev/null +++ b/spec/minify_javascript_spec.rb @@ -0,0 +1,21 @@ +require 'rack/test' +require File.join(File.dirname(__FILE__), "spec_helper") + +base = ::Middleman::Base +base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") + +describe "Minify Javascript Feature" do + it "should not minify inline js if off" do + base.disable :minify_javascript + browser = Rack::Test::Session.new(Rack::MockSession.new(base.new)) + browser.get("/inline-js.html") + browser.last_response.body.chomp.split($/).length.should == 10 + end + + it "should minify inline js if on" do + base.enable :minify_javascript + browser = Rack::Test::Session.new(Rack::MockSession.new(base.new)) + browser.get("/inline-js.html") + browser.last_response.body.chomp.split($/).length.should == 1 + end +end \ No newline at end of file diff --git a/spec/relative_assets_spec.rb b/spec/relative_assets_spec.rb index b1754072..5940a170 100644 --- a/spec/relative_assets_spec.rb +++ b/spec/relative_assets_spec.rb @@ -11,9 +11,7 @@ describe "Relative Assets Feature" do browser.get("/stylesheets/relative_assets.css") browser.last_response.body.should_not include("../") end -end - -describe "Relative Assets Feature" do + it "should contain ../ if on" do base.enable :relative_assets browser = Rack::Test::Session.new(Rack::MockSession.new(base.new))