Start gutting the provides_metadata methods and move some of frontmatter over
This commit is contained in:
parent
c59cefdafc
commit
69396d34c1
|
@ -29,29 +29,36 @@ module Middleman::CoreExtensions
|
|||
app.files.deleted { |file| ext.clear_data(file) }
|
||||
end
|
||||
|
||||
# Modify each resource to add data & options from frontmatter.
|
||||
def manipulate_resource_list(resources)
|
||||
resources.each do |resource|
|
||||
fmdata = data(resource.path).first
|
||||
|
||||
# Copy over special options
|
||||
opts = fmdata.extract!(:layout, :layout_engine, :renderer_options)
|
||||
if opts.has_key?(:renderer_options)
|
||||
opts[:renderer_options].symbolize_keys!
|
||||
end
|
||||
|
||||
ignored = fmdata.delete(:ignored)
|
||||
|
||||
# TODO: Enhance data? NOOOO
|
||||
# TODO: stringify-keys? immutable/freeze?
|
||||
|
||||
resource.add_metadata options: opts, page: fmdata
|
||||
|
||||
# TODO: resource.ignore! if ignored
|
||||
|
||||
# TODO: Save new template here somewhere?
|
||||
end
|
||||
end
|
||||
|
||||
def after_configuration
|
||||
app.ignore %r{\.frontmatter$}
|
||||
# TODO: Add to file watcher ignore?
|
||||
|
||||
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
|
||||
|
||||
app.sitemap.provides_metadata do |path|
|
||||
fmdata = data(path).first
|
||||
|
||||
data = {}
|
||||
|
||||
[:layout, :layout_engine].each do |opt|
|
||||
data[opt] = fmdata[opt] unless fmdata[opt].nil?
|
||||
end
|
||||
|
||||
if fmdata[:renderer_options]
|
||||
data[:renderer_options] = {}
|
||||
fmdata[:renderer_options].each do |k, v|
|
||||
data[:renderer_options][k.to_sym] = v
|
||||
end
|
||||
end
|
||||
|
||||
{ options: data }
|
||||
end
|
||||
# TODO: Replace all of this functionality
|
||||
#::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
|
||||
end
|
||||
|
||||
module ResourceInstanceMethods
|
||||
|
|
|
@ -46,7 +46,11 @@ module Middleman
|
|||
@source_file = source_file
|
||||
@destination_path = @path
|
||||
|
||||
@local_metadata = { options: {}, locals: {} }
|
||||
# Options are generally rendering/sitemap options
|
||||
# Locals are local variables for rendering this resource's template
|
||||
# Page are data that is exposed through this resource's data member.
|
||||
# Note: It is named 'page' for backwards compatibility with older MM.
|
||||
@local_metadata = { options: {}, locals: {}, page: {} }
|
||||
end
|
||||
|
||||
# Whether this resource has a template file
|
||||
|
@ -70,6 +74,12 @@ module Middleman
|
|||
result
|
||||
end
|
||||
|
||||
# Data about this resource, populated from frontmatter or extensions.
|
||||
# @return [HashWithIndifferentAccess]
|
||||
def data
|
||||
metadata[:page]
|
||||
end
|
||||
|
||||
# Merge in new metadata specific to this resource.
|
||||
# @param [Hash] meta A metadata block like provides_metadata_for_path takes
|
||||
def add_metadata(meta={})
|
||||
|
|
|
@ -19,9 +19,6 @@ module Middleman
|
|||
# which is the path relative to the source directory, minus any template
|
||||
# extensions. All "path" parameters used in this class are source paths.
|
||||
class Store
|
||||
# @return [Middleman::Application]
|
||||
attr_accessor :app
|
||||
|
||||
# Initialize with parent app
|
||||
# @param [Middleman::Application] app
|
||||
def initialize(app)
|
||||
|
@ -54,7 +51,7 @@ module Middleman
|
|||
register_resource_list_manipulator(k, m.new(self))
|
||||
end
|
||||
|
||||
app.config_context.class.send :delegate, :sitemap, to: :app
|
||||
@app.config_context.class.send :delegate, :sitemap, to: :app
|
||||
end
|
||||
|
||||
# Register an object which can transform the sitemap resource list. Best to register
|
||||
|
@ -128,63 +125,6 @@ module Middleman
|
|||
@resources_not_ignored = nil
|
||||
end
|
||||
|
||||
# Register a handler to provide metadata on a file path
|
||||
# @param [Regexp] matcher
|
||||
# @return [Array<Array<Proc, Regexp>>]
|
||||
def provides_metadata(matcher=nil, &block)
|
||||
@_provides_metadata ||= []
|
||||
@_provides_metadata << [block, matcher] if block_given?
|
||||
@_provides_metadata
|
||||
end
|
||||
|
||||
# Get the metadata for a specific file
|
||||
# @param [String] source_file
|
||||
# @return [Hash]
|
||||
def metadata_for_file(source_file)
|
||||
blank_metadata = { options: {}, locals: {} }
|
||||
|
||||
provides_metadata.reduce(blank_metadata) do |result, (callback, matcher)|
|
||||
next result if matcher && !source_file.match(matcher)
|
||||
|
||||
metadata = callback.call(source_file).dup
|
||||
result.deep_merge(metadata)
|
||||
end
|
||||
end
|
||||
|
||||
# Register a handler to provide metadata on a url path
|
||||
# @param [Regexp] matcher
|
||||
# @return [Array<Array<Proc, Regexp>>]
|
||||
def provides_metadata_for_path(matcher=nil, &block)
|
||||
@_provides_metadata_for_path ||= []
|
||||
if block_given?
|
||||
@_provides_metadata_for_path << [block, matcher]
|
||||
@_cached_metadata = {}
|
||||
end
|
||||
@_provides_metadata_for_path
|
||||
end
|
||||
|
||||
# Get the metadata for a specific URL
|
||||
# @param [String] request_path
|
||||
# @return [Hash]
|
||||
def metadata_for_path(request_path)
|
||||
return @_cached_metadata[request_path] if @_cached_metadata[request_path]
|
||||
|
||||
blank_metadata = { options: {}, locals: {} }
|
||||
|
||||
@_cached_metadata[request_path] = provides_metadata_for_path.reduce(blank_metadata) do |result, (callback, matcher)|
|
||||
case matcher
|
||||
when Regexp
|
||||
next result unless request_path =~ matcher
|
||||
when String
|
||||
next result unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{request_path}")
|
||||
end
|
||||
|
||||
metadata = callback.call(request_path).dup
|
||||
|
||||
result.deep_merge(metadata)
|
||||
end
|
||||
end
|
||||
|
||||
# Get the URL path for an on-disk file
|
||||
# @param [String] file
|
||||
# @return [String]
|
||||
|
|
Loading…
Reference in a new issue