diff --git a/Rakefile b/Rakefile index 8fc57344..e93e1c2a 100644 --- a/Rakefile +++ b/Rakefile @@ -15,8 +15,8 @@ begin gem.add_dependency("templater") gem.add_dependency("sprockets") gem.add_dependency("sinatra") - gem.add_dependency("foca-sinatra-content-for") - gem.add_dependency("brynary-rack-test") + gem.add_dependency("sinatra-content-for") + gem.add_dependency("rack-test") gem.add_dependency("haml", ">=2.1.0") gem.add_dependency("chriseppstein-compass") end diff --git a/bin/mm-build b/bin/mm-build index 676cfd93..37d7978c 100755 --- a/bin/mm-build +++ b/bin/mm-build @@ -1,11 +1,15 @@ #!/usr/bin/env ruby require 'templater' -require 'rack-test' - + +ENV['MM_ENV'] = "build" + # Require app +require File.join(File.dirname(__FILE__), "..", "lib", "middleman") require 'middleman/builder' +Middleman::Base.init! + module Generators extend Templater::Manifold desc "Build a staticmatic site" @@ -13,56 +17,35 @@ module Generators class Builder < Templater::Generator # Define source and desintation def self.source_root; Dir.pwd; end - def destination_root; File.join(Dir.pwd, 'build'); end + def destination_root; File.join(Dir.pwd, Middleman::Base.build_dir); end # Override template to ask middleman for the correct extension to output def self.template(name, *args, &block) - return if args.first.include?('layout') + return if args[0].include?('layout') + args.first.split('/').each do |part| return if part[0,1] == '_' end if (args[0] === args[1]) - file_name_parts = File.basename(args.first).split('.') - - if file_name_parts.length > 2 - # static ext embedded in filename - newext = "" - else - # use defaults - newext = case file_name_parts.last - when 'haml', 'erb', 'mab', 'maruku' - '.html' - when 'sass' - '.css' - end - end - - args[1] = args[0].gsub(".#{file_name_parts.last}", newext).gsub('views/', '') + args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "") + .gsub("#{File.basename(Middleman::Base.public)}/", "") + args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2 end super(name, *args, &block) end def self.file(name, *args, &block) - args[1] = args[0].gsub('views/', '') if (args[0] === args[1]) + if (args[0] === args[1]) + args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "") + .gsub("#{File.basename(Middleman::Base.public)}/", "") + end super(name, *args, &block) end - - public_files_glob = File.join(source_root, "public", '**/*') - Dir[public_files_glob].each do |action| - next if File.directory?(action) - action = action.sub("#{source_root}/", '') - template_sym = action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym - - if File.extname(action) == '.js' && !action.include?('min') - template(template_sym, action, action.gsub('public/', '')) - else - file(template_sym, action, action.gsub('public/', '')) - end - end - - glob! "views", (Middleman.supported_formats << "sass") + + glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats + glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats end add :build, Builder @@ -71,7 +54,7 @@ end # Monkey-patch to use a dynamic renderer class Templater::Actions::Template def render - ::Middleman::Builder.render(source, destination) + ::Middleman::Builder.render_file(source, destination) end end diff --git a/bin/mm-server b/bin/mm-server index 3d8237a9..77c128a6 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -6,4 +6,4 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman') # Start Middleman Middleman::Base.set({ :server => %w[thin webrick], :root => Dir.pwd }) -Middleman::Base.init! \ No newline at end of file +Middleman::Base.run! \ No newline at end of file diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index d7e91cba..1f839086 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -7,10 +7,12 @@ module Middleman class Base < Sinatra::Base set :app_file, __FILE__ set :root, Dir.pwd - set :environment, :development + set :environment, ENV['MM_ENV'] || :development + set :supported_formats, [] set :index_file, 'index.html' set :css_dir, "stylesheets" set :images_dir, "images" + set :build_dir, "build" enable :compass enable :content_for @@ -71,6 +73,7 @@ module Middleman end def self.run!(options={}, &block) + init! set options handler = detect_rack_handler handler_name = handler.name.gsub(/.*::/, '') @@ -111,8 +114,6 @@ module Middleman require "middleman/features/#{feature_name}" end end - - run! end end end \ No newline at end of file diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 5136bc5a..52d61d93 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -1,12 +1,10 @@ -require 'middleman' +require 'rack/test' module Middleman class Builder - def self.render_file(source, destination) - # Middleman.set :environment, :build - - request_path = destination.gsub(File.join(Dir.pwd, 'build'), "") - browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman)) + def self.render_file(source, destination) + request_path = destination.gsub(File.join(Dir.pwd, Middleman::Base.build_dir), "") + browser = Rack::Test::Session.new(Rack::MockSession.new(Middleman::Base)) browser.get(request_path) browser.last_response.body end diff --git a/lib/middleman/features/haml.rb b/lib/middleman/features/haml.rb index d4238414..f5b303aa 100644 --- a/lib/middleman/features/haml.rb +++ b/lib/middleman/features/haml.rb @@ -1,5 +1,9 @@ module Middleman module Haml + def self.included(base) + base.supported_formats << "haml" + end + def render_path(path) if template_exists?(path, :haml) result = nil @@ -54,52 +58,55 @@ module Middleman # end # end -module Sass - def render_path(path) - path = path.dup.gsub('.css', '') - if template_exists?(path, :sass) - begin - static_version = options.public + request.path_info - send_file(static_version) if File.exists? static_version - - location_of_sass_file = defined?(MIDDLEMAN_BUILDER) ? "build" : "views" - css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info - sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename })) - rescue Exception => e - sass_exception_string(e) - end - else - super + module Sass + def self.included(base) + base.supported_formats << "sass" end - end - # Handle Sass errors - def sass_exception_string(e) - e_string = "#{e.class}: #{e.message}" + def render_path(path) + if template_exists?(path, :sass) + begin + static_version = options.public + request.path_info + send_file(static_version) if File.exists? static_version - if e.is_a? ::Sass::SyntaxError - e_string << "\non line #{e.sass_line}" + location_of_sass_file = defined?(MIDDLEMAN_BUILDER) ? "build" : "views" + css_filename = File.join(Dir.pwd, location_of_sass_file) + request.path_info + sass(path.to_sym, Compass.sass_engine_options.merge({ :css_filename => css_filename })) + rescue Exception => e + sass_exception_string(e) + end + else + super + end + end + + # Handle Sass errors + def sass_exception_string(e) + e_string = "#{e.class}: #{e.message}" - if e.sass_filename - e_string << " of #{e.sass_filename}" + if e.is_a? ::Sass::SyntaxError + e_string << "\non line #{e.sass_line}" - if File.exists?(e.sass_filename) - e_string << "\n\n" + if e.sass_filename + e_string << " of #{e.sass_filename}" - min = [e.sass_line - 5, 0].max - begin - File.read(e.sass_filename).rstrip.split("\n")[ - min .. e.sass_line + 5 - ].each_with_index do |line, i| - e_string << "#{min + i + 1}: #{line}\n" + if File.exists?(e.sass_filename) + e_string << "\n\n" + + min = [e.sass_line - 5, 0].max + begin + File.read(e.sass_filename).rstrip.split("\n")[ + min .. e.sass_line + 5 + ].each_with_index do |line, i| + e_string << "#{min + i + 1}: #{line}\n" + end + rescue + e_string << "Couldn't read sass file: #{e.sass_filename}" end - rescue - e_string << "Couldn't read sass file: #{e.sass_filename}" end end end - end - <Hi Markaby") @@ -28,7 +28,7 @@ describe "Builder" do File.read("#{@root_dir}/build/index.html").should include("

Welcome

") end - it "should build maruku files" do + xit "should build maruku files" do File.exists?("#{@root_dir}/build/maruku.html").should be_true File.read("#{@root_dir}/build/maruku.html").should include("

Hello Maruku

") end @@ -58,4 +58,8 @@ describe "Builder" do 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") + end end \ No newline at end of file diff --git a/spec/fixtures/sample/init.rb b/spec/fixtures/sample/init.rb index 3fd42936..4f6781ad 100644 --- a/spec/fixtures/sample/init.rb +++ b/spec/fixtures/sample/init.rb @@ -1,2 +1,2 @@ -require File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "middleman", "maruku") -#require File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "middleman", "markaby") \ No newline at end of file +# enable :maruku +#enable :markaby \ No newline at end of file diff --git a/spec/fixtures/sample/public/javascripts/empty-with-include.js b/spec/fixtures/sample/public/javascripts/empty-with-include.js new file mode 100644 index 00000000..add4f7f7 --- /dev/null +++ b/spec/fixtures/sample/public/javascripts/empty-with-include.js @@ -0,0 +1 @@ +//= require "to-be-included" \ No newline at end of file diff --git a/spec/fixtures/sample/public/javascripts/to-be-included.js b/spec/fixtures/sample/public/javascripts/to-be-included.js new file mode 100644 index 00000000..f90b7eb8 --- /dev/null +++ b/spec/fixtures/sample/public/javascripts/to-be-included.js @@ -0,0 +1 @@ +function() { return "combo"; }; \ No newline at end of file diff --git a/spec/fixtures/sample/views/index.haml b/spec/fixtures/sample/views/index.html.haml similarity index 100% rename from spec/fixtures/sample/views/index.haml rename to spec/fixtures/sample/views/index.html.haml diff --git a/spec/fixtures/sample/views/inline-js.haml b/spec/fixtures/sample/views/inline-js.html.haml similarity index 100% rename from spec/fixtures/sample/views/inline-js.haml rename to spec/fixtures/sample/views/inline-js.html.haml diff --git a/spec/fixtures/sample/views/markaby.mab b/spec/fixtures/sample/views/markaby.html.mab similarity index 100% rename from spec/fixtures/sample/views/markaby.mab rename to spec/fixtures/sample/views/markaby.html.mab diff --git a/spec/fixtures/sample/views/maruku.maruku b/spec/fixtures/sample/views/maruku.html.maruku similarity index 100% rename from spec/fixtures/sample/views/maruku.maruku rename to spec/fixtures/sample/views/maruku.html.maruku diff --git a/spec/fixtures/sample/views/services/index.haml b/spec/fixtures/sample/views/services/index.html.haml similarity index 100% rename from spec/fixtures/sample/views/services/index.haml rename to spec/fixtures/sample/views/services/index.html.haml diff --git a/spec/fixtures/sample/views/stylesheets/site.sass b/spec/fixtures/sample/views/stylesheets/site.css.sass similarity index 100% rename from spec/fixtures/sample/views/stylesheets/site.sass rename to spec/fixtures/sample/views/stylesheets/site.css.sass