From 0cbc232dac43584d0523ee3cd4b245603e9edcd9 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 3 Jul 2014 19:44:26 -0700 Subject: [PATCH] Remove neighbor frontmatter support --- .../frontmatter-neighbor-app/config.rb | 27 +++++++ .../config.rb | 28 +++++++ .../core_extensions/front_matter.rb | 81 ++++++++----------- .../middleman-core/core_extensions/routing.rb | 2 - .../lib/middleman-core/sitemap/resource.rb | 2 +- 5 files changed, 90 insertions(+), 50 deletions(-) diff --git a/middleman-core/fixtures/frontmatter-neighbor-app/config.rb b/middleman-core/fixtures/frontmatter-neighbor-app/config.rb index e69de29b..234b9a20 100644 --- a/middleman-core/fixtures/frontmatter-neighbor-app/config.rb +++ b/middleman-core/fixtures/frontmatter-neighbor-app/config.rb @@ -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 diff --git a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb index a5e8129a..4318d2fd 100644 --- a/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb +++ b/middleman-core/fixtures/frontmatter-settings-neighbor-app/config.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb index 712d5d6c..546bc761 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -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] + 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] - 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 diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index b7f4ee52..24539ce0 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -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) || {}, diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 97b4e832..3ccc9895 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -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