2015-06-17 00:30:37 +02:00
|
|
|
# Core Pathname library used for traversal
|
2013-02-06 22:19:09 +01:00
|
|
|
require 'pathname'
|
2012-10-25 00:02:32 +02:00
|
|
|
|
2015-06-17 00:30:37 +02:00
|
|
|
# DbC
|
|
|
|
require 'middleman-core/contracts'
|
|
|
|
|
|
|
|
require 'active_support/core_ext/hash/keys'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2015-06-17 00:30:37 +02:00
|
|
|
require 'middleman-core/util/data'
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-05-25 02:11:46 +02:00
|
|
|
# Extensions namespace
|
|
|
|
module Middleman::CoreExtensions
|
|
|
|
class FrontMatter < ::Middleman::Extension
|
2014-05-31 07:32:39 +02:00
|
|
|
# Try to run after routing but before directory_indexes
|
|
|
|
self.resource_list_manipulator_priority = 90
|
|
|
|
|
2013-05-25 02:11:46 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2013-05-23 21:50:46 +02:00
|
|
|
|
2013-05-25 02:11:46 +02:00
|
|
|
@cache = {}
|
|
|
|
end
|
2012-05-09 22:15:39 +02:00
|
|
|
|
2013-05-25 02:11:46 +02:00
|
|
|
def before_configuration
|
2014-12-23 23:54:21 +01:00
|
|
|
app.files.on_change(:source, &method(:clear_data))
|
2013-05-25 02:11:46 +02:00
|
|
|
end
|
2012-05-09 22:15:39 +02:00
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
# @return Array<Middleman::Sitemap::Resource>
|
|
|
|
Contract ResourceList => ResourceList
|
2014-04-28 06:54:53 +02:00
|
|
|
def manipulate_resource_list(resources)
|
|
|
|
resources.each do |resource|
|
2015-09-09 20:55:56 +02:00
|
|
|
next if resource.file_descriptor.nil?
|
2014-05-31 06:50:10 +02:00
|
|
|
|
2015-09-09 20:55:56 +02:00
|
|
|
fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup
|
2014-04-28 06:54:53 +02:00
|
|
|
|
|
|
|
# Copy over special options
|
2014-04-28 08:21:12 +02:00
|
|
|
# TODO: Should we make people put these under "options" instead of having
|
|
|
|
# special known keys?
|
|
|
|
opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type)
|
2014-05-31 08:19:33 +02:00
|
|
|
opts[:renderer_options].symbolize_keys! if opts.key?(:renderer_options)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
ignored = fmdata.delete(:ignored)
|
2013-04-13 01:14:16 +02:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
# TODO: Enhance data? NOOOO
|
|
|
|
# TODO: stringify-keys? immutable/freeze?
|
2014-01-04 01:18:16 +01:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
resource.add_metadata options: opts, page: fmdata
|
2014-03-30 01:21:49 +01:00
|
|
|
|
2014-07-04 19:38:25 +02:00
|
|
|
resource.ignore! if ignored == true && !resource.is_a?(::Middleman::Sitemap::ProxyResource)
|
2011-11-28 07:04:19 +01:00
|
|
|
|
2014-04-28 06:54:53 +02:00
|
|
|
# TODO: Save new template here somewhere?
|
2012-05-09 06:05:55 +02:00
|
|
|
end
|
|
|
|
end
|
2013-12-28 19:14:15 +01:00
|
|
|
|
2014-01-02 06:19:05 +01:00
|
|
|
# Get the template data from a path
|
|
|
|
# @param [String] path
|
|
|
|
# @return [String]
|
2014-07-16 03:01:45 +02:00
|
|
|
Contract String => Maybe[String]
|
2014-01-02 06:19:05 +01:00
|
|
|
def template_data_for_file(path)
|
|
|
|
data(path).last
|
2013-05-25 02:11:46 +02:00
|
|
|
end
|
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String => [Hash, Maybe[String]]
|
2013-05-25 02:11:46 +02:00
|
|
|
def data(path)
|
2014-07-16 03:01:45 +02:00
|
|
|
file = app.files.find(:source, path)
|
2013-05-25 02:11:46 +02:00
|
|
|
|
2014-07-16 03:01:45 +02:00
|
|
|
return [{}, nil] unless file
|
2013-05-25 02:11:46 +02:00
|
|
|
|
2015-06-17 00:30:37 +02:00
|
|
|
@cache[file[:full_path]] ||= ::Middleman::Util::Data.parse(file[:full_path])
|
2014-07-16 03:01:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Contract ArrayOf[IsA['Middleman::SourceFile']], ArrayOf[IsA['Middleman::SourceFile']] => Any
|
|
|
|
def clear_data(updated_files, removed_files)
|
|
|
|
(updated_files + removed_files).each do |file|
|
|
|
|
@cache.delete(file[:full_path])
|
|
|
|
end
|
2013-05-25 02:11:46 +02:00
|
|
|
end
|
2011-06-06 20:39:57 +02:00
|
|
|
end
|
2012-05-19 22:27:38 +02:00
|
|
|
end
|