From da4e8f2ee1c557924a775cca3a86943ad5192f70 Mon Sep 17 00:00:00 2001 From: Jonas Pfenniger Date: Fri, 25 May 2012 18:59:26 +0100 Subject: [PATCH] Front-matter should be at start of file It's the theme of the moment ; regexp checking. Just in case we have two line start with --- in the file, we want to make sure it's not interpreted as a front-matter. --- lib/jekyll/convertible.rb | 2 +- test/fixtures/broken_front_matter1.erb | 5 +++++ test/fixtures/front_matter.erb | 4 ++++ test/test_convertible.rb | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/broken_front_matter1.erb create mode 100644 test/fixtures/front_matter.erb create mode 100644 test/test_convertible.rb diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb index fe25b30e..1d0139d3 100644 --- a/lib/jekyll/convertible.rb +++ b/lib/jekyll/convertible.rb @@ -28,7 +28,7 @@ module Jekyll self.content = File.read(File.join(base, name)) begin - if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m + if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m self.content = $POSTMATCH self.data = YAML.load($1) end diff --git a/test/fixtures/broken_front_matter1.erb b/test/fixtures/broken_front_matter1.erb new file mode 100644 index 00000000..c18befb6 --- /dev/null +++ b/test/fixtures/broken_front_matter1.erb @@ -0,0 +1,5 @@ +# Some stuff on the first line +--- +test: good +--- +Real content starts here diff --git a/test/fixtures/front_matter.erb b/test/fixtures/front_matter.erb new file mode 100644 index 00000000..96e46a07 --- /dev/null +++ b/test/fixtures/front_matter.erb @@ -0,0 +1,4 @@ +--- +test: good +--- +Real content starts here diff --git a/test/test_convertible.rb b/test/test_convertible.rb new file mode 100644 index 00000000..2032715b --- /dev/null +++ b/test/test_convertible.rb @@ -0,0 +1,22 @@ +require 'helper' +require 'ostruct' + +class TestConvertible < Test::Unit::TestCase + context "yaml front-matter" do + setup do + @convertible = OpenStruct.new + @convertible.extend Jekyll::Convertible + @base = File.expand_path('../fixtures', __FILE__) + end + + should "parse the front-matter correctly" do + ret = @convertible.read_yaml(@base, 'front_matter.erb') + assert_equal({'test' => 'good'}, ret) + end + + should "not parse if the front-matter is not at the start of the file" do + ret = @convertible.read_yaml(@base, 'broken_front_matter1.erb') + assert_equal({}, ret) + end + end +end