From b35f026aba421f11994f091abcf92ef058f31ca1 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Thu, 1 Oct 2009 11:26:46 -0700 Subject: [PATCH] a little testing --- lib/middleman/base.rb | 6 ++--- lib/middleman/features/cache_buster.rb | 27 ++++++++++++++-------- lib/middleman/features/compass.rb | 6 ++--- lib/middleman/features/haml.rb | 2 ++ lib/middleman/features/relative_assets.rb | 6 +++-- lib/middleman/features/slickmap.rb | 25 +++++++++++++++++--- lib/middleman/helpers.rb | 13 ++++++----- spec/cache_buster_spec.rb | 28 +++++++++++++++++++++++ spec/relative_assets_spec.rb | 28 +++++++++++++++++++++++ 9 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 spec/cache_buster_spec.rb create mode 100644 spec/relative_assets_spec.rb diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 72bbcab6..3414440d 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -1,5 +1,4 @@ require 'rubygems' unless ENV['NO_RUBYGEMS'] -require 'haml' require 'sinatra/base' require 'middleman/helpers' @@ -15,14 +14,16 @@ module Middleman set :environment, ENV['MM_ENV'] || :development set :supported_formats, [] set :index_file, 'index.html' + set :js_dir, "javascripts" set :css_dir, "stylesheets" set :images_dir, "images" set :build_dir, "build" + set :http_prefix, "/" enable :compass enable :content_for enable :sprockets - #enable :slickmap + disable :slickmap disable :cache_buster disable :minify_css disable :minify_javascript @@ -38,7 +39,6 @@ module Middleman enable :minify_css enable :minify_javascript enable :cache_buster - # disable :slickmap end def template_exists?(path, renderer=nil) diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index 22314350..a8433559 100644 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -1,9 +1,18 @@ - - -# def cache_buster -# 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 \ No newline at end of file +class Middleman::Base + helpers do + alias_method :pre_cache_buster_asset_url, :asset_url + def asset_url(path, prefix="") + path = pre_cache_buster_asset_url(path, prefix) + if path.include?("://") + path + else + real_path = File.join(options.public, path) + if File.readable?(real_path) + 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 \ No newline at end of file diff --git a/lib/middleman/features/compass.rb b/lib/middleman/features/compass.rb index ea323296..956e9bb7 100644 --- a/lib/middleman/features/compass.rb +++ b/lib/middleman/features/compass.rb @@ -17,9 +17,9 @@ class Middleman::Base false end end - - config.http_images_path = "/#{self.images_dir}" - config.http_stylesheets_path = "/#{self.css_dir}" + + config.http_images_path = File.join(self.http_prefix, self.images_dir) + config.http_stylesheets_path = File.join(self.http_prefix, self.css_dir) config.add_import_path(config.sass_dir) end diff --git a/lib/middleman/features/haml.rb b/lib/middleman/features/haml.rb index 0c2018e7..1d831f91 100644 --- a/lib/middleman/features/haml.rb +++ b/lib/middleman/features/haml.rb @@ -1,3 +1,5 @@ +require 'haml' + module Middleman module Haml def self.included(base) diff --git a/lib/middleman/features/relative_assets.rb b/lib/middleman/features/relative_assets.rb index 63320fe6..1039513f 100644 --- a/lib/middleman/features/relative_assets.rb +++ b/lib/middleman/features/relative_assets.rb @@ -8,12 +8,14 @@ class Middleman::Base end helpers do - def asset_url(path) + alias_method :pre_relative_asset_url, :asset_url + def asset_url(path, prefix="") + path = pre_relative_asset_url(path, prefix) if path.include?("://") path else request_path = request.path_info.dup - request_path << "index.html" if path.match(%r{/$}) + request_path << self.index_file if path.match(%r{/$}) request_path.gsub!(%r{^/}, '') parts = request_path.split('/') diff --git a/lib/middleman/features/slickmap.rb b/lib/middleman/features/slickmap.rb index 917c2746..cf84338b 100644 --- a/lib/middleman/features/slickmap.rb +++ b/lib/middleman/features/slickmap.rb @@ -1,5 +1,24 @@ require 'slickmap' -get '/sitemap.html' do - haml :sitemap, :layout => false -end \ No newline at end of file +class Middleman::Base + def build_sitemap(&block) + # views - stylesheets + # public + # .select + # block.call(this) + end + + get '/sitemap.html' do + @tree = build_sitemap do |file_name| + true + end + haml :sitemap, :layout => false + end + + use_in_file_templates! +end + +__END__ + +@@ sitemap +%div.title Hello world!!!!! \ No newline at end of file diff --git a/lib/middleman/helpers.rb b/lib/middleman/helpers.rb index 5bc07cbf..3fce5628 100644 --- a/lib/middleman/helpers.rb +++ b/lib/middleman/helpers.rb @@ -2,7 +2,7 @@ module Middleman module Helpers def page_classes(*additional) path = request.path_info - path << "index.html" if path.match(%r{/$}) + path << options.index_file if path.match(%r{/$}) path.gsub!(%r{^/}, '') classes = [] @@ -20,26 +20,27 @@ module Middleman %Q{#{title}} end - def asset_url(path) - path.include?("://") ? path : "/#{path}" + def asset_url(path, prefix="") + base_url = File.join(options.http_prefix, prefix) + path.include?("://") ? path : File.join(base_url, path) end def image_tag(path, options={}) options[:alt] ||= "" - params = options.merge(:src => asset_url(path)) + params = options.merge(:src => asset_url(path, self.images_dir)) params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') "" end def javascript_include_tag(path, options={}) - params = options.merge(:src => asset_url(path), :type => "text/javascript") + params = options.merge(:src => asset_url(path, self.js_dir), :type => "text/javascript") params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') "" end def stylesheet_link_tag(path, options={}) options[:rel] ||= "stylesheet" - params = options.merge(:href => asset_url(path), :type => "text/css") + params = options.merge(:href => asset_url(path, self.css_dir), :type => "text/css") params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ') "" end diff --git a/spec/cache_buster_spec.rb b/spec/cache_buster_spec.rb new file mode 100644 index 00000000..40fae46c --- /dev/null +++ b/spec/cache_buster_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), "spec_helper") + +base = ::Middleman::Base +base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") + +describe "Cache Buster Feature" do + before do + base.disable :cache_buster + base.init! + @app = base.new + end + + it "should not append query string if off" do + @app.asset_url("stylesheets/static.css").should_not include("?") + end +end + +describe "Cache Buster Feature" do + before do + base.enable :cache_buster + base.init! + @app = base.new + end + + it "should append query string if on" do + @app.asset_url("stylesheets/static.css").should include("?") + end +end \ No newline at end of file diff --git a/spec/relative_assets_spec.rb b/spec/relative_assets_spec.rb new file mode 100644 index 00000000..622dabf1 --- /dev/null +++ b/spec/relative_assets_spec.rb @@ -0,0 +1,28 @@ +# require File.join(File.dirname(__FILE__), "spec_helper") +# +# base = ::Middleman::Base +# base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") +# +# describe "Relative Assets Feature" do +# before do +# base.disable :relative_assets +# base.init! +# @app = base.new +# end +# +# it "should not contain ../ if off" do +# @app.asset_url("stylesheets/static.css").should_not include("?") +# end +# end +# +# describe "Relative Assets Feature" do +# before do +# base.enable :relative_assets +# base.init! +# @app = base.new +# end +# +# it "should contain ../ if on" do +# @app.asset_url("stylesheets/static.css").should include("?") +# end +# end \ No newline at end of file