break apart some methods to make code less complex

This commit is contained in:
Thomas Reynolds 2013-05-31 20:46:12 -04:00
parent fe2a7c4dd6
commit 10f1d7ada2
5 changed files with 158 additions and 121 deletions

View file

@ -174,28 +174,22 @@ 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
if content =~ /\A.*coding:/
lines = content.split(/\n/)
lines.shift
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
rescue
# Probably a binary file, move on
content = File.read(full_path)
begin
if content =~ /\A.*coding:/
lines = content.split(/\n/)
lines.shift
content = lines.join("\n")
end
result = parse_yaml_front_matter(content) || parse_json_front_matter(content)
return result if result
rescue
# Probably a binary file, move on
end
[data, content]

View file

@ -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