Minor perf tweaks

This commit is contained in:
Thomas Reynolds 2016-04-21 16:06:26 -07:00
parent a14934e08b
commit 0f2bc1e0ea
19 changed files with 130 additions and 68 deletions

View file

@ -23,7 +23,7 @@ module Middleman
end end
def include(mod) def include(mod)
self.extend(mod) extend(mod)
end end
def helpers(*helper_modules, &block) def helpers(*helper_modules, &block)

View file

@ -73,18 +73,20 @@ module Middleman::CoreExtensions
return [{}, nil] unless file return [{}, nil] unless file
return @cache[file[:full_path]] if @cache.key?(file[:full_path]) file_path = file[:full_path].to_s
@cache[file[:full_path]] = ::Middleman::Util::Data.parse( @cache[file_path] ||= begin
file, ::Middleman::Util::Data.parse(
app.config[:frontmatter_delims] file,
) app.config[:frontmatter_delims]
)
end
end end
Contract ArrayOf[IsA['Middleman::SourceFile']], ArrayOf[IsA['Middleman::SourceFile']] => Any Contract ArrayOf[IsA['Middleman::SourceFile']], ArrayOf[IsA['Middleman::SourceFile']] => Any
def clear_data(updated_files, removed_files) def clear_data(updated_files, removed_files)
(updated_files + removed_files).each do |file| (updated_files + removed_files).each do |file|
@cache.delete(file[:full_path]) @cache.delete(file[:full_path].to_s)
end end
end end
end end

View file

@ -1,6 +1,6 @@
require 'rack' require 'rack'
require 'rack/response' require 'rack/response'
require 'addressable/uri' require 'memoist'
require 'middleman-core/util' require 'middleman-core/util'
require 'middleman-core/contracts' require 'middleman-core/contracts'
@ -33,6 +33,8 @@ module Middleman
end end
def after_configuration def after_configuration
return if @rewriters.empty?
rewriters = @rewriters.values.sort do |a, b| rewriters = @rewriters.values.sort do |a, b|
if b[:after] && b[:after] == a[:id] if b[:after] && b[:after] == a[:id]
1 1
@ -45,6 +47,7 @@ module Middleman
end end
class Rack class Rack
extend Memoist
include Contracts include Contracts
Contract RespondTo[:call], { Contract RespondTo[:call], {
@ -55,6 +58,17 @@ module Middleman
@rack_app = app @rack_app = app
@middleman_app = options.fetch(:middleman_app) @middleman_app = options.fetch(:middleman_app)
@rewriters = options.fetch(:rewriters) @rewriters = options.fetch(:rewriters)
all_source_exts = @rewriters
.reduce([]) { |sum, rewriter| sum + rewriter[:source_extensions] }
.flatten
.uniq
@source_exts_regex_text = Regexp.union(all_source_exts).to_s
@all_asset_exts = @rewriters
.reduce([]) { |sum, rewriter| sum + rewriter[:url_extensions] }
.flatten
.uniq
end end
def call(env) def call(env)
@ -63,27 +77,16 @@ module Middleman
# Allow configuration or upstream request to skip all rewriting # Allow configuration or upstream request to skip all rewriting
return [status, headers, response] if env['bypass_inline_url_rewriter'] == 'true' return [status, headers, response] if env['bypass_inline_url_rewriter'] == 'true'
all_source_exts = @rewriters
.reduce([]) { |sum, rewriter| sum + rewriter[:source_extensions] }
.flatten
.uniq
source_exts_regex_text = Regexp.union(all_source_exts).to_s
all_asset_exts = @rewriters
.reduce([]) { |sum, rewriter| sum + rewriter[:url_extensions] }
.flatten
.uniq
path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app) path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app)
return [status, headers, response] unless path =~ /(^\/$)|(#{source_exts_regex_text}$)/ return [status, headers, response] unless path =~ /(^\/$)|(#{@source_exts_regex_text}$)/
return [status, headers, response] unless body = ::Middleman::Util.extract_response_text(response) return [status, headers, response] unless body = ::Middleman::Util.extract_response_text(response)
dirpath = ::Pathname.new(File.dirname(path)) dirpath = ::Pathname.new(File.dirname(path))
rewritten = ::Middleman::Util.instrument 'inline_url_rewriter', path: path do rewritten = ::Middleman::Util.instrument 'inline_url_rewriter', path: path do
::Middleman::Util.rewrite_paths(body, path, all_asset_exts, @middleman_app) do |asset_path| ::Middleman::Util.rewrite_paths(body, path, @all_asset_exts, @middleman_app) do |asset_path|
uri = ::Addressable::URI.parse(asset_path) uri = ::Middleman::Util.parse_uri(asset_path)
relative_path = uri.host.nil? relative_path = uri.host.nil?
@ -144,6 +147,7 @@ module Middleman
false false
end end
end end
memoize :should_ignore?
end end
end end
end end

View file

@ -1,4 +1,5 @@
require 'forwardable' require 'forwardable'
require 'memoist'
require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/class/attribute'
require 'middleman-core/configuration' require 'middleman-core/configuration'
require 'middleman-core/contracts' require 'middleman-core/contracts'
@ -66,6 +67,8 @@ module Middleman
# @see http://middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation # @see http://middlemanapp.com/advanced/custom/ Middleman Custom Extensions Documentation
class Extension class Extension
extend Forwardable extend Forwardable
extend Memoist
include Contracts include Contracts
def_delegator :@app, :logger def_delegator :@app, :logger
@ -510,7 +513,7 @@ module Middleman
self.class.exposed_to_config.each do |k, v| self.class.exposed_to_config.each do |k, v|
::Middleman::CoreExtensions::Collections::StepContext.add_to_context(k) do |*args, &b| ::Middleman::CoreExtensions::Collections::StepContext.add_to_context(k) do |*args, &b|
r = context.method(:"__original_#{v}").call(*args, &b) r = context.method(:"__original_#{v}").call(*args, &b)
self.descriptors << r if r.respond_to?(:execute_descriptor) descriptors << r if r.respond_to?(:execute_descriptor)
end end
end end
end end

View file

@ -32,7 +32,7 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
Contract String, Or[String, Pathname], Any => Maybe[String] Contract String, Or[String, Pathname], Any => Maybe[String]
def rewrite_url(asset_path, dirpath, _request_path) def rewrite_url(asset_path, dirpath, _request_path)
uri = ::Addressable::URI.parse(asset_path) uri = ::Middleman::Util.parse_uri(asset_path)
relative_path = !uri.path.start_with?('/') relative_path = !uri.path.start_with?('/')
full_asset_path = if relative_path full_asset_path = if relative_path

View file

@ -20,7 +20,7 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension
Contract String, Or[String, Pathname], Any => String Contract String, Or[String, Pathname], Any => String
def rewrite_url(asset_path, dirpath, _request_path) def rewrite_url(asset_path, dirpath, _request_path)
uri = ::Addressable::URI.parse(asset_path) uri = ::Middleman::Util.parse_uri(asset_path)
relative_path = uri.path[0..0] != '/' relative_path = uri.path[0..0] != '/'
full_asset_path = if relative_path full_asset_path = if relative_path
@ -37,4 +37,5 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension
File.join(asset_prefix, full_asset_path) File.join(asset_prefix, full_asset_path)
end end
memoize :rewrite_url
end end

View file

@ -1,3 +1,4 @@
require 'memoist'
require 'middleman-core/contracts' require 'middleman-core/contracts'
# Minify CSS Extension # Minify CSS Extension
@ -30,6 +31,7 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
# Rack middleware to look for CSS and compress it # Rack middleware to look for CSS and compress it
class Rack class Rack
extend Memoist
include Contracts include Contracts
INLINE_CSS_REGEX = /(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m INLINE_CSS_REGEX = /(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m
@ -82,8 +84,9 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def ignore?(path) def ignore?(path)
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) } @ignore.any? { |ignore| ::Middleman::Util.path_match(ignore, path) }
end end
memoize :ignore?
# Whether this type of content can be minified # Whether this type of content can be minified
# @param [String, nil] content_type # @param [String, nil] content_type
@ -91,6 +94,7 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
def minifiable?(content_type) def minifiable?(content_type)
@content_types.include?(content_type) @content_types.include?(content_type)
end end
memoize :minifiable?
# Whether this type of content contains inline content that can be minified # Whether this type of content contains inline content that can be minified
# @param [String, nil] content_type # @param [String, nil] content_type
@ -98,6 +102,7 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
def minifiable_inline?(content_type) def minifiable_inline?(content_type)
@inline_content_types.include?(content_type) @inline_content_types.include?(content_type)
end end
memoize :minifiable_inline?
# Minify the content # Minify the content
# @param [String] content # @param [String] content
@ -105,6 +110,7 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
def minify(content) def minify(content)
@compressor.compress(content) @compressor.compress(content)
end end
memoize :minify
# Detect and minify inline content # Detect and minify inline content
# @param [String] content # @param [String] content
@ -114,5 +120,6 @@ class Middleman::Extensions::MinifyCss < ::Middleman::Extension
$1 + minify($2) + $3 $1 + minify($2) + $3
end end
end end
memoize :minify_inline
end end
end end

View file

@ -1,4 +1,5 @@
require 'middleman-core/contracts' require 'middleman-core/contracts'
require 'memoist'
# Minify Javascript Extension # Minify Javascript Extension
class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
@ -22,6 +23,7 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
# Rack middleware to look for JS and compress it # Rack middleware to look for JS and compress it
class Rack class Rack
extend Memoist
include Contracts include Contracts
INLINE_JS_REGEX = /(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m INLINE_JS_REGEX = /(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m
@ -76,6 +78,7 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
def ignore?(path) def ignore?(path)
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) } @ignore.any? { |ignore| Middleman::Util.path_match(ignore, path) }
end end
memoize :ignore?
# Whether this type of content can be minified # Whether this type of content can be minified
# @param [String, nil] content_type # @param [String, nil] content_type
@ -83,6 +86,7 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
def minifiable?(content_type) def minifiable?(content_type)
@content_types.include?(content_type) @content_types.include?(content_type)
end end
memoize :minifiable?
# Whether this type of content contains inline content that can be minified # Whether this type of content contains inline content that can be minified
# @param [String, nil] content_type # @param [String, nil] content_type
@ -90,6 +94,7 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
def minifiable_inline?(content_type) def minifiable_inline?(content_type)
@inline_content_types.include?(content_type) @inline_content_types.include?(content_type)
end end
memoize :minifiable_inline?
# Minify the content # Minify the content
# @param [String] content # @param [String] content
@ -100,6 +105,7 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
warn "WARNING: Couldn't compress JavaScript in #{@path}: #{e.message}" warn "WARNING: Couldn't compress JavaScript in #{@path}: #{e.message}"
content content
end end
memoize :minify
# Detect and minify inline content # Detect and minify inline content
# @param [String] content # @param [String] content
@ -119,5 +125,6 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
end end
end end
end end
memoize :minify_inline
end end
end end

View file

@ -33,7 +33,7 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
Contract String, Or[String, Pathname], Any => Maybe[String] Contract String, Or[String, Pathname], Any => Maybe[String]
def rewrite_url(asset_path, dirpath, request_path) def rewrite_url(asset_path, dirpath, request_path)
uri = ::Addressable::URI.parse(asset_path) uri = ::Middleman::Util.parse_uri(asset_path)
return if uri.path[0..0] != '/' return if uri.path[0..0] != '/'
@ -50,4 +50,5 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
result result
end end
memoize :rewrite_url
end end

View file

@ -59,7 +59,7 @@ module Middleman
# Overwrite with frontmatter options # Overwrite with frontmatter options
options = options.deep_merge(options[:renderer_options]) if options[:renderer_options] options = options.deep_merge(options[:renderer_options]) if options[:renderer_options]
template_class = ::Tilt[path] template_class = ::Middleman::Util.tilt_class(path)
# Allow hooks to manipulate the template before render # Allow hooks to manipulate the template before render
body = @app.callbacks_for(:before_render).reduce(body) do |sum, callback| body = @app.callbacks_for(:before_render).reduce(body) do |sum, callback|
@ -99,12 +99,12 @@ module Middleman
def template_data_for_file def template_data_for_file
file = @app.files.find(:source, @path) file = @app.files.find(:source, @path)
if @app.extensions[:front_matter] || (file && !file[:types].include?(:no_frontmatter)) if @app.extensions[:front_matter] && (file && !file[:types].include?(:no_frontmatter))
result = @app.extensions[:front_matter].template_data_for_file(@path) result = @app.extensions[:front_matter].template_data_for_file(@path)
return result unless result.nil? return result unless result.nil?
end end
file ? file.read : File.read(@path) file ? file.read : ::File.read(@path)
end end
protected protected
@ -122,7 +122,7 @@ module Middleman
# Find all the engines which handle this extension in tilt. Look for # Find all the engines which handle this extension in tilt. Look for
# config variables of that name and merge it # config variables of that name and merge it
extension_class = ::Tilt[ext] extension_class = ::Middleman::Util.tilt_class(ext)
::Tilt.mappings.each do |mapping_ext, engines| ::Tilt.mappings.each do |mapping_ext, engines|
next unless engines.include? extension_class next unless engines.include? extension_class
engine_options = @app.config[mapping_ext.to_sym] || {} engine_options = @app.config[mapping_ext.to_sym] || {}

View file

@ -75,7 +75,7 @@ module Middleman
Contract Bool Contract Bool
def template? def template?
return false if file_descriptor.nil? return false if file_descriptor.nil?
!::Tilt[file_descriptor[:full_path].to_s].nil? !::Middleman::Util.tilt_class(file_descriptor[:full_path].to_s).nil?
end end
# Backwards compatible method for turning descriptor into a string. # Backwards compatible method for turning descriptor into a string.

View file

@ -298,9 +298,15 @@ module Middleman
relative_path = path.relative_path_from(directory) relative_path = path.relative_path_from(directory)
relative_path = File.join(destination_dir, relative_path) if destination_dir relative_path = File.join(destination_dir, relative_path) if destination_dir
types << :no_frontmatter if partial?(relative_path.to_s)
::Middleman::SourceFile.new(Pathname(relative_path), path, directory, types, 0) ::Middleman::SourceFile.new(Pathname(relative_path), path, directory, types, 0)
end end
def partial?(relative_path)
relative_path.split(::File::SEPARATOR).any? { |p| p.start_with?('_') }
end
Contract IsA['Middleman::SourceFile'] => Any Contract IsA['Middleman::SourceFile'] => Any
def record_file_change(f) def record_file_change(f)
if @files[f[:full_path]] if @files[f[:full_path]]

View file

@ -84,6 +84,7 @@ module Middleman
# Reset stored buffer, regardless of success # Reset stored buffer, regardless of success
restore_buffer(buf_was) restore_buffer(buf_was)
end end
# Render the layout, with the contents of the block inside. # Render the layout, with the contents of the block inside.
concat_safe_content render_file(layout_file, @locs, @opts) { content } concat_safe_content render_file(layout_file, @locs, @opts) { content }
ensure ensure
@ -185,7 +186,7 @@ module Middleman
# handles cases like `style.css.sass.erb` # handles cases like `style.css.sass.erb`
content = nil content = nil
while ::Tilt[path] while ::Middleman::Util.tilt_class(path)
begin begin
opts[:template_body] = content if content opts[:template_body] = content if content

View file

@ -61,7 +61,7 @@ module Middleman
# If we're specifically looking for a preferred engine # If we're specifically looking for a preferred engine
if options.key?(:preferred_engine) if options.key?(:preferred_engine)
extension_class = ::Tilt[options[:preferred_engine]] extension_class = ::Middleman::Util.tilt_class(options[:preferred_engine])
# Get a list of extensions for a preferred engine # Get a list of extensions for a preferred engine
preferred_engines += ::Tilt.mappings.select do |_, engines| preferred_engines += ::Tilt.mappings.select do |_, engines|
@ -89,7 +89,7 @@ module Middleman
app.files.find(:source, path_with_ext, globbing) app.files.find(:source, path_with_ext, globbing)
end end
found_template = file if file && (preferred_engine.nil? || ::Tilt[file[:full_path]]) found_template = file if file && (preferred_engine.nil? || ::Middleman::Util.tilt_class(file[:full_path].to_s))
break if found_template break if found_template
end end
@ -134,9 +134,9 @@ module Middleman
@app.extensions.add_exposed_to_context(context) @app.extensions.add_exposed_to_context(context)
locals.each do |k, _| locals.each do |k, _|
next unless context.respond_to?(k) && k != :current_path next unless context.respond_to?(k) && ![:current_path, :paginate, :page_articles, :blog_controller, :lang, :locale].include?(k.to_sym)
msg = "Template local `#{k}` tried to overwrite an existing context value. Please renamed the key when passing to `locals`" msg = "Template local `#{k}` tried to overwrite an existing context value. Please rename the key when passing to `locals`"
if @app.build? if @app.build?
throw msg throw msg
@ -152,13 +152,14 @@ module Middleman
# If we need a layout and have a layout, use it # If we need a layout and have a layout, use it
layout_file = fetch_layout(engine, options) layout_file = fetch_layout(engine, options)
if layout_file if layout_file
content = ::Middleman::Util.instrument 'builder.output.resource.render-layout', path: File.basename(layout_file[:relative_path].to_s) do content = if layout_file = fetch_layout(engine, options)
if layout_file = fetch_layout(engine, options) layout_renderer = ::Middleman::FileRenderer.new(@app, layout_file[:relative_path].to_s)
layout_renderer = ::Middleman::FileRenderer.new(@app, layout_file[:relative_path].to_s)
::Middleman::Util.instrument 'builder.output.resource.render-layout', path: File.basename(layout_file[:relative_path].to_s) do
layout_renderer.render(locals, options, context) { content } layout_renderer.render(locals, options, context) { content }
else
content
end end
else
content
end end
end end
@ -177,7 +178,7 @@ module Middleman
# handles cases like `style.css.sass.erb` # handles cases like `style.css.sass.erb`
content = nil content = nil
while ::Tilt[path] while ::Middleman::Util.tilt_class(path)
begin begin
opts[:template_body] = content if content opts[:template_body] = content if content

View file

@ -3,6 +3,7 @@ require 'json'
require 'pathname' require 'pathname'
require 'backports/2.1.0/array/to_h' require 'backports/2.1.0/array/to_h'
require 'hashie' require 'hashie'
require 'memoist'
require 'middleman-core/util/binary' require 'middleman-core/util/binary'
require 'middleman-core/contracts' require 'middleman-core/contracts'
@ -36,6 +37,7 @@ module Middleman
end end
module Data module Data
extend Memoist
include Contracts include Contracts
module_function module_function
@ -55,20 +57,7 @@ module Middleman
return [{}, nil] return [{}, nil]
end end
start_delims, stop_delims = frontmatter_delims match = build_regex(frontmatter_delims).match(content) || {}
.values
.flatten(1)
.transpose
.map(&::Regexp.method(:union))
match = /
\A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
(?<start>#{start_delims})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
^(?<stop>#{stop_delims})[ ]*\r?\n?
\r?\n?
(?<additional_content>.*)
/mx.match(content) || {}
unless match[:frontmatter] unless match[:frontmatter]
case known_type case known_type
@ -98,27 +87,53 @@ module Middleman
end end
end end
def build_regex(frontmatter_delims)
start_delims, stop_delims = frontmatter_delims
.values
.flatten(1)
.transpose
.map(&::Regexp.method(:union))
match = /
\A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
(?<start>#{start_delims})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
^(?<stop>#{stop_delims})[ ]*\r?\n?
\r?\n?
(?<additional_content>.*)
/mx
end
memoize :build_regex
# Parse YAML frontmatter out of a string # Parse YAML frontmatter out of a string
# @param [String] content # @param [String] content
# @return [Hash] # @return [Hash]
Contract String, Pathname, Bool => Hash Contract String, Pathname => Hash
def parse_yaml(content, full_path) def parse_yaml(content, full_path)
symbolize_recursive(::YAML.load(content) || {}) c = ::Middleman::Util.instrument 'parse.yaml' do
::YAML.load(content)
end
c ? symbolize_recursive(c) : {}
rescue StandardError, ::Psych::SyntaxError => error rescue StandardError, ::Psych::SyntaxError => error
warn "YAML Exception parsing #{full_path}: #{error.message}" warn "YAML Exception parsing #{full_path}: #{error.message}"
{} {}
end end
memoize :parse_yaml
# Parse JSON frontmatter out of a string # Parse JSON frontmatter out of a string
# @param [String] content # @param [String] content
# @return [Hash] # @return [Hash]
Contract String, Pathname => Hash Contract String, Pathname => Hash
def parse_json(content, full_path) def parse_json(content, full_path)
symbolize_recursive(::JSON.parse(content) || {}) c = ::Middleman::Util.instrument 'parse.json' do
::JSON.parse(content)
end
c ? symbolize_recursive(c) : {}
rescue StandardError => error rescue StandardError => error
warn "JSON Exception parsing #{full_path}: #{error.message}" warn "JSON Exception parsing #{full_path}: #{error.message}"
{} {}
end end
memoize :parse_json
def symbolize_recursive(value) def symbolize_recursive(value)
case value case value

View file

@ -54,15 +54,9 @@ module Middleman
result.encode('UTF-8', 'UTF-8-MAC') result.encode('UTF-8', 'UTF-8-MAC')
end end
Contract String => Bool
def tilt_recognizes?(path)
@@tilt_lookup_cache ||= {}
@@tilt_lookup_cache[path] ||= ::Tilt[path]
end
Contract String => String Contract String => String
def step_through_extensions(path) def step_through_extensions(path)
while tilt_recognizes?(path) while ::Middleman::Util.tilt_class(path)
ext = ::File.extname(path) ext = ::File.extname(path)
yield ext if block_given? yield ext if block_given?

View file

@ -1,16 +1,32 @@
# Core Pathname library used for traversal # Core Pathname library used for traversal
require 'pathname' require 'pathname'
require 'uri' require 'uri'
require 'memoist'
require 'addressable'
require 'tilt'
require 'middleman-core/contracts' require 'middleman-core/contracts'
# rubocop:disable ModuleLength # rubocop:disable ModuleLength
module Middleman module Middleman
module Util module Util
extend Memoist
include Contracts include Contracts
module_function module_function
Contract String => ::Addressable::URI
def parse_uri(uri)
::Addressable::URI.parse(uri)
end
memoize :parse_uri
Contract String => Any
def tilt_class(path)
::Tilt[path]
end
memoize :tilt_class
# Normalize a path to not include a leading slash # Normalize a path to not include a leading slash
# @param [String] path # @param [String] path
# @return [String] # @return [String]
@ -19,6 +35,7 @@ module Middleman
# The tr call works around a bug in Ruby's Unicode handling # The tr call works around a bug in Ruby's Unicode handling
::URI.decode(path).sub(%r{^/}, '').tr('', '') ::URI.decode(path).sub(%r{^/}, '').tr('', '')
end end
memoize :normalize_path
# This is a separate method from normalize_path in case we # This is a separate method from normalize_path in case we
# change how we normalize paths # change how we normalize paths
@ -26,6 +43,7 @@ module Middleman
def strip_leading_slash(path) def strip_leading_slash(path)
path.sub(%r{^/}, '') path.sub(%r{^/}, '')
end end
memoize :strip_leading_slash
# Get the path of a file of a given type # Get the path of a file of a given type
# #
@ -256,5 +274,6 @@ module Middleman
::File.fnmatch(matcher.to_s, path) ::File.fnmatch(matcher.to_s, path)
end end
end end
memoize :path_match
end end
end end

View file

@ -38,7 +38,7 @@ module Middleman
current_resource = app.sitemap.find_resource_by_destination_path(path) current_resource = app.sitemap.find_resource_by_destination_path(path)
begin begin
uri = ::Addressable::URI.parse(asset_path) uri = ::Middleman::Util.parse_uri(asset_path)
if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/) if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/)
dest_path = ::Middleman::Util.url_for(app, asset_path, relative: false, current_resource: current_resource) dest_path = ::Middleman::Util.url_for(app, asset_path, relative: false, current_resource: current_resource)

View file

@ -31,6 +31,7 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', ['~> 4.2']) s.add_dependency('activesupport', ['~> 4.2'])
s.add_dependency('padrino-helpers', ['~> 0.13.0']) s.add_dependency('padrino-helpers', ['~> 0.13.0'])
s.add_dependency("addressable", ["~> 2.3"]) s.add_dependency("addressable", ["~> 2.3"])
s.add_dependency('memoist', ['~> 0.14'])
# Watcher # Watcher
s.add_dependency('listen', ['~> 3.0']) s.add_dependency('listen', ['~> 3.0'])