From d40111d67ec90073ff063c1ec27596c29c80b5f4 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Tue, 6 Oct 2009 13:56:24 -0700 Subject: [PATCH] more tweaks, rdoc --- Rakefile | 17 ++++++ bin/mm-build | 20 +++++++ bin/mm-init | 16 +++--- lib/middleman/base.rb | 5 +- lib/middleman/builder.rb | 2 +- lib/middleman/features/cache_buster.rb | 17 +++--- lib/middleman/features/haml.rb | 2 +- lib/middleman/features/smush_pngs.rb | 58 ++++++++++++++++++++ lib/middleman/template/{init.rb => init.rbt} | 4 ++ 9 files changed, 120 insertions(+), 21 deletions(-) create mode 100644 lib/middleman/features/smush_pngs.rb rename lib/middleman/template/{init.rb => init.rbt} (65%) diff --git a/Rakefile b/Rakefile index 7226e107..e24c3d1c 100644 --- a/Rakefile +++ b/Rakefile @@ -47,15 +47,32 @@ task :spec => :check_dependencies task :default => :spec require 'rake/rdoctask' +require 'sdoc' + Rake::RDocTask.new do |rdoc| if File.exist?('VERSION') version = File.read('VERSION') else version = "" end + + # rdoc.template = 'direct' rdoc.rdoc_dir = 'rdoc' rdoc.title = "middleman #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') +end + +desc "Build and publish documentation using GitHub Pages." +task :pages do + if !`git status`.include?('nothing to commit') + abort "dirty index - not publishing!" + end + + Rake::Task[:rerdoc].invoke + `git checkout gh-pages` + `ls -1 | grep -v rdoc | xargs rm -rf; mv rdoc/* .; rm -rf rdoc` + `git commit -a -m "update docs"; git push origin gh-pages` + `git checkout master` end \ No newline at end of file diff --git a/bin/mm-build b/bin/mm-build index 3c7c0d5b..efcb1b62 100755 --- a/bin/mm-build +++ b/bin/mm-build @@ -54,10 +54,30 @@ module Generators 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.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 diff --git a/bin/mm-init b/bin/mm-init index 8e5972bc..38769566 100755 --- a/bin/mm-init +++ b/bin/mm-init @@ -8,9 +8,10 @@ module Generators class NewSite < Templater::Generator desc "Creates a new staticmatic scaffold." first_argument :location, :required => true, :desc => "Project location" - - # css_dir - # images_dir + + option :css_dir, :desc => 'The path to the css files' + option :js_dir, :desc => 'The path to the javascript files' + option :images_dir, :desc => 'The path to the image files' def destination_root File.expand_path(location) @@ -20,12 +21,13 @@ module Generators File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'template') end - file :init, "init.rb" + template :init, "init.rb" glob! :views glob! :public - empty_directory :stylesheets, "public/stylesheets" - empty_directory :javascripts, "public/javascripts" - empty_directory :images, "public/images" + + empty_directory :stylesheets, File.join("public", css_dir) + empty_directory :javascripts, File.join("public", js_dir) + empty_directory :images, File.join("public", images_dir) end add :setup, NewSite diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index b67f992f..5d94bc57 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -15,8 +15,8 @@ module Middleman set :app_file, __FILE__ set :root, Dir.pwd set :environment, ENV['MM_ENV'] || :development - set :supported_formats, [] - set :index_file, 'index.html' + set :supported_formats, ["erb"] + set :index_file, "index.html" set :js_dir, "javascripts" set :css_dir, "stylesheets" set :images_dir, "images" @@ -36,6 +36,7 @@ module Middleman disable :relative_assets disable :markaby disable :maruku + disable :smush_pngs # Default build features configure :build do diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 720106cb..637f1f29 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -5,7 +5,7 @@ require 'rack/test' module Middleman class Builder # The default render just requests the page over Rack and writes the response - def self.render_file(source, destination) + 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) diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index a8433559..bf434677 100644 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -1,17 +1,14 @@ class Middleman::Base + alias_method :pre_cache_buster_asset_url, :asset_url 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 + http_path = pre_cache_buster_asset_url(path, prefix) + if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path)) + http_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 + real_path = File.join(self.class.environment == "build" ? options.build_dir : options.public, prefix, path) + http_path << "?" + File.mtime(real_path).strftime("%s") if File.readable?(real_path) + http_path end end end diff --git a/lib/middleman/features/haml.rb b/lib/middleman/features/haml.rb index 1adaf001..26f5317a 100644 --- a/lib/middleman/features/haml.rb +++ b/lib/middleman/features/haml.rb @@ -72,7 +72,7 @@ module Middleman static_version = options.public + request.path_info send_file(static_version) if File.exists? static_version - location_of_sass_file = options.environment == "build" ? "build" : "public" + location_of_sass_file = options.environment == "build" ? File.join(options.build_dir, options.css_dir) : "public" 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 diff --git a/lib/middleman/features/smush_pngs.rb b/lib/middleman/features/smush_pngs.rb new file mode 100644 index 00000000..9c4c9b71 --- /dev/null +++ b/lib/middleman/features/smush_pngs.rb @@ -0,0 +1,58 @@ +require 'json' +require 'open-uri' + +begin + require 'httpclient' +rescue LoadError + puts "httpclient not available. Install it with: gem install httpclient" +end + +module Middleman + module SmushPngs + def self.included(base) + base.supported_formats << "png" + end + + def render_path(file) + if File.extname(file) == ".png" + file = File.join(options.public, file) + optimized = optimized_image_data_for(file) + + begin + raise "Error: got larger" if size(file) < optimized.size + raise "Error: empty file downloaded" if optimized.size < 20 + + optimized + rescue + File.read(file) + end + else + super + end + end + + protected + def size(file) + File.exist?(file) ? File.size(file) : 0 + end + + def optimized_image_data_for(file) + # I leave these urls here, just in case it stops working again... + # url = "http://smush.it/ws.php" # original, redirects to somewhere else.. + url = 'http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php' + # url = "http://developer.yahoo.com/yslow/smushit/ws.php" # official but does not work + # url = "http://smushit.com/ysmush.it/ws.php" # used at the new page but does not hande uploads + # url = "http://smushit.eperf.vip.ac4.yahoo.com/ysmush.it/ws.php" # used at the new page but does not hande uploads + response = HTTPClient.post url, { 'files[]' => File.new(file) } + response = JSON.parse(response.body.content) + raise "smush.it: #{response['error']}" if response['error'] + image_url = response['dest'] + raise "no dest path found" unless image_url + open(image_url) { |source| source.read() } + end + end + + class Base + include Middleman::SmushPngs + end +end \ No newline at end of file diff --git a/lib/middleman/template/init.rb b/lib/middleman/template/init.rbt similarity index 65% rename from lib/middleman/template/init.rb rename to lib/middleman/template/init.rbt index f25ea4a5..4cdec69a 100644 --- a/lib/middleman/template/init.rb +++ b/lib/middleman/template/init.rbt @@ -1,3 +1,7 @@ +<% if css_dir %>set :css_dir, "<%= css_dir -%>"<% end %> +<% if js_dir %>set :js_dir, "<%= js_dir -%>"<% end %> +<% if images_dir %>set :images_dir, "<%= images_dir -%>"<% end %> + # Helpers helpers do end