Remove neighbor frontmatter support

This commit is contained in:
Ben Hollis 2014-07-03 19:44:26 -07:00
parent a19c1cbecc
commit 0cbc232dac
5 changed files with 90 additions and 50 deletions

View file

@ -0,0 +1,27 @@
ignore '*.frontmatter'
# Reads neighbor for every file on every refresh.
# TODO: Optimize
class NeighborFrontmatter < ::Middleman::Extension
self.resource_list_manipulator_priority = 81
def manipulate_resource_list(resources)
resources.each do |resource|
next unless resource.source_file
neighbor = "#{resource.source_file}.frontmatter"
if File.exists?(neighbor)
fmdata = app.extensions[:front_matter].frontmatter_and_content(neighbor).first
opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type)
opts[:renderer_options].symbolize_keys! if opts.key?(:renderer_options)
ignored = fmdata.delete(:ignored)
resource.add_metadata options: opts, page: fmdata
resource.ignore! if ignored == true && !resource.proxy?
end
end
end
end
Middleman::Extensions.register :neighbor_frontmatter, NeighborFrontmatter unless Middleman::Extensions.registered.include? :neighbor_frontmatter
activate :neighbor_frontmatter

View file

@ -2,3 +2,31 @@
proxy 'proxied.html', 'ignored.html'
page 'override_layout.html', layout: :alternate
page 'page_mentioned.html'
ignore '*.frontmatter'
# Reads neighbor for every file on every refresh.
# TODO: Optimize
class NeighborFrontmatter < ::Middleman::Extension
self.resource_list_manipulator_priority = 81
def manipulate_resource_list(resources)
resources.each do |resource|
next unless resource.source_file
neighbor = "#{resource.source_file}.frontmatter"
if File.exists?(neighbor)
fmdata = app.extensions[:front_matter].frontmatter_and_content(neighbor).first
opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type)
opts[:renderer_options].symbolize_keys! if opts.key?(:renderer_options)
ignored = fmdata.delete(:ignored)
resource.add_metadata options: opts, page: fmdata
resource.ignore! if ignored == true && !resource.proxy?
end
end
end
end
Middleman::Extensions.register :neighbor_frontmatter, NeighborFrontmatter unless Middleman::Extensions.registered.include? :neighbor_frontmatter
activate :neighbor_frontmatter

View file

@ -57,10 +57,6 @@ module Middleman::CoreExtensions
end
end
def after_configuration
app.ignore %r{\.frontmatter$}
end
# Get the template data from a path
# @param [String] path
# @return [String]
@ -70,16 +66,7 @@ module Middleman::CoreExtensions
def data(path)
p = normalize_path(path)
@cache[p] ||= begin
data, content = frontmatter_and_content(p)
if file_watcher.exists?("#{path}.frontmatter")
external_data, _ = frontmatter_and_content("#{p}.frontmatter")
data = external_data.deep_merge(data)
end
[data, content]
end
@cache[p] ||= frontmatter_and_content(p)
end
def clear_data(file)
@ -88,11 +75,43 @@ module Middleman::CoreExtensions
file = File.join(app.root, file)
prefix = app.source_dir.sub(/\/$/, '') + '/'
return unless file.include?(prefix)
path = file.sub(prefix, '').sub(/\.frontmatter$/, '')
path = file.sub(prefix, '')
@cache.delete(path)
end
# Get the frontmatter and plain content from a file
# @param [String] path
# @return [Array<Middleman::Util::HashWithIndifferentAccess, String>]
def frontmatter_and_content(path)
full_path = if Pathname(path).relative?
File.join(app.source_dir, path)
else
path
end
data = {}
return [data, nil] if !app.files.exists?(full_path) || ::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
result = parse_yaml_front_matter(content, full_path) || parse_json_front_matter(content, full_path)
return result if result
rescue
# Probably a binary file, move on
end
[data, content]
end
private
# Parse YAML frontmatter out of a string
@ -142,38 +161,6 @@ module Middleman::CoreExtensions
[{}, content]
end
# Get the frontmatter and plain content from a file
# @param [String] path
# @return [Array<Middleman::Util::HashWithIndifferentAccess, String>]
def frontmatter_and_content(path)
full_path = if Pathname(path).relative?
File.join(app.source_dir, path)
else
path
end
data = {}
return [data, nil] if !file_watcher.exists?(full_path) || ::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
result = parse_yaml_front_matter(content, full_path) || parse_json_front_matter(content, full_path)
return result if result
rescue
# Probably a binary file, move on
end
[data, content]
end
def normalize_path(path)
path.sub(%r{^#{Regexp.escape(app.source_dir)}\/}, '')
end

View file

@ -45,8 +45,6 @@ module Middleman
options = opts.dup
# Default layout
# TODO: This seems wrong
options[:layout] = @app.config[:layout] if options[:layout].nil?
metadata = {
options: options,
locals: options.delete(:locals) || {},

View file

@ -136,7 +136,7 @@ module Middleman
#
# @return [Boolean]
def binary?
::Middleman::Util.binary?(source_file)
source_file && ::Middleman::Util.binary?(source_file)
end
end
end