Move PathManager methods into its singleton class

This commit is contained in:
Ashwin Maroli 2020-11-05 17:30:59 +05:30
parent 9b96cdfa7d
commit 0dedc09ab3
1 changed files with 37 additions and 35 deletions

View File

@ -16,45 +16,47 @@ module Jekyll
# This class cannot be initialized from outside # This class cannot be initialized from outside
private_class_method :new private_class_method :new
# Wraps `File.join` to cache the frozen result. class << self
# Reassigns `nil`, empty strings and empty arrays to a frozen empty string beforehand. # Wraps `File.join` to cache the frozen result.
# # Reassigns `nil`, empty strings and empty arrays to a frozen empty string beforehand.
# Returns a frozen string. #
def self.join(base, item) # Returns a frozen string.
base = "" if base.nil? || base.empty? def join(base, item)
item = "" if item.nil? || item.empty? base = "" if base.nil? || base.empty?
@join ||= {} item = "" if item.nil? || item.empty?
@join[base] ||= {} @join ||= {}
@join[base][item] ||= File.join(base, item).freeze @join[base] ||= {}
end @join[base][item] ||= File.join(base, item).freeze
end
# Ensures the questionable path is prefixed with the base directory # Ensures the questionable path is prefixed with the base directory
# and prepends the questionable path with the base directory if false. # and prepends the questionable path with the base directory if false.
# #
# Returns a frozen string. # Returns a frozen string.
def self.sanitized_path(base_directory, questionable_path) def sanitized_path(base_directory, questionable_path)
@sanitized_path ||= {} @sanitized_path ||= {}
@sanitized_path[base_directory] ||= {} @sanitized_path[base_directory] ||= {}
@sanitized_path[base_directory][questionable_path] ||= begin @sanitized_path[base_directory][questionable_path] ||= begin
return base_directory.freeze if questionable_path.nil? return base_directory.freeze if questionable_path.nil?
clean_path = if questionable_path.start_with?("~") clean_path = if questionable_path.start_with?("~")
questionable_path.dup.insert(0, "/") questionable_path.dup.insert(0, "/")
else else
questionable_path questionable_path
end end
clean_path = File.expand_path(clean_path, "/") clean_path = File.expand_path(clean_path, "/")
return clean_path.freeze if clean_path.eql?(base_directory) return clean_path.freeze if clean_path.eql?(base_directory)
# remove any remaining extra leading slashes not stripped away by calling # remove any remaining extra leading slashes not stripped away by calling
# `File.expand_path` above. # `File.expand_path` above.
clean_path.squeeze!("/") clean_path.squeeze!("/")
if clean_path.start_with?(base_directory.sub(%r!\z!, "/")) if clean_path.start_with?(base_directory.sub(%r!\z!, "/"))
clean_path.freeze clean_path.freeze
else else
clean_path.sub!(%r!\A\w:/!, "/") clean_path.sub!(%r!\A\w:/!, "/")
join(base_directory, clean_path) join(base_directory, clean_path)
end
end end
end end
end end