From 057367beb3758bef37a3e569a4f3daaf0c561eff Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Wed, 4 Sep 2013 19:45:20 -0600 Subject: [PATCH] resolves #354 don't match front matter in the middle of the file --- .../handlers/front_matter_handler.rb | 31 +++++++++++-------- spec/front_matter_handler_spec.rb | 14 +++++++++ .../front-matter-file-no-content.txt | 1 + spec/test-data/front-matter-looking.txt | 9 ++++++ spec/test-data/front-matter-middle.txt | 12 +++++++ 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 spec/test-data/front-matter-looking.txt create mode 100644 spec/test-data/front-matter-middle.txt diff --git a/lib/awestruct/handlers/front_matter_handler.rb b/lib/awestruct/handlers/front_matter_handler.rb index 19414903..50024bd5 100644 --- a/lib/awestruct/handlers/front_matter_handler.rb +++ b/lib/awestruct/handlers/front_matter_handler.rb @@ -51,35 +51,40 @@ def parse_parts dash_lines = 0 mode = :yaml - @raw_content = '' + @raw_content = nil @content_line_offset = 0 + first_line = true full_content.each_line do |line| - if ( line.strip == '---' ) - dash_lines = dash_lines +1 + if line.rstrip == '---' && mode == :yaml + unless first_line + @content_line_offset += 1 + yaml_content << line + mode = :page + next + end + elsif first_line + mode = :page end - if ( mode == :yaml ) + + if mode == :yaml @content_line_offset += 1 yaml_content << line + elsif @raw_content.nil? + @raw_content = line else @raw_content << line end - if ( dash_lines == 2 ) - mode = :page - end + first_line = false end - if ( dash_lines == 0 ) - @raw_content = yaml_content - yaml_content = '' - @content_line_offset = 0 - elsif ( mode == :yaml ) + if mode == :yaml @raw_content = nil @content_line_offset = -1 end begin - @front_matter = YAML.load( yaml_content ) || {} + @front_matter = yaml_content.empty? ? {} : (YAML.load yaml_content) rescue => e $LOG.error "could not parse #{relative_source_path}" if $LOG.error? raise e diff --git a/spec/front_matter_handler_spec.rb b/spec/front_matter_handler_spec.rb index 02e458c3..3d111282 100644 --- a/spec/front_matter_handler_spec.rb +++ b/spec/front_matter_handler_spec.rb @@ -42,6 +42,20 @@ def file_input(filename) handler.raw_content.should be_nil end + it 'should not match front matter in the middle of a file' do + handler = Awestruct::Handlers::FrontMatterHandler.new( @site, file_input('front-matter-middle.txt') ) + handler.front_matter.should_not be_nil + handler.front_matter.should be_empty + handler.raw_content.should_not be_nil + end + + it 'should not mistake horizontal rule for front matter' do + handler = Awestruct::Handlers::FrontMatterHandler.new( @site, file_input('front-matter-looking.txt') ) + handler.front_matter.should_not be_nil + handler.front_matter.should be_empty + handler.raw_content.should_not be_nil + end + it 'should be able to handle UTF-8 characters' do handler = Awestruct::Handlers::FrontMatterHandler.new( @site, file_input('front-matter-file-utf8.txt') ) handler.front_matter.should_not be_nil diff --git a/spec/test-data/front-matter-file-no-content.txt b/spec/test-data/front-matter-file-no-content.txt index 23809fe0..b0f4d77f 100644 --- a/spec/test-data/front-matter-file-no-content.txt +++ b/spec/test-data/front-matter-file-no-content.txt @@ -1,2 +1,3 @@ --- foo: bar +--- diff --git a/spec/test-data/front-matter-looking.txt b/spec/test-data/front-matter-looking.txt new file mode 100644 index 00000000..1efba5e3 --- /dev/null +++ b/spec/test-data/front-matter-looking.txt @@ -0,0 +1,9 @@ + --- + +^ That was a Markdown horizontal rule. + +Here's another one: + +--- + +This is more content. diff --git a/spec/test-data/front-matter-middle.txt b/spec/test-data/front-matter-middle.txt new file mode 100644 index 00000000..bf68a3cc --- /dev/null +++ b/spec/test-data/front-matter-middle.txt @@ -0,0 +1,12 @@ +This is content + +--- + +slkdjfklsjd f;klsjaljkdflj2k'l23423 4234jkl +wejtrkl'w jrk l'23tr +wj fkwe'jt'wk j'tfk l34j +tFjroi k ntfklqwnkltf: get off my lawn : This is not front matter! + +--- + +We're all just content here.