From bfa5332c98bd6945dc87b2cf821d0dca0192ed91 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Wed, 22 Jun 2011 12:13:35 -0700 Subject: [PATCH 1/4] Add `auto_javascript_include_tag` that behaves similarly to `auto_stylesheet_link_tag`. Includes new features/fixtures. --- ...elpers_auto_javascript_include_tag.feature | 27 +++++++++++ .../helpers_auto_stylesheet_link_tag.feature | 18 ++++++-- fixtures/test-app/config.rb | 27 ++++++++--- fixtures/test-app/source/auto-js.html.haml | 1 + .../auto-css.css => javascripts/auto-js.js} | 0 .../auto-js/auto-js.js} | 0 .../source/javascripts/auto-js/index.js | 0 .../source/javascripts/auto-js/sub/auto-js.js | 0 .../source/stylesheets/auto-css/auto-css.css | 0 .../source/stylesheets/auto-css/index.css | 0 .../stylesheets/auto-css/sub/auto-css.css | 0 lib/middleman/features/default_helpers.rb | 46 ++++++++++++------- 12 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 features/helpers_auto_javascript_include_tag.feature create mode 100755 fixtures/test-app/source/auto-js.html.haml rename fixtures/test-app/source/{stylesheets/sub1/auto-css.css => javascripts/auto-js.js} (100%) mode change 100755 => 100644 rename fixtures/test-app/source/{stylesheets/sub1/sub2/auto-css.css => javascripts/auto-js/auto-js.js} (100%) mode change 100755 => 100644 create mode 100644 fixtures/test-app/source/javascripts/auto-js/index.js create mode 100644 fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js create mode 100755 fixtures/test-app/source/stylesheets/auto-css/auto-css.css create mode 100644 fixtures/test-app/source/stylesheets/auto-css/index.css create mode 100755 fixtures/test-app/source/stylesheets/auto-css/sub/auto-css.css diff --git a/features/helpers_auto_javascript_include_tag.feature b/features/helpers_auto_javascript_include_tag.feature new file mode 100644 index 00000000..193f074d --- /dev/null +++ b/features/helpers_auto_javascript_include_tag.feature @@ -0,0 +1,27 @@ +Feature: Built-in auto_javascript_include_tag view helper + In order to simplify including javascript files + + Scenario: Viewing the root path + Given the Server is running + When I go to "/auto-js.html" + Then I should see "javascripts/auto-js.js" + + Scenario: Viewing a tier-1 path + Given the Server is running + When I go to "/auto-js/auto-js.html" + Then I should see "javascripts/auto-js/auto-js.js" + + Scenario: Viewing the index file of a tier-1 path, without filename + Given the Server is running + When I go to "/auto-js" + Then I should see "javascripts/auto-js/index.js" + + Scenario: Viewing the index file of a tier-1 path, without filename, with a trailing slash + Given the Server is running + When I go to "/auto-js/" + Then I should see "javascripts/auto-js/index.js" + + Scenario: Viewing a tier-2 path + Given the Server is running + When I go to "/auto-js/sub/auto-js.html" + Then I should see "javascripts/auto-js/sub/auto-js.js" diff --git a/features/helpers_auto_stylesheet_link_tag.feature b/features/helpers_auto_stylesheet_link_tag.feature index 8139662b..e4ef140b 100644 --- a/features/helpers_auto_stylesheet_link_tag.feature +++ b/features/helpers_auto_stylesheet_link_tag.feature @@ -8,10 +8,20 @@ Feature: Built-in auto_stylesheet_link_tag view helper Scenario: Viewing a tier-1 path Given the Server is running - When I go to "/sub1/auto-css.html" - Then I should see "stylesheets/sub1/auto-css.css" + When I go to "/auto-css/auto-css.html" + Then I should see "stylesheets/auto-css/auto-css.css" + + Scenario: Viewing the index file of a tier-1 path, without filename + Given the Server is running + When I go to "/auto-css" + Then I should see "stylesheets/auto-css/index.css" + + Scenario: Viewing the index file of a tier-1 path, without filename, with a trailing slash + Given the Server is running + When I go to "/auto-css/" + Then I should see "stylesheets/auto-css/index.css" Scenario: Viewing a tier-2 path Given the Server is running - When I go to "/sub1/sub2/auto-css.html" - Then I should see "stylesheets/sub1/sub2/auto-css.css" \ No newline at end of file + When I go to "/auto-css/sub/auto-css.html" + Then I should see "stylesheets/auto-css/sub/auto-css.css" diff --git a/fixtures/test-app/config.rb b/fixtures/test-app/config.rb index b91191ed..0d12818c 100644 --- a/fixtures/test-app/config.rb +++ b/fixtures/test-app/config.rb @@ -17,14 +17,27 @@ get "/sub1/sub2/page-class.html" do haml :"page-classes.html", :layout => false end -get "/auto-css.html" do - haml :"auto-css.html", :layout => false +%w{ + /auto-css.html + /auto-css + /auto-css/ + /auto-css/auto-css.html + /auto-css/sub/auto-css.html +}.each do |path| + get path do + haml :"auto-css.html", :layout => false + end end -get "/sub1/auto-css.html" do - haml :"auto-css.html", :layout => false +%w{ + /auto-js.html + /auto-js + /auto-js/ + /auto-js/auto-js.html + /auto-js/sub/auto-js.html +}.each do |path| + get path do + haml :"auto-js.html", :layout => false + end end -get "/sub1/sub2/auto-css.html" do - haml :"auto-css.html", :layout => false -end diff --git a/fixtures/test-app/source/auto-js.html.haml b/fixtures/test-app/source/auto-js.html.haml new file mode 100755 index 00000000..3bf0eb1e --- /dev/null +++ b/fixtures/test-app/source/auto-js.html.haml @@ -0,0 +1 @@ += auto_javascript_include_tag diff --git a/fixtures/test-app/source/stylesheets/sub1/auto-css.css b/fixtures/test-app/source/javascripts/auto-js.js old mode 100755 new mode 100644 similarity index 100% rename from fixtures/test-app/source/stylesheets/sub1/auto-css.css rename to fixtures/test-app/source/javascripts/auto-js.js diff --git a/fixtures/test-app/source/stylesheets/sub1/sub2/auto-css.css b/fixtures/test-app/source/javascripts/auto-js/auto-js.js old mode 100755 new mode 100644 similarity index 100% rename from fixtures/test-app/source/stylesheets/sub1/sub2/auto-css.css rename to fixtures/test-app/source/javascripts/auto-js/auto-js.js diff --git a/fixtures/test-app/source/javascripts/auto-js/index.js b/fixtures/test-app/source/javascripts/auto-js/index.js new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js b/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/test-app/source/stylesheets/auto-css/auto-css.css b/fixtures/test-app/source/stylesheets/auto-css/auto-css.css new file mode 100755 index 00000000..e69de29b diff --git a/fixtures/test-app/source/stylesheets/auto-css/index.css b/fixtures/test-app/source/stylesheets/auto-css/index.css new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/test-app/source/stylesheets/auto-css/sub/auto-css.css b/fixtures/test-app/source/stylesheets/auto-css/sub/auto-css.css new file mode 100755 index 00000000..e69de29b diff --git a/lib/middleman/features/default_helpers.rb b/lib/middleman/features/default_helpers.rb index e431c8b7..56168461 100644 --- a/lib/middleman/features/default_helpers.rb +++ b/lib/middleman/features/default_helpers.rb @@ -7,23 +7,37 @@ module Middleman::Features::DefaultHelpers end module Helpers - def auto_stylesheet_link_tag(separator="/") - 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("/", separator) - - css_file = File.join(self.class.views, 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) || File.exists?(less_file) - stylesheet_link_tag "#{path}.css" + def auto_stylesheet_link_tag(separator="/") + auto_tag(:css, separator) do |path| + stylesheet_link_tag path + end + end + + def auto_javascript_include_tag(separator="/") + auto_tag(:js, separator) do |path| + javascript_include_tag path end end - + + def auto_tag(asset_ext, separator="/", asset_dir=nil) + if asset_dir.nil? + asset_dir = case asset_ext + when :js then self.class.js_dir + when :css then self.class.css_dir + end + end + path = request.path_info.dup + # If the basename of the request as no extension, assume we are serving a + # directory and join index_file to the path. + path = File.join(path, self.class.index_file) if File.extname(path).empty? + path = path.gsub(%r{^/}, '') + path = path.gsub(File.extname(path), ".#{asset_ext}") + path = path.gsub("/", separator) + + views = Dir[File.join(self.class.views, asset_dir, "#{path}*")] + yield path if views.any? + end + def page_classes path = request.path_info.dup path << settings.index_file if path.match(%r{/$}) @@ -58,4 +72,4 @@ module Middleman::Features::DefaultHelpers "#{result_path}#{timestamp}" end end -end \ No newline at end of file +end From 621825b36c28a3904c48d9db5e923d71a17d7f57 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Thu, 23 Jun 2011 09:32:05 -0700 Subject: [PATCH 2/4] Ignore swapfiles (used by vim) --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 572e021f..5587be6a 100755 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ pkg Gemfile.lock docs .rvmrc +.*.swp From fe9a5ece325c5d39bcd71f3cc5836f5d7b1d0c4f Mon Sep 17 00:00:00 2001 From: David Siegel Date: Thu, 23 Jun 2011 09:32:10 -0700 Subject: [PATCH 3/4] Take a bit more care when checking if a view file exists. --- lib/middleman/features/default_helpers.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/middleman/features/default_helpers.rb b/lib/middleman/features/default_helpers.rb index 56168461..283929c3 100644 --- a/lib/middleman/features/default_helpers.rb +++ b/lib/middleman/features/default_helpers.rb @@ -7,12 +7,12 @@ module Middleman::Features::DefaultHelpers end module Helpers - def auto_stylesheet_link_tag(separator="/") - auto_tag(:css, separator) do |path| + def auto_stylesheet_link_tag(separator="/") + auto_tag(:css, separator) do |path| stylesheet_link_tag path - end + end end - + def auto_javascript_include_tag(separator="/") auto_tag(:js, separator) do |path| javascript_include_tag path @@ -27,15 +27,15 @@ module Middleman::Features::DefaultHelpers end end path = request.path_info.dup - # If the basename of the request as no extension, assume we are serving a - # directory and join index_file to the path. - path = File.join(path, self.class.index_file) if File.extname(path).empty? + # If the basename of the request as no extension, assume we are serving a + # directory and join index_file to the path. + path = File.join(path, self.class.index_file) if File.extname(path).empty? path = path.gsub(%r{^/}, '') path = path.gsub(File.extname(path), ".#{asset_ext}") path = path.gsub("/", separator) - views = Dir[File.join(self.class.views, asset_dir, "#{path}*")] - yield path if views.any? + view = File.join(self.class.views, asset_dir, path) + yield path if File.exists?(view) or Dir["#{view}.*"].any? end def page_classes From 3836b1d90ca99dacd2ea31d147298ea312def456 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Thu, 23 Jun 2011 20:46:21 -0700 Subject: [PATCH 4/4] Add empty files! --- fixtures/test-app/source/javascripts/auto-js.js | 1 + fixtures/test-app/source/javascripts/auto-js/auto-js.js | 1 + fixtures/test-app/source/javascripts/auto-js/index.js | 1 + fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js | 1 + 4 files changed, 4 insertions(+) diff --git a/fixtures/test-app/source/javascripts/auto-js.js b/fixtures/test-app/source/javascripts/auto-js.js index e69de29b..8b137891 100644 --- a/fixtures/test-app/source/javascripts/auto-js.js +++ b/fixtures/test-app/source/javascripts/auto-js.js @@ -0,0 +1 @@ + diff --git a/fixtures/test-app/source/javascripts/auto-js/auto-js.js b/fixtures/test-app/source/javascripts/auto-js/auto-js.js index e69de29b..8b137891 100644 --- a/fixtures/test-app/source/javascripts/auto-js/auto-js.js +++ b/fixtures/test-app/source/javascripts/auto-js/auto-js.js @@ -0,0 +1 @@ + diff --git a/fixtures/test-app/source/javascripts/auto-js/index.js b/fixtures/test-app/source/javascripts/auto-js/index.js index e69de29b..8b137891 100644 --- a/fixtures/test-app/source/javascripts/auto-js/index.js +++ b/fixtures/test-app/source/javascripts/auto-js/index.js @@ -0,0 +1 @@ + diff --git a/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js b/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js index e69de29b..8b137891 100644 --- a/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js +++ b/fixtures/test-app/source/javascripts/auto-js/sub/auto-js.js @@ -0,0 +1 @@ +