middleman/middleman-core/lib/middleman-core/util.rb

465 lines
14 KiB
Ruby
Raw Normal View History

# For instrumenting
require 'active_support/notifications'
# Using Thor's indifferent hash access
require 'thor'
# Core Pathname library used for traversal
require 'pathname'
# Template and Mime detection
require 'tilt'
require 'rack/mime'
2014-07-03 04:04:34 +02:00
# DbC
require 'middleman-core/contracts'
module Middleman
module Util
2014-07-03 04:04:34 +02:00
include Contracts
2014-07-03 04:04:34 +02:00
# A hash with indifferent access and magic predicates.
# Copied from Thor
#
# hash = Middleman::Util::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
#
# hash[:foo] #=> 'bar'
# hash['foo'] #=> 'bar'
# hash.foo? #=> true
#
class HashWithIndifferentAccess < ::Hash #:nodoc:
def initialize(hash={})
super()
hash.each do |key, value|
self[convert_key(key)] = value
end
end
2014-07-03 04:04:34 +02:00
def [](key)
super(convert_key(key))
end
2014-07-03 04:04:34 +02:00
def []=(key, value)
super(convert_key(key), value)
end
2014-07-03 04:04:34 +02:00
def delete(key)
super(convert_key(key))
end
2014-07-03 04:04:34 +02:00
def values_at(*indices)
indices.map { |key| self[convert_key(key)] }
end
2014-07-03 04:04:34 +02:00
def merge(other)
dup.merge!(other)
end
2014-07-03 04:04:34 +02:00
def merge!(other)
other.each do |key, value|
self[convert_key(key)] = value
end
2014-07-03 04:04:34 +02:00
self
end
2014-07-03 04:04:34 +02:00
# Convert to a Hash with String keys.
def to_hash
Hash.new(default).merge!(self)
end
2014-07-03 04:04:34 +02:00
protected
2014-07-03 04:04:34 +02:00
def convert_key(key)
key.is_a?(Symbol) ? key.to_s : key
end
2014-07-03 04:04:34 +02:00
# Magic predicates. For instance:
#
2014-07-03 04:04:34 +02:00
# options.force? # => !!options['force']
# options.shebang # => "/usr/lib/local/ruby"
# options.test_framework?(:rspec) # => options[:test_framework] == :rspec
# rubocop:disable DoubleNegation
def method_missing(method, *args)
method = method.to_s
if method =~ /^(\w+)\?$/
if args.empty?
!!self[$1]
else
2014-07-03 04:04:34 +02:00
self[$1] == args.first
end
else
2014-07-03 04:04:34 +02:00
self[method]
end
2014-07-03 04:04:34 +02:00
end
end
2014-07-03 04:04:34 +02:00
# Whether the source file is binary.
#
# @param [String] filename The file to check.
# @return [Boolean]
Contract String => Bool
def self.binary?(filename)
ext = File.extname(filename)
2014-07-03 04:04:34 +02:00
# We hardcode detecting of gzipped SVG files
return true if ext == '.svgz'
2014-07-03 04:04:34 +02:00
return false if Tilt.registered?(ext.sub('.', ''))
2014-07-03 04:04:34 +02:00
dot_ext = (ext.to_s[0] == '.') ? ext.dup : ".#{ext}"
2014-07-03 04:04:34 +02:00
if mime = ::Rack::Mime.mime_type(dot_ext, nil)
!nonbinary_mime?(mime)
else
file_contents_include_binary_bytes?(filename)
end
2014-07-03 04:04:34 +02:00
end
2014-07-03 04:04:34 +02:00
# Takes a matcher, which can be a literal string
# or a string containing glob expressions, or a
# regexp, or a proc, or anything else that responds
# to #match or #call, and returns whether or not the
# given path matches that matcher.
#
# @param [String, #match, #call] matcher A matcher String, RegExp, Proc, etc.
# @param [String] path A path as a string
# @return [Boolean] Whether the path matches the matcher
Contract Or[String, RespondTo[:match], RespondTo[:call], RespondTo[:to_s]], String => Bool
def self.path_match(matcher, path)
case
when matcher.is_a?(String)
if matcher.include? '*'
File.fnmatch(matcher, path)
else
2014-07-03 04:04:34 +02:00
path == matcher
end
2014-07-03 04:04:34 +02:00
when matcher.respond_to?(:match)
!matcher.match(path).nil?
when matcher.respond_to?(:call)
matcher.call(path)
else
File.fnmatch(matcher.to_s, path)
2013-06-19 20:13:23 +02:00
end
2014-07-03 04:04:34 +02:00
end
2013-06-19 20:13:23 +02:00
2014-07-03 04:04:34 +02:00
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Middleman::Util::HashWithIndifferentAccess]
Contract Or[Hash, Array] => Or[HashWithIndifferentAccess, Array]
def self.recursively_enhance(data)
if data.is_a? Hash
enhanced = ::Middleman::Util::HashWithIndifferentAccess.new(data)
enhanced.each do |key, val|
enhanced[key] = if val.is_a?(Hash) || val.is_a?(Array)
recursively_enhance(val)
2014-04-16 00:16:52 +02:00
else
2014-07-03 04:04:34 +02:00
val
2014-04-16 00:16:52 +02:00
end
end
2014-07-03 04:04:34 +02:00
enhanced
elsif data.is_a? Array
enhanced = data.dup
2014-07-03 04:04:34 +02:00
enhanced.each_with_index do |val, i|
enhanced[i] = if val.is_a?(Hash) || val.is_a?(Array)
recursively_enhance(val)
else
val
end
end
2014-07-03 04:04:34 +02:00
enhanced
2013-06-19 20:13:23 +02:00
end
2014-07-03 04:04:34 +02:00
end
2013-06-19 20:13:23 +02:00
2014-07-03 04:04:34 +02:00
# Normalize a path to not include a leading slash
# @param [String] path
# @return [String]
Contract String => String
def self.normalize_path(path)
# The tr call works around a bug in Ruby's Unicode handling
path.sub(%r{^/}, '').tr('', '')
end
2014-07-03 04:04:34 +02:00
# This is a separate method from normalize_path in case we
# change how we normalize paths
Contract String => String
def self.strip_leading_slash(path)
path.sub(%r{^/}, '')
end
2014-07-03 04:04:34 +02:00
# Facade for ActiveSupport/Notification
def self.instrument(name, payload={}, &block)
suffixed_name = (name =~ /\.middleman$/) ? name.dup : "#{name}.middleman"
::ActiveSupport::Notifications.instrument(suffixed_name, payload, &block)
end
2013-06-19 20:13:23 +02:00
2014-07-03 04:04:34 +02:00
# Extract the text of a Rack response as a string.
# Useful for extensions implemented as Rack middleware.
# @param response The response from #call
# @return [String] The whole response as a string.
2014-07-09 19:46:03 +02:00
Contract Or[ArrayOf[String], IsA['Rack::BodyProxy']] => String
2014-07-03 04:04:34 +02:00
def self.extract_response_text(response)
# The rack spec states all response bodies must respond to each
result = ''
response.each do |part, _|
result << part
2013-06-19 20:13:23 +02:00
end
2014-07-03 04:04:34 +02:00
result
2013-06-19 20:13:23 +02:00
end
2014-07-03 04:04:34 +02:00
# Get a recusive list of files inside a path.
# Works with symlinks.
2014-01-03 01:34:08 +01:00
#
2014-07-03 04:04:34 +02:00
# @param path Some path string or Pathname
# @param ignore A proc/block that returns true if a given path should be ignored - if a path
# is ignored, nothing below it will be searched either.
# @return [Array<Pathname>] An array of Pathnames for each file (no directories)
Contract Or[String, Pathname], Proc => ArrayOf[Pathname]
def self.all_files_under(path, &ignore)
path = Pathname(path)
return [] if ignore && ignore.call(path)
if path.directory?
path.children.flat_map do |child|
all_files_under(child, &ignore)
end.compact
elsif path.file?
[path]
else
[]
end
end
# Get the path of a file of a given type
2014-01-03 01:34:08 +01:00
#
2014-07-03 04:04:34 +02:00
# @param [Middleman::Application] app The app.
# @param [Symbol] kind The type of file
# @param [String, Symbol] source The path to the file
# @param [Hash] options Data to pass through.
# @return [String]
Contract IsA['Middleman::Application'], Symbol, Or[String, Symbol], Hash => String
def self.asset_path(app, kind, source, options={})
return source if source.to_s.include?('//') || source.to_s.start_with?('data:')
asset_folder = case kind
when :css
app.config[:css_dir]
when :js
app.config[:js_dir]
when :images
app.config[:images_dir]
when :fonts
app.config[:fonts_dir]
else
kind.to_s
end
source = source.to_s.tr(' ', '')
ignore_extension = (kind == :images || kind == :fonts) # don't append extension
source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
asset_folder = '' if source.start_with?('/') # absolute path
asset_url(app, source, asset_folder, options)
end
# Get the URL of an asset given a type/prefix
2014-01-03 01:34:08 +01:00
#
2014-07-03 04:04:34 +02:00
# @param [String] path The path (such as "photo.jpg")
# @param [String] prefix The type prefix (such as "images")
# @param [Hash] options Data to pass through.
# @return [String] The fully qualified asset url
Contract IsA['Middleman::Application'], String, String, Hash => String
def self.asset_url(app, path, prefix='', _options={})
# Don't touch assets which already have a full path
if path.include?('//') || path.start_with?('data:')
path
else # rewrite paths to use their destination path
if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
resource.url
else
path = File.join(prefix, path)
if resource = app.sitemap.find_resource_by_path(path)
resource.url
else
File.join(app.config[:http_prefix], path)
end
2014-01-03 01:34:08 +01:00
end
end
2014-07-03 04:04:34 +02:00
end
2014-01-03 01:34:08 +01:00
2014-07-03 04:04:34 +02:00
# Given a source path (referenced either absolutely or relatively)
# or a Resource, this will produce the nice URL configured for that
# path, respecting :relative_links, directory indexes, etc.
Contract IsA['Middleman::Application'], Or[String, IsA['Middleman::Sitemap::Resource']], Hash => String
def self.url_for(app, path_or_resource, options={})
# Handle Resources and other things which define their own url method
url = if path_or_resource.respond_to?(:url)
path_or_resource.url
else
path_or_resource.dup
end.gsub(' ', '%20')
# Try to parse URL
begin
uri = URI(url)
rescue URI::InvalidURIError
# Nothing we can do with it, it's not really a URI
return url
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
relative = options[:relative]
raise "Can't use the relative option with an external URL" if relative && uri.host
# Allow people to turn on relative paths for all links with
# set :relative_links, true
# but still override on a case by case basis with the :relative parameter.
effective_relative = relative || false
effective_relative = true if relative.nil? && app.config[:relative_links]
# Try to find a sitemap resource corresponding to the desired path
this_resource = options[:current_resource]
if path_or_resource.is_a?(::Middleman::Sitemap::Resource)
resource = path_or_resource
resource_url = url
elsif this_resource && uri.path
# Handle relative urls
url_path = Pathname(uri.path)
current_source_dir = Pathname('/' + this_resource.path).dirname
url_path = current_source_dir.join(url_path) if url_path.relative?
resource = app.sitemap.find_resource_by_path(url_path.to_s)
resource_url = resource.url if resource
elsif options[:find_resource] && uri.path
resource = app.sitemap.find_resource_by_path(uri.path)
resource_url = resource.url if resource
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
if resource
uri.path = if this_resource
relative_path_from_resource(this_resource, resource_url, effective_relative)
else
resource_url
end
else
# If they explicitly asked for relative links but we can't find a resource...
raise "No resource exists at #{url}" if relative
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
# Support a :query option that can be a string or hash
if query = options[:query]
uri.query = query.respond_to?(:to_param) ? query.to_param : query.to_s
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
# Support a :fragment or :anchor option just like Padrino
fragment = options[:anchor] || options[:fragment]
uri.fragment = fragment.to_s if fragment
# Finally make the URL back into a string
uri.to_s
end
# Expand a path to include the index file if it's a directory
#
# @param [String] path Request path/
# @param [Middleman::Application] app The requesting app.
# @return [String] Path with index file if necessary.
Contract String, IsA['Middleman::Application'] => String
def self.full_path(path, app)
resource = app.sitemap.find_resource_by_destination_path(path)
unless resource
# Try it with /index.html at the end
indexed_path = File.join(path.sub(%r{/$}, ''), app.config[:index_file])
resource = app.sitemap.find_resource_by_destination_path(indexed_path)
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
if resource
'/' + resource.destination_path
else
'/' + normalize_path(path)
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
end
2014-01-03 01:34:08 +01:00
2014-07-03 04:04:34 +02:00
Contract String, String, ArrayOf[String], Proc => String
def self.rewrite_paths(body, _path, exts, &_block)
body.dup.gsub(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{Regexp.union(exts)}))/) do |match|
opening_character = $1
asset_path = $2
if result = yield(asset_path)
"#{opening_character}#{result}"
else
match
end
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
end
2014-01-03 01:34:08 +01:00
2014-07-03 04:04:34 +02:00
# Is mime type known to be non-binary?
#
# @param [String] mime The mimetype to check.
# @return [Boolean]
Contract String => Bool
def self.nonbinary_mime?(mime)
case
when mime.start_with?('text/')
true
when mime.include?('xml')
true
when mime.include?('json')
true
when mime.include?('javascript')
true
else
false
end
end
2014-01-03 01:34:08 +01:00
2014-07-03 04:04:34 +02:00
# Read a few bytes from the file and see if they are binary.
#
# @param [String] filename The file to check.
# @return [Boolean]
Contract String => Bool
def self.file_contents_include_binary_bytes?(filename)
binary_bytes = [0, 1, 2, 3, 4, 5, 6, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31]
s = File.read(filename, 4096) || ''
s.each_byte do |c|
return true if binary_bytes.include?(c)
2014-04-29 19:50:21 +02:00
end
2014-01-03 01:34:08 +01:00
2014-07-03 04:04:34 +02:00
false
end
# Get a relative path to a resource.
#
# @param [Middleman::Sitemap::Resource] curr_resource The resource.
# @param [String] resource_url The target url.
# @param [Boolean] relative If the path should be relative.
# @return [String]
Contract IsA['Middleman::Sitemap::Resource'], String, Bool => String
def self.relative_path_from_resource(curr_resource, resource_url, relative)
# Switch to the relative path between resource and the given resource
# if we've been asked to.
if relative
# Output urls relative to the destination path, not the source path
current_dir = Pathname('/' + curr_resource.destination_path).dirname
relative_path = Pathname(resource_url).relative_path_from(current_dir).to_s
# Put back the trailing slash to avoid unnecessary Apache redirects
if resource_url.end_with?('/') && !relative_path.end_with?('/')
relative_path << '/'
2014-01-03 01:34:08 +01:00
end
2014-07-03 04:04:34 +02:00
relative_path
else
resource_url
2014-04-29 19:50:21 +02:00
end
2014-01-03 01:34:08 +01:00
end
end
2014-01-03 01:34:08 +01:00
end