break apart some methods to make code less complex
This commit is contained in:
parent
fe2a7c4dd6
commit
10f1d7ada2
5 changed files with 158 additions and 121 deletions
|
@ -174,11 +174,9 @@ module Middleman::CoreExtensions
|
|||
end
|
||||
|
||||
data = {}
|
||||
content = nil
|
||||
|
||||
return [data, content] unless app.files.exists?(full_path)
|
||||
return [data, nil] if !app.files.exists?(full_path) || ::Middleman::Util.binary?(full_path)
|
||||
|
||||
if !::Middleman::Util.binary?(full_path)
|
||||
content = File.read(full_path)
|
||||
|
||||
begin
|
||||
|
@ -188,15 +186,11 @@ module Middleman::CoreExtensions
|
|||
content = lines.join("\n")
|
||||
end
|
||||
|
||||
if result = parse_yaml_front_matter(content)
|
||||
data, content = result
|
||||
elsif result = parse_json_front_matter(content)
|
||||
data, content = result
|
||||
end
|
||||
result = parse_yaml_front_matter(content) || parse_json_front_matter(content)
|
||||
return result if result
|
||||
rescue
|
||||
# Probably a binary file, move on
|
||||
end
|
||||
end
|
||||
|
||||
[data, content]
|
||||
end
|
||||
|
|
|
@ -136,53 +136,17 @@ module Middleman
|
|||
attr_accessor :options
|
||||
attr_reader :app
|
||||
|
||||
def initialize(klass, options_hash={})
|
||||
def initialize(klass, options_hash={}, &block)
|
||||
@_helpers = []
|
||||
@klass = klass
|
||||
|
||||
@options = self.class.config.dup
|
||||
@options.finalize!
|
||||
setup_options(options_hash, &block)
|
||||
setup_app_reference_when_available
|
||||
|
||||
options_hash.each do |k, v|
|
||||
@options[k] = v
|
||||
end
|
||||
|
||||
yield @options if block_given?
|
||||
|
||||
ext = self
|
||||
|
||||
klass.initialized do
|
||||
ext.app = self
|
||||
end
|
||||
|
||||
if ext.respond_to?(:before_configuration)
|
||||
klass.before_configuration do
|
||||
ext.before_configuration
|
||||
end
|
||||
end
|
||||
|
||||
klass.instance_available do
|
||||
ext.app ||= self
|
||||
end
|
||||
|
||||
klass.after_configuration do
|
||||
if ext.respond_to?(:after_configuration)
|
||||
ext.after_configuration
|
||||
end
|
||||
|
||||
if ext.respond_to?(:manipulate_resource_list)
|
||||
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
|
||||
end
|
||||
end
|
||||
|
||||
if ext.respond_to?(:after_build)
|
||||
klass.after_build do |builder|
|
||||
if ext.method(:after_build).arity === 1
|
||||
ext.after_build(builder)
|
||||
else
|
||||
ext.after_build
|
||||
end
|
||||
end
|
||||
end
|
||||
# Bind app hooks to local methods
|
||||
bind_before_configuration
|
||||
bind_after_configuration
|
||||
bind_after_build
|
||||
end
|
||||
|
||||
def app=(app)
|
||||
|
@ -192,5 +156,65 @@ module Middleman
|
|||
app.class.send(:include, m)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def setup_options(options_hash, &block)
|
||||
@options = self.class.config.dup
|
||||
@options.finalize!
|
||||
|
||||
options_hash.each do |k, v|
|
||||
@options[k] = v
|
||||
end
|
||||
|
||||
yield @options if block_given?
|
||||
end
|
||||
|
||||
def setup_app_reference_when_available
|
||||
ext = self
|
||||
|
||||
@klass.initialized do
|
||||
ext.app = self
|
||||
end
|
||||
|
||||
@klass.instance_available do
|
||||
ext.app ||= self
|
||||
end
|
||||
end
|
||||
|
||||
def bind_before_configuration
|
||||
ext = self
|
||||
if ext.respond_to?(:before_configuration)
|
||||
@klass.before_configuration do
|
||||
ext.before_configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def bind_after_configuration
|
||||
ext = self
|
||||
@klass.after_configuration do
|
||||
if ext.respond_to?(:after_configuration)
|
||||
ext.after_configuration
|
||||
end
|
||||
|
||||
if ext.respond_to?(:manipulate_resource_list)
|
||||
ext.app.sitemap.register_resource_list_manipulator(ext.class.extension_name, ext)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def bind_after_build
|
||||
ext = self
|
||||
if ext.respond_to?(:after_build)
|
||||
@klass.after_build do |builder|
|
||||
if ext.method(:after_build).arity === 1
|
||||
ext.after_build(builder)
|
||||
else
|
||||
ext.after_build
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -173,11 +173,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
|
|||
|
||||
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
|
||||
|
||||
if @mount_at_root == lang
|
||||
prefix = "/"
|
||||
prefix = if @mount_at_root == lang
|
||||
"/"
|
||||
else
|
||||
replacement = options[:lang_map].fetch(lang, lang)
|
||||
prefix = options[:path].sub(":locale", replacement.to_s)
|
||||
options[:path].sub(":locale", replacement.to_s)
|
||||
end
|
||||
|
||||
path = ::Middleman::Util.normalize_path(
|
||||
|
|
|
@ -20,10 +20,11 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
# Update the main sitemap resource list
|
||||
# @return [void]
|
||||
def manipulate_resource_list(resources)
|
||||
@rack_client ||= ::Rack::Test::Session.new(app.class.to_rack_app)
|
||||
|
||||
# Process resources in order: binary images and fonts, then SVG, then JS/CSS.
|
||||
# This is so by the time we get around to the text files (which may reference
|
||||
# images and fonts) the static assets' hashes are already calculated.
|
||||
rack_client = ::Rack::Test::Session.new(app.class.to_rack_app)
|
||||
resources.sort_by do |a|
|
||||
if %w(.svg).include? a.ext
|
||||
0
|
||||
|
@ -32,18 +33,24 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
else
|
||||
-1
|
||||
end
|
||||
end.each do |resource|
|
||||
next unless options.exts.include? resource.ext
|
||||
next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
|
||||
end.each(&method(:manipulate_single_resource))
|
||||
end
|
||||
|
||||
def manipulate_single_resource(resource)
|
||||
return unless options.exts.include?(resource.ext)
|
||||
return if ignored_resource?(resource)
|
||||
|
||||
# Render through the Rack interface so middleware and mounted apps get a shot
|
||||
response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" })
|
||||
response = @rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" })
|
||||
raise "#{resource.path} should be in the sitemap!" unless response.status == 200
|
||||
|
||||
digest = Digest::SHA1.hexdigest(response.body)[0..7]
|
||||
|
||||
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
|
||||
end
|
||||
|
||||
def ignored_resource?(resource)
|
||||
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
|
||||
end
|
||||
|
||||
# The asset hash middleware is responsible for rewriting references to
|
||||
|
@ -64,14 +71,24 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
return [status, headers, response] if env["bypass_asset_hash"] == 'true'
|
||||
|
||||
path = @middleman_app.full_path(env["PATH_INFO"])
|
||||
dirpath = Pathname.new(File.dirname(path))
|
||||
|
||||
if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/
|
||||
body = ::Middleman::Util.extract_response_text(response)
|
||||
|
||||
if body
|
||||
status, headers, response = Rack::Response.new(rewrite_paths(body, path), status, headers).finish
|
||||
end
|
||||
end
|
||||
|
||||
[status, headers, response]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def rewrite_paths(body, path)
|
||||
dirpath = Pathname.new(File.dirname(path))
|
||||
|
||||
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
|
||||
body.gsub!(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match|
|
||||
body.gsub(/([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match|
|
||||
opening_character = $1
|
||||
asset_path = $2
|
||||
|
||||
|
@ -90,12 +107,8 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
|||
match
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
status, headers, response = Rack::Response.new(body, status, headers).finish
|
||||
end
|
||||
end
|
||||
[status, headers, response]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -44,10 +44,31 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
|
|||
path = env["PATH_INFO"]
|
||||
|
||||
begin
|
||||
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
||||
if @inline && (path.end_with?('.html') || path.end_with?('.php'))
|
||||
uncompressed_source = ::Middleman::Util.extract_response_text(response)
|
||||
|
||||
minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
|
||||
minified = minify_inline_content(uncompressed_source)
|
||||
|
||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
||||
response = [minified]
|
||||
elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
|
||||
uncompressed_source = ::Middleman::Util.extract_response_text(response)
|
||||
minified = @compressor.compress(uncompressed_source)
|
||||
|
||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
||||
response = [minified]
|
||||
end
|
||||
rescue ExecJS::ProgramError => e
|
||||
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
|
||||
end
|
||||
|
||||
[status, headers, response]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def minify_inline_content(uncompressed_source)
|
||||
uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
|
||||
first = $1
|
||||
javascript = $2
|
||||
last = $3
|
||||
|
@ -63,21 +84,6 @@ class Middleman::Extensions::MinifyJavascript < ::Middleman::Extension
|
|||
match
|
||||
end
|
||||
end
|
||||
|
||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
||||
response = [minified]
|
||||
elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
|
||||
uncompressed_source = ::Middleman::Util.extract_response_text(response)
|
||||
minified_js = @compressor.compress(uncompressed_source)
|
||||
|
||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_js).to_s
|
||||
response = [minified_js]
|
||||
end
|
||||
rescue ExecJS::ProgramError => e
|
||||
warn "WARNING: Couldn't compress JavaScript in #{path}: #{e.message}"
|
||||
end
|
||||
|
||||
[status, headers, response]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue