From 92713dfe929b86a83c0b961b4109cfe7ff613854 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 25 Mar 2012 12:43:11 -0700 Subject: [PATCH] Make frontmatter always return a hash, and freeze it. This reduces the need for nil checks and prevents users from accidentally trying to modify frontmatter data which may be cached elsewhere. --- .../lib/middleman-core/core_extensions/front_matter.rb | 6 +++--- middleman-core/lib/middleman-core/sitemap/page.rb | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) 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 0ef6f34c..63bb2366 100644 --- a/middleman-core/lib/middleman-core/core_extensions/front_matter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/front_matter.rb @@ -139,7 +139,7 @@ module Middleman::CoreExtensions::FrontMatter if result data, content = result - data = ::Middleman.recursively_enhance(data) + data = ::Middleman.recursively_enhance(data).freeze file = file.sub(@app.source_dir, "") @local_data[file] = [data, content] path = File.join(@app.source_dir, file) @@ -164,12 +164,12 @@ module Middleman::CoreExtensions::FrontMatter # Get the frontmatter for a given path # @param [String] path - # @return [Hash, nil] + # @return [Hash] def data(path) if @local_data.has_key?(path.to_s) @local_data[path.to_s] else - nil + {}.freeze end end diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index 3e08fd76..730089e7 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -204,10 +204,9 @@ module Middleman::Sitemap end # This page's frontmatter - # @return [Hash, nil] + # @return [Hash] def data - data, content = app.frontmatter(relative_path) - data || nil + app.frontmatter(relative_path).first end # This page's parent page @@ -216,7 +215,6 @@ module Middleman::Sitemap parts = path.split("/") if path.include?(app.index_file) parts.pop - else end return nil if parts.length < 1