Micro-optimizations around string comparisons
This commit is contained in:
parent
ed119bed7b
commit
923697b517
|
@ -233,7 +233,7 @@ module Middleman::Cli
|
||||||
|
|
||||||
paths = ::Middleman::Util.all_files_under(@destination)
|
paths = ::Middleman::Util.all_files_under(@destination)
|
||||||
@cleaning_queue += paths.select do |path|
|
@cleaning_queue += paths.select do |path|
|
||||||
!path.to_s.match(/\/\./) || path.to_s.match(/\.htaccess|\.htpasswd/)
|
path.to_s !~ /\/\./ || path.to_s =~ /\.(htaccess|htpasswd)/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -25,11 +25,11 @@ module Middleman
|
||||||
# parsing config.rb
|
# parsing config.rb
|
||||||
def initialize
|
def initialize
|
||||||
self.files.changed DataStore.matcher do |file|
|
self.files.changed DataStore.matcher do |file|
|
||||||
self.data.touch_file(file) if file.match(%r{^#{self.data_dir}\/})
|
self.data.touch_file(file) if file.start_with?("#{self.data_dir}/")
|
||||||
end
|
end
|
||||||
|
|
||||||
self.files.deleted DataStore.matcher do |file|
|
self.files.deleted DataStore.matcher do |file|
|
||||||
self.data.remove_file(file) if file.match(%r{^#{self.data_dir}\/})
|
self.data.remove_file(file) if file.start_with?("#{self.data_dir}/")
|
||||||
end
|
end
|
||||||
|
|
||||||
super
|
super
|
||||||
|
|
|
@ -145,7 +145,8 @@ module Middleman
|
||||||
# @param [Pathname] path
|
# @param [Pathname] path
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
def ignored?(path)
|
def ignored?(path)
|
||||||
IGNORE_LIST.any? { |r| path.to_s.match(r) }
|
path = path.to_s
|
||||||
|
IGNORE_LIST.any? { |r| path =~ r }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Notify callbacks for a file given an array of callbacks
|
# Notify callbacks for a file given an array of callbacks
|
||||||
|
|
|
@ -408,7 +408,7 @@ module Middleman
|
||||||
# Find the path by searching or using the cache
|
# Find the path by searching or using the cache
|
||||||
request_path = request_path.to_s
|
request_path = request_path.to_s
|
||||||
cache.fetch(:resolve_template, request_path, options) do
|
cache.fetch(:resolve_template, request_path, options) do
|
||||||
relative_path = request_path.sub(%r{^/}, "")
|
relative_path = Util.strip_leading_slash(request_path)
|
||||||
on_disk_path = File.expand_path(relative_path, self.source_dir)
|
on_disk_path = File.expand_path(relative_path, self.source_dir)
|
||||||
|
|
||||||
# By default, any engine will do
|
# By default, any engine will do
|
||||||
|
|
|
@ -185,7 +185,7 @@ module Middleman
|
||||||
|
|
||||||
paths.any? do |path|
|
paths.any? do |path|
|
||||||
match_against.any? do |matcher|
|
match_against.any? do |matcher|
|
||||||
path.match(matcher)
|
path =~ matcher
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,18 +25,18 @@ module Middleman
|
||||||
# Setup callbacks which can exclude paths from the sitemap
|
# Setup callbacks which can exclude paths from the sitemap
|
||||||
app.set :ignored_sitemap_matchers, {
|
app.set :ignored_sitemap_matchers, {
|
||||||
# dotfiles and folders in the root
|
# dotfiles and folders in the root
|
||||||
:root_dotfiles => proc { |file| file.match(%r{^\.}) },
|
:root_dotfiles => proc { |file| file.start_with?('.') },
|
||||||
|
|
||||||
# Files starting with an dot, but not .htaccess
|
# Files starting with an dot, but not .htaccess
|
||||||
:source_dotfiles => proc { |file|
|
:source_dotfiles => proc { |file|
|
||||||
file.match(%r{/\.}) && !file.match(%r{/\.htaccess|\.htpasswd})
|
file =~ %r{/\.} && file !~ %r{/\.(htaccess|htpasswd)}
|
||||||
},
|
},
|
||||||
|
|
||||||
# Files starting with an underscore, but not a double-underscore
|
# Files starting with an underscore, but not a double-underscore
|
||||||
:partials => proc { |file| file.match(%r{/_}) && !file.match(%r{/__}) },
|
:partials => proc { |file| file =~ %r{/_} && file !~ %r{/__} },
|
||||||
|
|
||||||
:layout => proc { |file|
|
:layout => proc { |file|
|
||||||
file.match(%r{^source/layout\.}) || file.match(%r{^source/layouts/})
|
file.start_with?('source/layout.') || file.start_with?('source/layouts/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,7 @@ module Middleman
|
||||||
blank_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] }
|
blank_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] }
|
||||||
|
|
||||||
provides_metadata.inject(blank_metadata) do |result, (callback, matcher)|
|
provides_metadata.inject(blank_metadata) do |result, (callback, matcher)|
|
||||||
next result if !matcher.nil? && !source_file.match(matcher)
|
next result if matcher && !source_file.match(matcher)
|
||||||
|
|
||||||
metadata = callback.call(source_file)
|
metadata = callback.call(source_file)
|
||||||
|
|
||||||
|
@ -146,9 +146,9 @@ module Middleman
|
||||||
@_cached_metadata[request_path] = provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)|
|
@_cached_metadata[request_path] = provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)|
|
||||||
case matcher
|
case matcher
|
||||||
when Regexp
|
when Regexp
|
||||||
next result unless request_path.match(matcher)
|
next result unless request_path =~ matcher
|
||||||
when String
|
when String
|
||||||
next result unless File.fnmatch("/" + matcher.sub(%r{^/}, ''), "/#{request_path}")
|
next result unless File.fnmatch("/" + Util.strip_leading_slash(matcher), "/#{request_path}")
|
||||||
end
|
end
|
||||||
|
|
||||||
metadata = callback.call(request_path)
|
metadata = callback.call(request_path)
|
||||||
|
|
|
@ -83,7 +83,13 @@ module Middleman
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def self.normalize_path(path)
|
def self.normalize_path(path)
|
||||||
# The tr call works around a bug in Ruby's Unicode handling
|
# The tr call works around a bug in Ruby's Unicode handling
|
||||||
path.sub(/^\//, "").tr('','')
|
path.sub(%r{^/}, "").tr('','')
|
||||||
|
end
|
||||||
|
|
||||||
|
# This is a separate method from normalize_path in case we
|
||||||
|
# change how we normalize paths
|
||||||
|
def self.strip_leading_slash(path)
|
||||||
|
path.sub(%r{^/}, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Extract the text of a Rack response as a string.
|
# Extract the text of a Rack response as a string.
|
||||||
|
|
|
@ -64,7 +64,7 @@ module Middleman
|
||||||
# If the basename of the request as no extension, assume we are serving a
|
# If the basename of the request as no extension, assume we are serving a
|
||||||
# directory and join index_file to the path.
|
# directory and join index_file to the path.
|
||||||
path = File.join(asset_dir, current_path)
|
path = File.join(asset_dir, current_path)
|
||||||
path = path.gsub(File.extname(path), ".#{asset_ext}")
|
path = path.sub(/#{File.extname(path)}$/, ".#{asset_ext}")
|
||||||
|
|
||||||
yield path if sitemap.find_resource_by_path(path)
|
yield path if sitemap.find_resource_by_path(path)
|
||||||
end
|
end
|
||||||
|
@ -74,11 +74,11 @@ module Middleman
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def page_classes
|
def page_classes
|
||||||
path = current_path.dup
|
path = current_path.dup
|
||||||
path << index_file if path.match(%r{/$})
|
path << index_file if path.end_with?('/')
|
||||||
path = path.gsub(%r{^/}, '')
|
path = Util.strip_leading_slash(path)
|
||||||
|
|
||||||
classes = []
|
classes = []
|
||||||
parts = path.split('.')[0].split('/')
|
parts = path.split('.').first.split('/')
|
||||||
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
|
parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
|
||||||
|
|
||||||
classes.join(' ')
|
classes.join(' ')
|
||||||
|
@ -90,19 +90,18 @@ module Middleman
|
||||||
# @param [String] source The path to the file
|
# @param [String] source The path to the file
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def asset_path(kind, source)
|
def asset_path(kind, source)
|
||||||
return source if source =~ /^http/
|
return source if source.to_s.include?('//')
|
||||||
asset_folder = case kind
|
asset_folder = case kind
|
||||||
when :css then css_dir
|
when :css then css_dir
|
||||||
when :js then js_dir
|
when :js then js_dir
|
||||||
when :images then images_dir
|
when :images then images_dir
|
||||||
else kind.to_s
|
else kind.to_s
|
||||||
end
|
end
|
||||||
source = source.to_s.gsub(/\s/, '')
|
source = source.to_s.tr(' ', '')
|
||||||
ignore_extension = (kind == :images) # don't append extension
|
ignore_extension = (kind == :images) # don't append extension
|
||||||
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
|
source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
|
||||||
if source =~ %r{^/} # absolute path
|
asset_folder = "" if source.start_with?('/') # absolute path
|
||||||
asset_folder = ""
|
|
||||||
end
|
|
||||||
asset_url(source, asset_folder)
|
asset_url(source, asset_folder)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ module Middleman
|
||||||
@app = app
|
@app = app
|
||||||
@locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}")
|
@locales_glob = File.join(app.locales_dir, "**", "*.{rb,yml,yaml}")
|
||||||
|
|
||||||
|
# File.fnmatch doesn't support brackets: {rb,yml}
|
||||||
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
|
regex = @locales_glob.sub(/\./, '\.').sub(File.join("**", "*"), ".*").sub(/\//, '\/').sub("{rb,yml,yaml}", "rb|ya?ml")
|
||||||
@locales_regex = %r{^#{regex}}
|
@locales_regex = %r{^#{regex}}
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ module Middleman
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_file_changed(file)
|
def on_file_changed(file)
|
||||||
if @locales_regex.match(file)
|
if @locales_regex =~ file
|
||||||
::I18n.reload!
|
::I18n.reload!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ module Middleman
|
||||||
params[:alt] ||= ""
|
params[:alt] ||= ""
|
||||||
|
|
||||||
real_path = path
|
real_path = path
|
||||||
real_path = File.join(images_dir, real_path) unless real_path =~ %r{^/}
|
real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
|
||||||
full_path = File.join(source_dir, real_path)
|
full_path = File.join(source_dir, real_path)
|
||||||
|
|
||||||
if File.exists? full_path
|
if File.exists? full_path
|
||||||
|
|
Loading…
Reference in a new issue