diff --git a/bin/mm-build b/bin/mm-build index 572e5e38..ac012a46 100755 --- a/bin/mm-build +++ b/bin/mm-build @@ -1,87 +1,10 @@ #!/usr/bin/env ruby -require 'templater' - 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" - - class Builder < Templater::Generator - # Define source and desintation - def self.source_root; Dir.pwd; 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[0].include?('layout') - - args.first.split('/').each do |part| - return if part[0,1] == '_' - end - - if (args[0] === args[1]) - args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "") - .gsub("#{File.basename(Middleman::Base.public)}/", "") - if File.extname(args[1]) != ".js" - args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2 - end - end - - super(name, *args, &block) - end - - def self.file(name, *args, &block) - 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 - - glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats - glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats - - if Middleman::Base.slickmap? - template :slickmap, "sitemap.html", "sitemap.html" - end - end - - add :build, Builder -end - -# Monkey-patch to use a dynamic renderer -class Templater::Actions::File - def identical? - if exists? - return true if File.mtime(source) < File.mtime(destination) - ::FileUtils.identical?(source, destination) - else - false - end - end -end - -class Templater::Actions::Template - def render - ::Middleman::Builder.render_file(source, destination) - end - - def identical? - if ::File.exists?(destination) - return true if File.exists?(source) && File.mtime(source) < File.mtime(destination) - ::File.read(destination) == render - else - false - end - end -end - -Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV)) \ No newline at end of file +require 'middleman/builder' +Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV)) \ No newline at end of file diff --git a/bin/mm-init b/bin/mm-init index 3fc88c8c..99005033 100755 --- a/bin/mm-init +++ b/bin/mm-init @@ -6,7 +6,7 @@ module Generators desc "Generator for streamlining staticmatic" class NewSite < Templater::Generator - desc "Creates a new staticmatic scaffold." + desc "Creates a new middleman scaffold." first_argument :location, :required => true, :desc => "Project location" option :css_dir, :desc => 'The path to the css files' diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index 88c7c4e4..434e0fbe 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -55,7 +55,11 @@ module Middleman # Base case renderer (do nothing), Should be over-ridden module StaticRender def render_path(path) - false + if template_exists?(path, :erb) + erb(path.to_sym) + else + false + end end end include StaticRender @@ -113,7 +117,7 @@ module Middleman end # Require the features for this project - def self.init! + def self.init!(quiet=false) # Built-in helpers require 'middleman/helpers' helpers Middleman::Helpers @@ -124,7 +128,7 @@ module Middleman # Check for and evaluate local configuration local_config = File.join(self.root, "init.rb") if File.exists? local_config - puts "== Local config at: #{local_config}" + puts "== Local config at: #{local_config}" unless quiet class_eval File.read(local_config) end diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 637f1f29..4cd15a6b 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -1,15 +1,79 @@ -# Use Rack::Test to access Sinatra without starting up a full server -require 'rack/test' +require 'templater' +require 'rack/test' # Use Rack::Test to access Sinatra without starting up a full server # Placeholder for any methods the builder needs to abstract to allow feature integration module Middleman - class Builder - # The default render just requests the page over Rack and writes the response - 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 + class Builder < ::Templater::Generator + # Define source and desintation + def self.source_root; Dir.pwd; 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[0].include?('layout') + + args.first.split('/').each do |part| + return if part[0,1] == '_' + end + + if (args[0] === args[1]) + args[1] = args[0].gsub("#{File.basename(Middleman::Base.views)}/", "") + .gsub("#{File.basename(Middleman::Base.public)}/", "") + if File.extname(args[1]) != ".js" + args[1] = args[1].gsub!(File.extname(args[1]), "") if File.basename(args[1]).split('.').length > 2 + end + end + + super(name, *args, &block) + end + + def self.file(name, *args, &block) + 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 + + glob! File.basename(Middleman::Base.public), Middleman::Base.supported_formats + glob! File.basename(Middleman::Base.views), Middleman::Base.supported_formats + end + + module Generators + extend ::Templater::Manifold + desc "Build a static site" + + add :build, ::Middleman::Builder + end +end + +# Monkey-patch to use a dynamic renderer +class Templater::Actions::File + def identical? + if exists? + return true if File.mtime(source) < File.mtime(destination) + FileUtils.identical?(source, destination) + else + false end end end + +class Templater::Actions::Template + def render + # The default render just requests the page over Rack and writes the response + 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 + + def identical? + if File.exists?(destination) + return true if File.exists?(source) && File.mtime(source) < File.mtime(destination) + File.read(destination) == render + else + false + end + end +end \ No newline at end of file diff --git a/lib/middleman/features/slickmap.rb b/lib/middleman/features/slickmap.rb index 4f1b9280..33b8f71e 100644 --- a/lib/middleman/features/slickmap.rb +++ b/lib/middleman/features/slickmap.rb @@ -5,12 +5,14 @@ rescue LoadError puts "Slickmap not available. Install it with: gem install compass-slickmap" end +if Middleman::Base.environment == "build" + Middleman::Builder.template :slickmap, "sitemap.html", "sitemap.html" +end + Entry = Struct.new(:dir, :children) class Middleman::Base - def build_sitemap(&block) - html_files = Dir[File.join(File.dirname(Middleman::Base.views), "**", "*.html*")] - + def build_sitemap(&block) @@utility = [] [recurse_sitemap(Middleman::Base.views, &block), @@utility] end diff --git a/spec/cache_buster_spec.rb b/spec/cache_buster_spec.rb index 40fae46c..2acbe3cf 100644 --- a/spec/cache_buster_spec.rb +++ b/spec/cache_buster_spec.rb @@ -6,7 +6,7 @@ base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample") describe "Cache Buster Feature" do before do base.disable :cache_buster - base.init! + base.init!(true) @app = base.new end @@ -18,7 +18,7 @@ end describe "Cache Buster Feature" do before do base.enable :cache_buster - base.init! + base.init!(true) @app = base.new end diff --git a/spec/fixtures/sample/init.rb b/spec/fixtures/sample/init.rb index 4f6781ad..dd38727b 100644 --- a/spec/fixtures/sample/init.rb +++ b/spec/fixtures/sample/init.rb @@ -1,2 +1,2 @@ # enable :maruku -#enable :markaby \ No newline at end of file +# enable :markaby \ No newline at end of file