Skip to content

Commit

Permalink
resolves #354 don't match front matter in the middle of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Sep 8, 2013
1 parent 266b4f8 commit 057367b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/awestruct/handlers/front_matter_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/front_matter_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/test-data/front-matter-file-no-content.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
foo: bar
---
9 changes: 9 additions & 0 deletions spec/test-data/front-matter-looking.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---

^ That was a Markdown horizontal rule.

Here's another one:

---

This is more content.
12 changes: 12 additions & 0 deletions spec/test-data/front-matter-middle.txt
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 057367b

Please sign in to comment.