From 11a35075289bddb9798aec641c01c44573f3b5cd Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 20 Apr 2013 14:27:25 -0700 Subject: [PATCH] update minify css extension, unindent other exts --- middleman-more/lib/middleman-more.rb | 6 +- .../middleman-more/extensions/asset_hash.rb | 188 +++++------ .../middleman-more/extensions/asset_host.rb | 85 +++-- .../extensions/automatic_image_sizes.rb | 68 ++-- .../middleman-more/extensions/cache_buster.rb | 104 +++--- .../extensions/directory_indexes.rb | 40 +-- .../lib/middleman-more/extensions/gzip.rb | 117 ++++--- .../lib/middleman-more/extensions/lorem.rb | 317 +++++++++--------- .../middleman-more/extensions/minify_css.rb | 142 ++++---- 9 files changed, 508 insertions(+), 559 deletions(-) diff --git a/middleman-more/lib/middleman-more.rb b/middleman-more/lib/middleman-more.rb index dc36cdd5..6c786e91 100644 --- a/middleman-more/lib/middleman-more.rb +++ b/middleman-more/lib/middleman-more.rb @@ -55,10 +55,8 @@ module Middleman end # MinifyCss compresses CSS - Middleman::Extensions.register(:minify_css) do - require "middleman-more/extensions/minify_css" - Middleman::Extensions::MinifyCss - end + require "middleman-more/extensions/minify_css" + Middleman::Extensions::MinifyCss.register # MinifyJavascript compresses JS Middleman::Extensions.register(:minify_javascript) do diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index 9be7d611..70b6dd1d 100644 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -1,108 +1,104 @@ -module Middleman - module Extensions - class AssetHash < ::Middleman::Extension - option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), "List of extensions that get asset hashes appended to them." - option :ignore, [], "Regexes of filenames to skip adding asset hashes to" +class Middleman::Extensions::AssetHash < ::Middleman::Extension + option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), "List of extensions that get asset hashes appended to them." + option :ignore, [], "Regexes of filenames to skip adding asset hashes to" - def initialize(app, options_hash={}) - super + def initialize(app, options_hash={}, &block) + super - require 'digest/sha1' - require 'rack/test' - require 'uri' + require 'digest/sha1' + require 'rack/test' + require 'uri' + end + + def after_configuration + # Allow specifying regexes to ignore, plus always ignore apple touch icons + @ignore = Array(options.ignore) + [/^apple-touch-icon/] + + app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => @ignore + end + + # Update the main sitemap resource list + # @return [void] + def manipulate_resource_list(resources) + # Process resources in order: binary images and fonts, then SVG, then JS/CSS. + # This is so by the time we get around to the text files (which may reference + # images and fonts) the static assets' hashes are already calculated. + rack_client = ::Rack::Test::Session.new(app.class.to_rack_app) + resources.sort_by do |a| + if %w(.svg).include? a.ext + 0 + elsif %w(.js .css).include? a.ext + 1 + else + -1 end + end.each do |resource| + next unless options.exts.include? resource.ext + next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } - def after_configuration - # Allow specifying regexes to ignore, plus always ignore apple touch icons - @ignore = Array(options.ignore) + [/^apple-touch-icon/] + # Render through the Rack interface so middleware and mounted apps get a shot + response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" }) + raise "#{resource.path} should be in the sitemap!" unless response.status == 200 - app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => @ignore - end + digest = Digest::SHA1.hexdigest(response.body)[0..7] - # Update the main sitemap resource list - # @return [void] - def manipulate_resource_list(resources) - # Process resources in order: binary images and fonts, then SVG, then JS/CSS. - # This is so by the time we get around to the text files (which may reference - # images and fonts) the static assets' hashes are already calculated. - rack_client = ::Rack::Test::Session.new(app.class.to_rack_app) - resources.sort_by do |a| - if %w(.svg).include? a.ext - 0 - elsif %w(.js .css).include? a.ext - 1 - else - -1 - end - end.each do |resource| - next unless options.exts.include? resource.ext - next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } - - # Render through the Rack interface so middleware and mounted apps get a shot - response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" }) - raise "#{resource.path} should be in the sitemap!" unless response.status == 200 - - digest = Digest::SHA1.hexdigest(response.body)[0..7] - - resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } - end - end - end - - # The asset hash middleware is responsible for rewriting references to - # assets to include their new, hashed name. - class Middleware - def initialize(app, options={}) - @rack_app = app - @exts = options[:exts] - @ignore = options[:ignore] - @exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|') - @middleman_app = options[:middleman_app] - end - - def call(env) - status, headers, response = @rack_app.call(env) - - # We don't want to use this middleware when rendering files to figure out their hash! - return [status, headers, response] if env["bypass_asset_hash"] == 'true' - - path = @middleman_app.full_path(env["PATH_INFO"]) - dirpath = Pathname.new(File.dirname(path)) - - if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/ - body = ::Middleman::Util.extract_response_text(response) - - if body - # TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK? - body.gsub!(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match| - opening_character = $1 - asset_path = $2 - - relative_path = Pathname.new(asset_path).relative? - - asset_path = dirpath.join(asset_path).to_s if relative_path - - if @ignore.any? { |r| asset_path.match(r) } - match - elsif asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path) - replacement_path = "/#{asset_page.destination_path}" - replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path - - "#{opening_character}#{replacement_path}" - else - match - end - end - - status, headers, response = Rack::Response.new(body, status, headers).finish - end - end - [status, headers, response] - end + resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } end end -end + # The asset hash middleware is responsible for rewriting references to + # assets to include their new, hashed name. + class Middleware + def initialize(app, options={}) + @rack_app = app + @exts = options[:exts] + @ignore = options[:ignore] + @exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|') + @middleman_app = options[:middleman_app] + end + + def call(env) + status, headers, response = @rack_app.call(env) + + # We don't want to use this middleware when rendering files to figure out their hash! + return [status, headers, response] if env["bypass_asset_hash"] == 'true' + + path = @middleman_app.full_path(env["PATH_INFO"]) + dirpath = Pathname.new(File.dirname(path)) + + if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/ + body = ::Middleman::Util.extract_response_text(response) + + if body + # TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK? + body.gsub!(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match| + opening_character = $1 + asset_path = $2 + + relative_path = Pathname.new(asset_path).relative? + + asset_path = dirpath.join(asset_path).to_s if relative_path + + if @ignore.any? { |r| asset_path.match(r) } + match + elsif asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path) + replacement_path = "/#{asset_page.destination_path}" + replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path + + "#{opening_character}#{replacement_path}" + else + match + end + end + + status, headers, response = Rack::Response.new(body, status, headers).finish + end + end + [status, headers, response] + end + end + +end # =================Temp Generate Test data============================== # ["jpg", "png", "gif"].each do |ext| diff --git a/middleman-more/lib/middleman-more/extensions/asset_host.rb b/middleman-more/lib/middleman-more/extensions/asset_host.rb index 4dd3ea3e..f6eff8d8 100644 --- a/middleman-more/lib/middleman-more/extensions/asset_host.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_host.rb @@ -1,55 +1,50 @@ -# Extensions namespace -module Middleman - module Extensions - # Asset Host module - class AssetHost < ::Middleman::Extension - option :host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host' +# Asset Host module +class Middleman::Extensions::AssetHost < ::Middleman::Extension + option :host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host' - def initialize(app, options_hash={}, &block) - super + def initialize(app, options_hash={}, &block) + super - # Backwards compatible API - app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host' + # Backwards compatible API + app.config.define_setting :asset_host, nil, 'The asset host to use, or false for no asset host, or a Proc to determine asset host' - app.compass_config do |config| - if asset_host = extensions[:asset_host].host - if asset_host.is_a?(Proc) - config.asset_host(&asset_host) - else - config.asset_host do |asset| - asset_host - end - end + app.compass_config do |config| + if asset_host = extensions[:asset_host].host + if asset_host.is_a?(Proc) + config.asset_host(&asset_host) + else + config.asset_host do |asset| + asset_host end end end - - def host - app.config[:asset_host] || options[:host] - end - - helpers do - # Override default asset url helper to include asset hosts - # - # @param [String] path - # @param [String] prefix - # @return [String] - def asset_url(path, prefix="") - controller = extensions[:asset_host] - - original_output = super - return original_output unless controller.host - - asset_prefix = if controller.host.is_a?(Proc) - controller.host.call(original_output) - elsif controller.host.is_a?(String) - controller.host - end - - File.join(asset_prefix, original_output) - end - end + end + end + + def host + app.config[:asset_host] || options[:host] + end + + helpers do + # Override default asset url helper to include asset hosts + # + # @param [String] path + # @param [String] prefix + # @return [String] + def asset_url(path, prefix="") + controller = extensions[:asset_host] + + original_output = super + return original_output unless controller.host + + asset_prefix = if controller.host.is_a?(Proc) + controller.host.call(original_output) + elsif controller.host.is_a?(String) + controller.host + end + + File.join(asset_prefix, original_output) end end end diff --git a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb index 32205bf7..93a2f58c 100644 --- a/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb +++ b/middleman-more/lib/middleman-more/extensions/automatic_image_sizes.rb @@ -1,48 +1,42 @@ -# Extensions namespace -module Middleman - module Extensions +# Automatic Image Sizes extension +class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension - # Automatic Image Sizes extension - class AutomaticImageSizes < ::Middleman::Extension + def initialize(app, options_hash={}, &block) + super - def initialize(app, options_hash={}, &block) - super + # Include 3rd-party fastimage library + require "middleman-more/extensions/automatic_image_sizes/fastimage" + end - # Include 3rd-party fastimage library - require "middleman-more/extensions/automatic_image_sizes/fastimage" - end + helpers do + # Override default image_tag helper to automatically calculate and include + # image dimensions. + # + # @param [String] path + # @param [Hash] params + # @return [String] + def image_tag(path, params={}) + if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://") + params[:alt] ||= "" - helpers do - # Override default image_tag helper to automatically calculate and include - # image dimensions. - # - # @param [String] path - # @param [Hash] params - # @return [String] - def image_tag(path, params={}) - if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://") - params[:alt] ||= "" + real_path = path + real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') + full_path = File.join(source_dir, real_path) - real_path = path - real_path = File.join(images_dir, real_path) unless real_path.start_with?('/') - full_path = File.join(source_dir, real_path) - - if File.exists?(full_path) - begin - width, height = ::FastImage.size(full_path, :raise_on_failure => true) - params[:width] = width - params[:height] = height - rescue FastImage::UnknownImageType - # No message, it's just not supported - rescue - warn "Couldn't determine dimensions for image #{path}: #{$!.message}" - end - end + if File.exists?(full_path) + begin + width, height = ::FastImage.size(full_path, :raise_on_failure => true) + params[:width] = width + params[:height] = height + rescue FastImage::UnknownImageType + # No message, it's just not supported + rescue + warn "Couldn't determine dimensions for image #{path}: #{$!.message}" end - - super(path, params) end end + + super(path, params) end end end diff --git a/middleman-more/lib/middleman-more/extensions/cache_buster.rb b/middleman-more/lib/middleman-more/extensions/cache_buster.rb index ec48ee9d..a252fd5c 100644 --- a/middleman-more/lib/middleman-more/extensions/cache_buster.rb +++ b/middleman-more/lib/middleman-more/extensions/cache_buster.rb @@ -1,62 +1,56 @@ -# Extension namespace -module Middleman - module Extensions +# The Cache Buster extension +class Middleman::Extensions::CacheBuster < ::Middleman::Extension - # The Cache Buster extension - class CacheBuster < ::Middleman::Extension + def initialize(app, options_hash={}, &block) + super - def initialize(app, options_hash={}, &block) - super - - # After compass is setup, make it use the registered cache buster - app.compass_config do |config| - config.asset_cache_buster do |path, real_path| - real_path = real_path.path if real_path.is_a? File - real_path = real_path.gsub(File.join(root, build_dir), source) - if File.readable?(real_path) - File.mtime(real_path).strftime("%s") - else - logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" - end - end - end - end - - helpers do - # asset_url override if we're using cache busting - # @param [String] path - # @param [String] prefix - def asset_url(path, prefix="") - http_path = super - - if http_path.include?("://") || !%w(.css .png .jpg .jpeg .svg .svgz .js .gif).include?(File.extname(http_path)) - http_path - else - if respond_to?(:http_images_path) && prefix == http_images_path - prefix = images_dir - end - - real_path_static = File.join(prefix, path) - - if build? - real_path_dynamic = File.join(build_dir, prefix, path) - real_path_dynamic = File.expand_path(real_path_dynamic, root) - http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) - elsif resource = sitemap.find_resource_by_path(real_path_static) - if !resource.template? - http_path << "?" + File.mtime(resource.source_file).strftime("%s") - else - # It's a template, possible with partials. We can't really - # know when it's updated, so generate fresh cache buster every - # time during developement - http_path << "?" + Time.now.strftime("%s") - end - end - - http_path - end + # After compass is setup, make it use the registered cache buster + app.compass_config do |config| + config.asset_cache_buster do |path, real_path| + real_path = real_path.path if real_path.is_a? File + real_path = real_path.gsub(File.join(root, build_dir), source) + if File.readable?(real_path) + File.mtime(real_path).strftime("%s") + else + logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}" end end end end + + helpers do + # asset_url override if we're using cache busting + # @param [String] path + # @param [String] prefix + def asset_url(path, prefix="") + http_path = super + + if http_path.include?("://") || !%w(.css .png .jpg .jpeg .svg .svgz .js .gif).include?(File.extname(http_path)) + http_path + else + if respond_to?(:http_images_path) && prefix == http_images_path + prefix = images_dir + end + + real_path_static = File.join(prefix, path) + + if build? + real_path_dynamic = File.join(build_dir, prefix, path) + real_path_dynamic = File.expand_path(real_path_dynamic, root) + http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic) + elsif resource = sitemap.find_resource_by_path(real_path_static) + if !resource.template? + http_path << "?" + File.mtime(resource.source_file).strftime("%s") + else + # It's a template, possible with partials. We can't really + # know when it's updated, so generate fresh cache buster every + # time during developement + http_path << "?" + Time.now.strftime("%s") + end + end + + http_path + end + end + end end diff --git a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb index 4394ca3a..a082abeb 100644 --- a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb +++ b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb @@ -1,30 +1,24 @@ -# Extensions namespace -module Middleman - module Extensions +# Directory Indexes extension +class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension + # Update the main sitemap resource list + # @return [void] + def manipulate_resource_list(resources) + index_file = app.index_file + new_index_path = "/#{index_file}" - # Directory Indexes extension - class DirectoryIndexes < ::Middleman::Extension - # Update the main sitemap resource list - # @return [void] - def manipulate_resource_list(resources) - index_file = app.index_file - new_index_path = "/#{index_file}" + resources.each do |resource| + # Check if it would be pointless to reroute + next if resource.destination_path == index_file || + resource.destination_path.end_with?(new_index_path) || + File.extname(index_file) != resource.ext - resources.each do |resource| - # Check if it would be pointless to reroute - next if resource.destination_path == index_file || - resource.destination_path.end_with?(new_index_path) || - File.extname(index_file) != resource.ext + # Check if frontmatter turns directory_index off + next if resource.data[:directory_index] == false - # Check if frontmatter turns directory_index off - next if resource.data[:directory_index] == false + # Check if file metadata (options set by "page" in config.rb) turns directory_index off + next if resource.metadata[:options][:directory_index] == false - # Check if file metadata (options set by "page" in config.rb) turns directory_index off - next if resource.metadata[:options][:directory_index] == false - - resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path - end - end + resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path end end end diff --git a/middleman-more/lib/middleman-more/extensions/gzip.rb b/middleman-more/lib/middleman-more/extensions/gzip.rb index dbe0a4f6..653ff7c7 100644 --- a/middleman-more/lib/middleman-more/extensions/gzip.rb +++ b/middleman-more/lib/middleman-more/extensions/gzip.rb @@ -1,73 +1,70 @@ -module Middleman::Extensions +# This extension Gzips assets and pages when building. +# Gzipped assets and pages can be served directly by Apache or +# Nginx with the proper configuration, and pre-zipping means that we +# can use a more agressive compression level at no CPU cost per request. +# +# Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache +# to serve your Gzipped files whenever the normal (non-.gz) filename is requested. +# +# Pass the :exts options to customize which file extensions get zipped (defaults +# to .html, .htm, .js and .css. +# +class Middleman::Extensions::Gzip < ::Middleman::Extension + option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.' - # This extension Gzips assets and pages when building. - # Gzipped assets and pages can be served directly by Apache or - # Nginx with the proper configuration, and pre-zipping means that we - # can use a more agressive compression level at no CPU cost per request. - # - # Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache - # to serve your Gzipped files whenever the normal (non-.gz) filename is requested. - # - # Pass the :exts options to customize which file extensions get zipped (defaults - # to .html, .htm, .js and .css. - # - class Gzip < ::Middleman::Extension - option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.' + def initialize(app, options_hash={}) + super + + require 'zlib' + require 'stringio' + require 'find' - def initialize(app, options_hash={}) - super - - require 'zlib' - require 'stringio' - require 'find' + gzip_ext = self - gzip_ext = self + app.after_build do |builder| + paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir) + paths.each do |path| + next unless gzip_ext.options.exts.include? path.extname - app.after_build do |builder| - paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir) - paths.each do |path| - next unless gzip_ext.options.exts.include? path.extname + output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s) - output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s) - - if output_filename - size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger' - old_locale = I18n.locale - I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist - builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})" - I18n.locale = old_locale - end + if output_filename + size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger' + old_locale = I18n.locale + I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist + builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})" + I18n.locale = old_locale end end end + end - def gzip_file(path) - input_file = File.open(path, 'rb').read - output_filename = path + '.gz' - input_file_time = File.mtime(path) + def gzip_file(path) + input_file = File.open(path, 'rb').read + output_filename = path + '.gz' + input_file_time = File.mtime(path) - # Check if the right file's already there - if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time - return - end - - File.open(output_filename, 'wb') do |f| - gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) - gz.mtime = input_file_time.to_i - gz.write input_file - gz.close - end - - # Make the file times match, both for Nginx's gzip_static extension - # and so we can ID existing files. Also, so even if the GZ files are - # wiped out by build --clean and recreated, we won't rsync them over - # again because they'll end up with the same mtime. - File.utime(File.atime(output_filename), input_file_time, output_filename) - - old_size = File.size(path) - new_size = File.size(output_filename) - - [output_filename, old_size, new_size] + # Check if the right file's already there + if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time + return end + + File.open(output_filename, 'wb') do |f| + gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION) + gz.mtime = input_file_time.to_i + gz.write input_file + gz.close + end + + # Make the file times match, both for Nginx's gzip_static extension + # and so we can ID existing files. Also, so even if the GZ files are + # wiped out by build --clean and recreated, we won't rsync them over + # again because they'll end up with the same mtime. + File.utime(File.atime(output_filename), input_file_time, output_filename) + + old_size = File.size(path) + new_size = File.size(output_filename) + + [output_filename, old_size, new_size] end end diff --git a/middleman-more/lib/middleman-more/extensions/lorem.rb b/middleman-more/lib/middleman-more/extensions/lorem.rb index fd87682b..74486b8c 100644 --- a/middleman-more/lib/middleman-more/extensions/lorem.rb +++ b/middleman-more/lib/middleman-more/extensions/lorem.rb @@ -1,182 +1,175 @@ -# Extension namespace -module Middleman - module Extensions +class Middleman::Extensions::Lorem < ::Middleman::Extension + helpers do + # Access to the Lorem object + # @return [Middleman::Extensions::Lorem::LoremObject] + def lorem + LoremObject + end - # Lorem helper - class Lorem < ::Middleman::Extension - helpers do - # Access to the Lorem object - # @return [Middleman::Extensions::Lorem::LoremObject] - def lorem - LoremObject - end + # Return a placeholder image using placekitten.com + # + # @param [String] size + # @param [Hash] options + # @return [String] + def placekitten(size, options={}) + options[:domain] = "http://placekitten.com" + lorem.image(size, options) + end + end - # Return a placeholder image using placekitten.com - # - # @param [String] size - # @param [Hash] options - # @return [String] - def placekitten(size, options={}) - options[:domain] = "http://placekitten.com" - lorem.image(size, options) - end + # Adapted from Frank: + # https://github.com/blahed/frank/ + # Copyright (c) 2010 Travis Dunn + # + # Permission is hereby granted, free of charge, to any person + # obtaining a copy of this software and associated documentation + # files (the "Software"), to deal in the Software without + # restriction, including without limitation the rights to use, + # copy, modify, merge, publish, distribute, sublicense, and/or sell + # copies of the Software, and to permit persons to whom the + # Software is furnished to do so, subject to the following + # conditions: + # + # The above copyright notice and this permission notice shall be + # included in all copies or substantial portions of the Software. + # + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + # OTHER DEALINGS IN THE SOFTWARE. + module LoremObject + class << self + # Words for use in lorem text + WORDS = %w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat) + + # Get one placeholder word + # @return [String] + def word + words(1) end - # Adapted from Frank: - # https://github.com/blahed/frank/ - # Copyright (c) 2010 Travis Dunn - # - # Permission is hereby granted, free of charge, to any person - # obtaining a copy of this software and associated documentation - # files (the "Software"), to deal in the Software without - # restriction, including without limitation the rights to use, - # copy, modify, merge, publish, distribute, sublicense, and/or sell - # copies of the Software, and to permit persons to whom the - # Software is furnished to do so, subject to the following - # conditions: - # - # The above copyright notice and this permission notice shall be - # included in all copies or substantial portions of the Software. - # - # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - # OTHER DEALINGS IN THE SOFTWARE. - module LoremObject - class << self - # Words for use in lorem text - WORDS = %w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat) + # Get some number of placeholder words + # @param [Fixnum] total + # @return [String] + def words(total) + (1..total).map do + randm(WORDS) + end.join(' ') + end - # Get one placeholder word - # @return [String] - def word - words(1) - end + # Get one placeholder sentence + # @return [String] + def sentence + sentences(1) + end - # Get some number of placeholder words - # @param [Fixnum] total - # @return [String] - def words(total) - (1..total).map do - randm(WORDS) - end.join(' ') - end + # Get some number of placeholder sentences + # @param [Fixnum] total + # @return [String] + def sentences(total) + (1..total).map do + words(randm(4..15)).capitalize + end.join('. ') + end - # Get one placeholder sentence - # @return [String] - def sentence - sentences(1) - end + # Get one placeholder paragraph + # @return [String] + def paragraph + paragraphs(1) + end - # Get some number of placeholder sentences - # @param [Fixnum] total - # @return [String] - def sentences(total) - (1..total).map do - words(randm(4..15)).capitalize - end.join('. ') - end + # Get some number of placeholder paragraphs + # @param [Fixnum] total + # @return [String] + def paragraphs(total) + (1..total).map do + sentences(randm(3..7)).capitalize + end.join("\n\n") + end - # Get one placeholder paragraph - # @return [String] - def paragraph - paragraphs(1) - end + # Get a placeholder date + # @param [String] fmt + # @return [String] + def date(fmt = '%a %b %d, %Y') + y = rand(20) + 1990 + m = rand(12) + 1 + d = rand(31) + 1 + Time.local(y,m,d).strftime(fmt) + end - # Get some number of placeholder paragraphs - # @param [Fixnum] total - # @return [String] - def paragraphs(total) - (1..total).map do - sentences(randm(3..7)).capitalize - end.join("\n\n") - end + # Get a placeholder name + # @return [String] + def name + "#{first_name} #{last_name}" + end - # Get a placeholder date - # @param [String] fmt - # @return [String] - def date(fmt = '%a %b %d, %Y') - y = rand(20) + 1990 - m = rand(12) + 1 - d = rand(31) + 1 - Time.local(y,m,d).strftime(fmt) - end + # Get a placeholder first name + # @return [String] + def first_name + names = "Judith Angelo Margarita Kerry Elaine Lorenzo Justice Doris Raul Liliana Kerry Elise Ciaran Johnny Moses Davion Penny Mohammed Harvey Sheryl Hudson Brendan Brooklynn Denis Sadie Trisha Jacquelyn Virgil Cindy Alexa Marianne Giselle Casey Alondra Angela Katherine Skyler Kyleigh Carly Abel Adrianna Luis Dominick Eoin Noel Ciara Roberto Skylar Brock Earl Dwayne Jackie Hamish Sienna Nolan Daren Jean Shirley Connor Geraldine Niall Kristi Monty Yvonne Tammie Zachariah Fatima Ruby Nadia Anahi Calum Peggy Alfredo Marybeth Bonnie Gordon Cara John Staci Samuel Carmen Rylee Yehudi Colm Beth Dulce Darius inley Javon Jason Perla Wayne Laila Kaleigh Maggie Don Quinn Collin Aniya Zoe Isabel Clint Leland Esmeralda Emma Madeline Byron Courtney Vanessa Terry Antoinette George Constance Preston Rolando Caleb Kenneth Lynette Carley Francesca Johnnie Jordyn Arturo Camila Skye Guy Ana Kaylin Nia Colton Bart Brendon Alvin Daryl Dirk Mya Pete Joann Uriel Alonzo Agnes Chris Alyson Paola Dora Elias Allen Jackie Eric Bonita Kelvin Emiliano Ashton Kyra Kailey Sonja Alberto Ty Summer Brayden Lori Kelly Tomas Joey Billie Katie Stephanie Danielle Alexis Jamal Kieran Lucinda Eliza Allyson Melinda Alma Piper Deana Harriet Bryce Eli Jadyn Rogelio Orlaith Janet Randal Toby Carla Lorie Caitlyn Annika Isabelle inn Ewan Maisie Michelle Grady Ida Reid Emely Tricia Beau Reese Vance Dalton Lexi Rafael Makenzie Mitzi Clinton Xena Angelina Kendrick Leslie Teddy Jerald Noelle Neil Marsha Gayle Omar Abigail Alexandra Phil Andre Billy Brenden Bianca Jared Gretchen Patrick Antonio Josephine Kyla Manuel Freya Kellie Tonia Jamie Sydney Andres Ruben Harrison Hector Clyde Wendell Kaden Ian Tracy Cathleen Shawn".split(" ") + names[rand(names.size)] + end - # Get a placeholder name - # @return [String] - def name - "#{first_name} #{last_name}" - end + # Get a placeholder last name + # @return [String] + def last_name + names = "Chung Chen Melton Hill Puckett Song Hamilton Bender Wagner McLaughlin McNamara Raynor Moon Woodard Desai Wallace Lawrence Griffin Dougherty Powers May Steele Teague Vick Gallagher Solomon Walsh Monroe Connolly Hawkins Middleton Goldstein Watts Johnston Weeks Wilkerson Barton Walton Hall Ross Chung Bender Woods Mangum Joseph Rosenthal Bowden Barton Underwood Jones Baker Merritt Cross Cooper Holmes Sharpe Morgan Hoyle Allen Rich Rich Grant Proctor Diaz Graham Watkins Hinton Marsh Hewitt Branch Walton O'Brien Case Watts Christensen Parks Hardin Lucas Eason Davidson Whitehead Rose Sparks Moore Pearson Rodgers Graves Scarborough Sutton Sinclair Bowman Olsen Love McLean Christian Lamb James Chandler Stout Cowan Golden Bowling Beasley Clapp Abrams Tilley Morse Boykin Sumner Cassidy Davidson Heath Blanchard McAllister McKenzie Byrne Schroeder Griffin Gross Perkins Robertson Palmer Brady Rowe Zhang Hodge Li Bowling Justice Glass Willis Hester Floyd Graves Fischer Norman Chan Hunt Byrd Lane Kaplan Heller May Jennings Hanna Locklear Holloway Jones Glover Vick O'Donnell Goldman McKenna Starr Stone McClure Watson Monroe Abbott Singer Hall Farrell Lucas Norman Atkins Monroe Robertson Sykes Reid Chandler Finch Hobbs Adkins Kinney Whitaker Alexander Conner Waters Becker Rollins Love Adkins Black Fox Hatcher Wu Lloyd Joyce Welch Matthews Chappell MacDonald Kane Butler Pickett Bowman Barton Kennedy Branch Thornton McNeill Weinstein Middleton Moss Lucas Rich Carlton Brady Schultz Nichols Harvey Stevenson Houston Dunn West O'Brien Barr Snyder Cain Heath Boswell Olsen Pittman Weiner Petersen Davis Coleman Terrell Norman Burch Weiner Parrott Henry Gray Chang McLean Eason Weeks Siegel Puckett Heath Hoyle Garrett Neal Baker Goldman Shaffer Choi Carver".split(" ") + names[rand(names.size)] + end - # Get a placeholder first name - # @return [String] - def first_name - names = "Judith Angelo Margarita Kerry Elaine Lorenzo Justice Doris Raul Liliana Kerry Elise Ciaran Johnny Moses Davion Penny Mohammed Harvey Sheryl Hudson Brendan Brooklynn Denis Sadie Trisha Jacquelyn Virgil Cindy Alexa Marianne Giselle Casey Alondra Angela Katherine Skyler Kyleigh Carly Abel Adrianna Luis Dominick Eoin Noel Ciara Roberto Skylar Brock Earl Dwayne Jackie Hamish Sienna Nolan Daren Jean Shirley Connor Geraldine Niall Kristi Monty Yvonne Tammie Zachariah Fatima Ruby Nadia Anahi Calum Peggy Alfredo Marybeth Bonnie Gordon Cara John Staci Samuel Carmen Rylee Yehudi Colm Beth Dulce Darius inley Javon Jason Perla Wayne Laila Kaleigh Maggie Don Quinn Collin Aniya Zoe Isabel Clint Leland Esmeralda Emma Madeline Byron Courtney Vanessa Terry Antoinette George Constance Preston Rolando Caleb Kenneth Lynette Carley Francesca Johnnie Jordyn Arturo Camila Skye Guy Ana Kaylin Nia Colton Bart Brendon Alvin Daryl Dirk Mya Pete Joann Uriel Alonzo Agnes Chris Alyson Paola Dora Elias Allen Jackie Eric Bonita Kelvin Emiliano Ashton Kyra Kailey Sonja Alberto Ty Summer Brayden Lori Kelly Tomas Joey Billie Katie Stephanie Danielle Alexis Jamal Kieran Lucinda Eliza Allyson Melinda Alma Piper Deana Harriet Bryce Eli Jadyn Rogelio Orlaith Janet Randal Toby Carla Lorie Caitlyn Annika Isabelle inn Ewan Maisie Michelle Grady Ida Reid Emely Tricia Beau Reese Vance Dalton Lexi Rafael Makenzie Mitzi Clinton Xena Angelina Kendrick Leslie Teddy Jerald Noelle Neil Marsha Gayle Omar Abigail Alexandra Phil Andre Billy Brenden Bianca Jared Gretchen Patrick Antonio Josephine Kyla Manuel Freya Kellie Tonia Jamie Sydney Andres Ruben Harrison Hector Clyde Wendell Kaden Ian Tracy Cathleen Shawn".split(" ") - names[rand(names.size)] - end + # Get a placeholder 140 character tweet about Philip the Purple Otter + # Via http://www.kevadamson.com/talking-of-design/article/140-alternative-characters-to-lorem-ipsum + # @return [String] + def tweet + tweets = [ 'Far away, in a forest next to a river beneath the mountains, there lived a small purple otter called Philip. Philip likes sausages. The End.', + 'He liked the quality sausages from Marks & Spencer but due to the recession he had been forced to shop in a less desirable supermarket. End.', + 'He awoke one day to find his pile of sausages missing. Roger the greedy boar with human eyes, had skateboarded into the forest & eaten them!'] + tweets[rand(tweets.size)] + end - # Get a placeholder last name - # @return [String] - def last_name - names = "Chung Chen Melton Hill Puckett Song Hamilton Bender Wagner McLaughlin McNamara Raynor Moon Woodard Desai Wallace Lawrence Griffin Dougherty Powers May Steele Teague Vick Gallagher Solomon Walsh Monroe Connolly Hawkins Middleton Goldstein Watts Johnston Weeks Wilkerson Barton Walton Hall Ross Chung Bender Woods Mangum Joseph Rosenthal Bowden Barton Underwood Jones Baker Merritt Cross Cooper Holmes Sharpe Morgan Hoyle Allen Rich Rich Grant Proctor Diaz Graham Watkins Hinton Marsh Hewitt Branch Walton O'Brien Case Watts Christensen Parks Hardin Lucas Eason Davidson Whitehead Rose Sparks Moore Pearson Rodgers Graves Scarborough Sutton Sinclair Bowman Olsen Love McLean Christian Lamb James Chandler Stout Cowan Golden Bowling Beasley Clapp Abrams Tilley Morse Boykin Sumner Cassidy Davidson Heath Blanchard McAllister McKenzie Byrne Schroeder Griffin Gross Perkins Robertson Palmer Brady Rowe Zhang Hodge Li Bowling Justice Glass Willis Hester Floyd Graves Fischer Norman Chan Hunt Byrd Lane Kaplan Heller May Jennings Hanna Locklear Holloway Jones Glover Vick O'Donnell Goldman McKenna Starr Stone McClure Watson Monroe Abbott Singer Hall Farrell Lucas Norman Atkins Monroe Robertson Sykes Reid Chandler Finch Hobbs Adkins Kinney Whitaker Alexander Conner Waters Becker Rollins Love Adkins Black Fox Hatcher Wu Lloyd Joyce Welch Matthews Chappell MacDonald Kane Butler Pickett Bowman Barton Kennedy Branch Thornton McNeill Weinstein Middleton Moss Lucas Rich Carlton Brady Schultz Nichols Harvey Stevenson Houston Dunn West O'Brien Barr Snyder Cain Heath Boswell Olsen Pittman Weiner Petersen Davis Coleman Terrell Norman Burch Weiner Parrott Henry Gray Chang McLean Eason Weeks Siegel Puckett Heath Hoyle Garrett Neal Baker Goldman Shaffer Choi Carver".split(" ") - names[rand(names.size)] - end + # Get a placeholder email address + # @return [String] + def email + delimiters = [ '_', '-', '' ] + domains = %w(gmail.com yahoo.com hotmail.com email.com live.com me.com mac.com aol.com fastmail.com mail.com) + username = name.gsub(/[^\w]/, delimiters[rand(delimiters.size)]) + "#{username}@#{domains[rand(domains.size)]}".downcase + end - # Get a placeholder 140 character tweet about Philip the Purple Otter - # Via http://www.kevadamson.com/talking-of-design/article/140-alternative-characters-to-lorem-ipsum - # @return [String] - def tweet - tweets = [ 'Far away, in a forest next to a river beneath the mountains, there lived a small purple otter called Philip. Philip likes sausages. The End.', - 'He liked the quality sausages from Marks & Spencer but due to the recession he had been forced to shop in a less desirable supermarket. End.', - 'He awoke one day to find his pile of sausages missing. Roger the greedy boar with human eyes, had skateboarded into the forest & eaten them!'] - tweets[rand(tweets.size)] - end + # Get a placeholder image, using placehold.it by default + # @param [String] size + # @param [Hash] options + # @return [String] + def image(size, options={}) + domain = options[:domain] || "http://placehold.it" + src = "#{domain}/#{size}" + hex = %w[a b c d e f 0 1 2 3 4 5 6 7 8 9] + background_color = options[:background_color] + color = options[:color] - # Get a placeholder email address - # @return [String] - def email - delimiters = [ '_', '-', '' ] - domains = %w(gmail.com yahoo.com hotmail.com email.com live.com me.com mac.com aol.com fastmail.com mail.com) - username = name.gsub(/[^\w]/, delimiters[rand(delimiters.size)]) - "#{username}@#{domains[rand(domains.size)]}".downcase - end - - # Get a placeholder image, using placehold.it by default - # @param [String] size - # @param [Hash] options - # @return [String] - def image(size, options={}) - domain = options[:domain] || "http://placehold.it" - src = "#{domain}/#{size}" - hex = %w[a b c d e f 0 1 2 3 4 5 6 7 8 9] - background_color = options[:background_color] - color = options[:color] - - if options[:random_color] - background_color = hex.shuffle[0...6].join - color = hex.shuffle[0...6].join - end - - src << "/#{background_color.sub(/^#/, '')}" if background_color - src << "/ccc" if background_color.nil? && color - src << "/#{color.sub(/^#/, '')}" if color - src << "&text=#{Rack::Utils::escape(options[:text])}" if options[:text] - - src - end - - # Pick a random item from a given range - # @param [Range] range - # @return [Object] - def randm(range) - a = range.to_a - a[rand(a.length)] - end + if options[:random_color] + background_color = hex.shuffle[0...6].join + color = hex.shuffle[0...6].join end + + src << "/#{background_color.sub(/^#/, '')}" if background_color + src << "/ccc" if background_color.nil? && color + src << "/#{color.sub(/^#/, '')}" if color + src << "&text=#{Rack::Utils::escape(options[:text])}" if options[:text] + + src + end + + # Pick a random item from a given range + # @param [Range] range + # @return [Object] + def randm(range) + a = range.to_a + a[rand(a.length)] end end end diff --git a/middleman-more/lib/middleman-more/extensions/minify_css.rb b/middleman-more/lib/middleman-more/extensions/minify_css.rb index 43f64391..f5914f2d 100644 --- a/middleman-more/lib/middleman-more/extensions/minify_css.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_css.rb @@ -1,89 +1,77 @@ -# Extensions namespace -module Middleman - module Extensions +# Minify CSS Extension +class Middleman::Extensions::MinifyCss < ::Middleman::Extension + option :compressor, nil, 'Set the CSS compressor to use.' + option :inline, false, 'Whether to minify CSS inline within HTML files' + option :ignore, [], 'Patterns to avoid minifying' - # Minify CSS Extension - module MinifyCss + def initialize(app, options_hash={}, &block) + super - # Setup extension - class << self + app.config.define_setting :css_compressor, nil, 'Set the CSS compressor to use. Deprecated in favor of the :compressor option when activating :minify_css' + end - # Once registered - def registered(app, options={}) - app.config.define_setting :css_compressor, nil, 'Set the CSS compressor to use. Deprecated in favor of the :compressor option when activating :minify_css' + def after_configuration + chosen_compressor = app.config[:css_compressor] || options[:compressor] || SassCompressor - ignore = Array(options[:ignore]) << /\.min\./ - inline = options[:inline] || false + # Setup Rack middleware to minify CSS + app.use Rack, :compressor => chosen_compressor, + :ignore => options[:ignore] + [/\.min\./], + :inline => options[:inline] + end - app.after_configuration do - chosen_compressor = config[:css_compressor] || options[:compressor] || begin - ::Middleman::Extensions::MinifyCss::SassCompressor - end + class SassCompressor + def self.compress(style, options = {}) + root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse + root_node.options = { :style => :compressed } + root_node.render.strip + end + end - # Setup Rack middleware to minify CSS - use Rack, :compressor => chosen_compressor, - :ignore => ignore, - :inline => inline - end + # Rack middleware to look for CSS and compress it + class Rack + + # Init + # @param [Class] app + # @param [Hash] options + def initialize(app, options={}) + @app = app + @compressor = options[:compressor] + @ignore = options[:ignore] + @inline = options[:inline] + end + + # Rack interface + # @param [Rack::Environmemt] env + # @return [Array] + def call(env) + status, headers, response = @app.call(env) + + path = env["PATH_INFO"] + + if (path.end_with?('.html') || path.end_with?('.php')) && @inline + uncompressed_source = ::Middleman::Util.extract_response_text(response) + + minified = uncompressed_source.gsub(/(]*>\s*(?:\/\*\*\/)?\s*<\/style>)/m) do |match| + first = $1 + css = $2 + last = $3 + + minified_css = @compressor.compress(css) + + first << minified_css << last end - alias :included :registered + + headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s + response = [minified] + elsif path.end_with?('.css') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) } + uncompressed_source = ::Middleman::Util.extract_response_text(response) + minified_css = @compressor.compress(uncompressed_source) + + headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s + response = [minified_css] end - class SassCompressor - def self.compress(style, options = {}) - root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse - root_node.options = { :style => :compressed } - root_node.render.strip - end - end - - # Rack middleware to look for CSS and compress it - class Rack - - # Init - # @param [Class] app - # @param [Hash] options - def initialize(app, options={}) - @app = app - @compressor = options[:compressor] - @ignore = options[:ignore] - @inline = options[:inline] - end - - # Rack interface - # @param [Rack::Environmemt] env - # @return [Array] - def call(env) - status, headers, response = @app.call(env) - - path = env["PATH_INFO"] - - if (path.end_with?('.html') || path.end_with?('.php')) && @inline - uncompressed_source = ::Middleman::Util.extract_response_text(response) - - minified = uncompressed_source.gsub(/(]*>\s*(?:\/\*\*\/)?\s*<\/style>)/m) do |match| - first = $1 - css = $2 - last = $3 - - minified_css = @compressor.compress(css) - - first << minified_css << last - end - - headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s - response = [minified] - elsif path.end_with?('.css') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) } - uncompressed_source = ::Middleman::Util.extract_response_text(response) - minified_css = @compressor.compress(uncompressed_source) - - headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s - response = [minified_css] - end - - [status, headers, response] - end - end + [status, headers, response] end end end