Add auto_javascript_include_tag that behaves similarly to auto_stylesheet_link_tag.

Includes new features/fixtures.
This commit is contained in:
David Siegel 2011-06-22 12:13:35 -07:00
parent 38ed58070d
commit bfa5332c98
12 changed files with 92 additions and 27 deletions

View file

@ -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"

View file

@ -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"
When I go to "/auto-css/sub/auto-css.html"
Then I should see "stylesheets/auto-css/sub/auto-css.css"

View file

@ -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

View file

@ -0,0 +1 @@
= auto_javascript_include_tag

View file

@ -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
end