diff --git a/lib/jekyll/frontmatter_defaults.rb b/lib/jekyll/frontmatter_defaults.rb index e747143b..2b57cdc2 100644 --- a/lib/jekyll/frontmatter_defaults.rb +++ b/lib/jekyll/frontmatter_defaults.rb @@ -63,7 +63,7 @@ module Jekyll end def applies_path?(scope, path) - return true if scope['path'].empty? + return true if !scope.has_key?('path') || scope['path'].empty? scope_path = Pathname.new(scope['path']) Pathname.new(sanitize_path(path)).ascend do |path| @@ -83,7 +83,7 @@ module Jekyll # # Returns true if the set is valid and can be used in this class def valid?(set) - set.is_a?(Hash) && set['scope'].is_a?(Hash) && set['scope']['path'].is_a?(String) && set['values'].is_a?(Hash) + set.is_a?(Hash) && set['values'].is_a?(Hash) end # Determines if a new scope has precedence over an old one @@ -112,7 +112,7 @@ module Jekyll # Returns an array of hashes def matching_sets(path, type) valid_sets.select do |set| - applies?(set['scope'], path, type) + !set.has_key?('scope') || applies?(set['scope'], path, type) end end diff --git a/test/test_front_matter_defaults.rb b/test/test_front_matter_defaults.rb new file mode 100644 index 00000000..fe0244c8 --- /dev/null +++ b/test/test_front_matter_defaults.rb @@ -0,0 +1,127 @@ +require 'helper' + +class TestFrontMatterDefaults < Test::Unit::TestCase + + context "A site with full front matter defaults" do + setup do + @site = Site.new(Jekyll.configuration({ + "source" => source_dir, + "destination" => dest_dir, + "defaults" => [{ + "scope" => { + "path" => "contacts", + "type" => "page" + }, + "values" => { + "key" => "val" + } + }] + })) + @site.process + @affected = @site.pages.find { |page| page.relative_path == "/contacts/bar.html" } + @not_affected = @site.pages.find { |page| page.relative_path == "about.html" } + end + + should "affect only the specified path and type" do + assert_equal @affected.data["key"], "val" + assert_equal @not_affected.data["key"], nil + end + end + + context "A site with front matter defaults with no type" do + setup do + @site = Site.new(Jekyll.configuration({ + "source" => source_dir, + "destination" => dest_dir, + "defaults" => [{ + "scope" => { + "path" => "win" + }, + "values" => { + "key" => "val" + } + }] + })) + @site.process + @affected = @site.posts.find { |page| page.relative_path =~ /^\/win/ } + @not_affected = @site.pages.find { |page| page.relative_path == "about.html" } + end + + should "affect only the specified path and all types" do + assert_equal @affected.data["key"], "val" + assert_equal @not_affected.data["key"], nil + end + end + + context "A site with front matter defaults with no path" do + setup do + @site = Site.new(Jekyll.configuration({ + "source" => source_dir, + "destination" => dest_dir, + "defaults" => [{ + "scope" => { + "type" => "page" + }, + "values" => { + "key" => "val" + } + }] + })) + @site.process + @affected = @site.pages + @not_affected = @site.posts + end + + should "affect only the specified type and all paths" do + assert_equal @affected.reject { |page| page.data["key"] == "val" }, [] + assert_equal @not_affected.reject { |page| page.data["key"] == "val" }, @not_affected + end + end + + context "A site with front matter defaults with no path or type" do + setup do + @site = Site.new(Jekyll.configuration({ + "source" => source_dir, + "destination" => dest_dir, + "defaults" => [{ + "scope" => { + }, + "values" => { + "key" => "val" + } + }] + })) + @site.process + @affected = @site.pages + @not_affected = @site.posts + end + + should "affect all types and paths" do + assert_equal @affected.reject { |page| page.data["key"] == "val" }, [] + assert_equal @not_affected.reject { |page| page.data["key"] == "val" }, [] + end + end + + context "A site with front matter defaults with no scope" do + setup do + @site = Site.new(Jekyll.configuration({ + "source" => source_dir, + "destination" => dest_dir, + "defaults" => [{ + "values" => { + "key" => "val" + } + }] + })) + @site.process + @affected = @site.pages + @not_affected = @site.posts + end + + should "affect all types and paths" do + assert_equal @affected.reject { |page| page.data["key"] == "val" }, [] + assert_equal @not_affected.reject { |page| page.data["key"] == "val" }, [] + end + end + +end