From 439ecb18878b7ed6e78a9a0d1dd9af62487e82de Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 28 Apr 2016 10:08:43 -0700 Subject: [PATCH] Minor performance tweak --- .../lib/middleman-core/application.rb | 6 +++--- .../lib/middleman-core/configuration.rb | 18 +++++++++++++++++- .../middleman-core/extensions/asset_hash.rb | 5 ++--- .../middleman-core/extensions/cache_buster.rb | 4 ++-- .../extensions/directory_indexes.rb | 4 +++- .../lib/middleman-core/extensions/gzip.rb | 2 +- .../middleman-core/extensions/minify_css.rb | 4 ++-- .../extensions/minify_javascript.rb | 4 ++-- .../extensions/relative_assets.rb | 4 ++-- .../lib/middleman-core/sitemap/resource.rb | 2 +- middleman-core/lib/middleman-core/util/data.rb | 2 +- .../lib/middleman-core/util/files.rb | 6 ++---- middleman-core/lib/middleman-core/util/rack.rb | 2 +- 13 files changed, 39 insertions(+), 24 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 379d1e81..4ecd40f8 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -157,12 +157,12 @@ module Middleman define_setting :layout, :_auto_layout, 'Default layout name' # Which file extensions have a layout by default. - # @return [Array.] - define_setting :extensions_with_layout, %w(.htm .html .xhtml .php), 'Which file extensions have a layout by default.' + # @return [Set.] + define_setting :extensions_with_layout, %w(.htm .html .xhtml .php), 'Which file extensions have a layout by default.', set: true # Which file extensions are "assets." # @return [Array.] - define_setting :asset_extensions, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2 .eot .ico .map), 'Which file extensions are treated as assets.' + define_setting :asset_extensions, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2 .eot .ico .map), 'Which file extensions are treated as assets.', set: true # Default string encoding for templates and output. # @return [String] diff --git a/middleman-core/lib/middleman-core/configuration.rb b/middleman-core/lib/middleman-core/configuration.rb index c916fc90..0d6d9be7 100644 --- a/middleman-core/lib/middleman-core/configuration.rb +++ b/middleman-core/lib/middleman-core/configuration.rb @@ -1,3 +1,5 @@ +require 'set' + module Middleman module Configuration # A class that manages a collection of documented settings. @@ -129,23 +131,37 @@ module Middleman def initialize(key, default, description, options={}) @value_set = false + @array_wrapped_value = nil + @array_wrapped_default = nil self.key = key self.default = default self.description = description self.options = options + + @array_wrapped_default = if self.default && options[:set] && self.default.is_a?(Array) + Set.new(self.default) + end end # The user-supplied value for this setting, overriding the default def value=(value) @value = value @value_set = true + + @array_wrapped_value = if @value && options[:set] && @value.is_a?(Array) + Set.new(@value) + end end # The effective value of the setting, which may be the default # if the user has not set a value themselves. Note that even if the # user sets the value to nil it will override the default. def value - value_set? ? @value : default + if value_set? + @array_wrapped_value ? @array_wrapped_value : @value + else + @array_wrapped_default ? @array_wrapped_default : default + end end # Whether or not there has been a value set beyond the default diff --git a/middleman-core/lib/middleman-core/extensions/asset_hash.rb b/middleman-core/lib/middleman-core/extensions/asset_hash.rb index fadf3e20..f78eed5e 100644 --- a/middleman-core/lib/middleman-core/extensions/asset_hash.rb +++ b/middleman-core/lib/middleman-core/extensions/asset_hash.rb @@ -1,8 +1,8 @@ require 'middleman-core/util' class Middleman::Extensions::AssetHash < ::Middleman::Extension - option :sources, %w(.css .htm .html .js .php .xhtml), 'List of extensions that are searched for hashable assets.' - option :exts, nil, 'List of extensions that get asset hashes appended to them.' + option :sources, %w(.css .htm .html .js .php .xhtml), 'List of extensions that are searched for hashable assets.', set: true + option :exts, nil, 'List of extensions that get asset hashes appended to them.', set: true option :ignore, [], 'Regexes of filenames to skip adding asset hashes to' option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites' @@ -30,7 +30,6 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension asset_path end - return unless asset_page = app.sitemap.find_resource_by_destination_path(full_asset_path) || app.sitemap.find_resource_by_path(full_asset_path) replacement_path = "/#{asset_page.destination_path}" diff --git a/middleman-core/lib/middleman-core/extensions/cache_buster.rb b/middleman-core/lib/middleman-core/extensions/cache_buster.rb index 769a0191..ea723a81 100644 --- a/middleman-core/lib/middleman-core/extensions/cache_buster.rb +++ b/middleman-core/lib/middleman-core/extensions/cache_buster.rb @@ -1,7 +1,7 @@ # The Cache Buster extension class Middleman::Extensions::CacheBuster < ::Middleman::Extension - option :exts, nil, 'List of extensions that get cache busters strings appended to them.' - option :sources, %w(.css .htm .html .js .php .xhtml), 'List of extensions that are searched for bustable assets.' + option :exts, nil, 'List of extensions that get cache busters strings appended to them.', set: true + option :sources, %w(.css .htm .html .js .php .xhtml), 'List of extensions that are searched for bustable assets.', set: true option :ignore, [], 'Regexes of filenames to skip adding query strings to' option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites' diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index cef88868..3bc340ee 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -1,3 +1,5 @@ +require 'set' + # Directory Indexes extension class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension # This should run after most other sitemap manipulators so that it @@ -11,7 +13,7 @@ class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension index_file = app.config[:index_file] new_index_path = "/#{index_file}" - extensions = %w(.htm .html .php .xhtml) + extensions = Set.new(%w(.htm .html .php .xhtml)) resources.each do |resource| # Check if it would be pointless to reroute diff --git a/middleman-core/lib/middleman-core/extensions/gzip.rb b/middleman-core/lib/middleman-core/extensions/gzip.rb index 0483fe32..a633f78f 100644 --- a/middleman-core/lib/middleman-core/extensions/gzip.rb +++ b/middleman-core/lib/middleman-core/extensions/gzip.rb @@ -10,7 +10,7 @@ # to .css, .htm, .html, .js, and .xhtml # class Middleman::Extensions::Gzip < ::Middleman::Extension - option :exts, %w(.css .htm .html .js .svg .xhtml), 'File extensions to Gzip when building.' + option :exts, %w(.css .htm .html .js .svg .xhtml), 'File extensions to Gzip when building.', set: true option :ignore, [], 'Patterns to avoid gzipping' option :overwrite, false, 'Overwrite original files instead of adding .gz extension.' diff --git a/middleman-core/lib/middleman-core/extensions/minify_css.rb b/middleman-core/lib/middleman-core/extensions/minify_css.rb index 35b152db..9174322f 100644 --- a/middleman-core/lib/middleman-core/extensions/minify_css.rb +++ b/middleman-core/lib/middleman-core/extensions/minify_css.rb @@ -9,8 +9,8 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension require 'sass' SassCompressor }, 'Set the CSS compressor to use.' - option :content_types, %w(text/css), 'Content types of resources that contain CSS' - option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline CSS' + option :content_types, %w(text/css), 'Content types of resources that contain CSS', set: true + option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline CSS', set: true INLINE_CSS_REGEX = /(]*>\s*(?:\/\*\*\/)?\s*<\/style>)/m diff --git a/middleman-core/lib/middleman-core/extensions/minify_javascript.rb b/middleman-core/lib/middleman-core/extensions/minify_javascript.rb index 878236fb..02172e91 100644 --- a/middleman-core/lib/middleman-core/extensions/minify_javascript.rb +++ b/middleman-core/lib/middleman-core/extensions/minify_javascript.rb @@ -9,8 +9,8 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension require 'uglifier' ::Uglifier.new }, 'Set the JS compressor to use.' - option :content_types, %w(application/javascript), 'Content types of resources that contain JS' - option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline JS' + option :content_types, %w(application/javascript), 'Content types of resources that contain JS', set: true + option :inline_content_types, %w(text/html text/php), 'Content types of resources that contain inline JS', set: true INLINE_JS_REGEX = /(]*>\s*(?:\/\/(?:(?:)|(?:\]\]>)))?\s*<\/script>)/m diff --git a/middleman-core/lib/middleman-core/extensions/relative_assets.rb b/middleman-core/lib/middleman-core/extensions/relative_assets.rb index 287cdcf1..4d2e15db 100644 --- a/middleman-core/lib/middleman-core/extensions/relative_assets.rb +++ b/middleman-core/lib/middleman-core/extensions/relative_assets.rb @@ -1,7 +1,7 @@ # Relative Assets extension class Middleman::Extensions::RelativeAssets < ::Middleman::Extension - option :exts, nil, 'List of extensions that get converted to relative paths.' - option :sources, %w(.css .htm .html .xhtml), 'List of extensions that are searched for relative assets.' + option :exts, nil, 'List of extensions that get converted to relative paths.', set: true + option :sources, %w(.css .htm .html .xhtml), 'List of extensions that are searched for relative assets.', set: true option :ignore, [], 'Regexes of filenames to skip converting to relative paths.' option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites.' option :helpers_only, false, 'Allow only Ruby helpers to change paths.' diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 4b7cefc5..f4a33f83 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -142,7 +142,7 @@ module Middleman # Render this resource # @return [String] # Contract Maybe[Hash], Maybe[Hash], Maybe[Proc] => String - def render(opts={}, locs={}, &block) + def render(opts={}, locs={}) body = render_without_filters(opts, locs) return body if @filters.empty? diff --git a/middleman-core/lib/middleman-core/util/data.rb b/middleman-core/lib/middleman-core/util/data.rb index ce73540a..b0026293 100644 --- a/middleman-core/lib/middleman-core/util/data.rb +++ b/middleman-core/lib/middleman-core/util/data.rb @@ -94,7 +94,7 @@ module Middleman .transpose .map(&::Regexp.method(:union)) - match = / + / \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)? (?#{start_delims})[ ]*\r?\n (?.*?)[ ]*\r?\n? diff --git a/middleman-core/lib/middleman-core/util/files.rb b/middleman-core/lib/middleman-core/util/files.rb index 3d185cea..607b3bd5 100644 --- a/middleman-core/lib/middleman-core/util/files.rb +++ b/middleman-core/lib/middleman-core/util/files.rb @@ -56,10 +56,8 @@ module Middleman Contract String => String def step_through_extensions(path) - while ::Middleman::Util.tilt_class(path) - ext = ::File.extname(path) - break if ext.empty? - + while ext = File.extname(path) + break if ext.empty? || !::Middleman::Util.tilt_class(ext) yield ext if block_given? # Strip templating extensions as long as Tilt knows them diff --git a/middleman-core/lib/middleman-core/util/rack.rb b/middleman-core/lib/middleman-core/util/rack.rb index a95c58a6..6783ff2d 100644 --- a/middleman-core/lib/middleman-core/util/rack.rb +++ b/middleman-core/lib/middleman-core/util/rack.rb @@ -6,7 +6,7 @@ module Middleman module_function - Contract String, String, ArrayOf[String], IsA['::Middleman::Application'], Proc => String + Contract String, String, SetOf[String], IsA['::Middleman::Application'], Proc => String def rewrite_paths(body, path, exts, app, &_block) exts = exts.sort_by(&:length).reverse matcher = /([\'\"\(,]\s*|# sourceMappingURL=)([^\s\'\"\)\(>]+(#{::Regexp.union(exts)}))/