Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix bug where wrong content was produced when file contained a front …

…matter.
  • Loading branch information...
commit 7904031bae88c540ef7526e2135120140de58641 1 parent ffad550
@marano authored
View
42 lib/rollin/article.rb
@@ -44,20 +44,15 @@ def matches?(search)
end
def metatags
- # Stolen from Jekyll
+ file = read_from_file
+ return {} unless file.has_front_matter?
begin
- content = File.read(@source_file)
-
- if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
- content = $POSTMATCH
- return YAML.safe_load($1)
- end
+ return YAML.safe_load(file.yaml_front_matter)
rescue SyntaxError => e
puts "YAML Exception reading #{File.join(@source_file)}: #{e.message}"
rescue Exception => e
puts "Error reading file #{File.join(@source_file)}: #{e.message}"
end
-
return {}
end
@@ -70,6 +65,35 @@ def body
autolink: true,
space_after_headers: true,
hard_wrap: true)
- redcarpet.render(File.read(@source_file))
+ redcarpet.render(read_from_file.content)
+ end
+
+ private
+
+ def read_from_file
+ raw = File.read(@source_file)
+
+ # Stolen from Jekyll
+ if raw =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
+ content = $POSTMATCH
+ yaml_front_matter = $1
+ content = raw[yaml_front_matter.size + 3, raw.size - 1]
+ FileContent.new(yaml_front_matter, content)
+ else
+ FileContent.new(nil, raw)
+ end
+ end
+end
+
+class FileContent
+ attr_reader :yaml_front_matter, :content
+
+ def initialize(yaml_front_matter, content)
+ @yaml_front_matter = yaml_front_matter
+ @content = content
+ end
+
+ def has_front_matter?
+ !yaml_front_matter.nil?
end
end
View
5 spec/fixtures/2013_05_01_My_first_post.mk
@@ -1,3 +1,8 @@
+---
+tags:
+ - manero
+---
+
## This is my first post
And here we go!
View
41 spec/rollin_spec.rb
@@ -16,8 +16,9 @@
let (:third_article) { blog.articles[2] }
let (:fourth_article) { blog.articles[3] }
+ let (:article_with_single_tag_metatag) { blog.articles[0] }
let (:article_with_title_metatag) { blog.articles[1] }
- let (:article_with_tag_metatag) { blog.articles[2] }
+ let (:article_with_multiple_tag_metatag) { blog.articles[2] }
let (:article_with_published_metatag) { blog.articles[3] }
context 'article content' do
@@ -31,7 +32,7 @@
article.day.should == 1
article.date.should == Date.new(2013, 5, 1)
article.body.should == "<h2>This is my first post</h2>\n\n<p>And here we go!</p>\n"
- article.metatags.should be_empty
+ article.metatags.should == { 'tags' => [ 'manero' ] }
end
it 'allows article title definition with metatag' do
@@ -39,7 +40,7 @@
end
it 'exposes the list of defined metatags' do
- article_with_tag_metatag.metatags.should == { 'tags' => ['manero', 'massa', 'bacana'] }
+ article_with_multiple_tag_metatag.metatags.should == { 'tags' => ['manero', 'massa', 'bacana'] }
article_with_published_metatag.metatags.should == { 'published' => false }
end
end
@@ -50,11 +51,11 @@
end
it 'searches by metatags' do
- blog.article(:tags => 'manero').should == article_with_tag_metatag
- blog.article('tags' => 'manero').should == article_with_tag_metatag
+ blog.article(:tags => 'manero').should == article_with_single_tag_metatag
+ blog.article('tags' => 'manero').should == article_with_single_tag_metatag
- blog.articles(:tags => 'manero').should == [ article_with_tag_metatag ]
- blog.articles('tags' => 'manero').should == [ article_with_tag_metatag ]
+ blog.articles(:tags => 'manero').should == [ article_with_single_tag_metatag, article_with_multiple_tag_metatag ]
+ blog.articles('tags' => 'manero').should == [ article_with_single_tag_metatag, article_with_multiple_tag_metatag ]
blog.article(:published => false).should == article_with_published_metatag
blog.article('published' => false).should == article_with_published_metatag
@@ -79,19 +80,19 @@
it 'shows a list of existent metatags' do
blog.should have(3).metatags
- blog.metatags[0].label.should == 'title'
- blog.metatags[0].should have(1).values
- blog.metatags[0].values[0].content.should == 'This is a super post!'
- blog.metatags[0].values[0].articles.should == [ article_with_title_metatag ]
-
- blog.metatags[1].label.should == 'tags'
- blog.metatags[1].should have(3).values
- blog.metatags[1].values[0].content.should == 'manero'
- blog.metatags[1].values[0].articles.should == [ article_with_tag_metatag ]
- blog.metatags[1].values[1].content.should == 'massa'
- blog.metatags[1].values[1].articles.should == [ article_with_tag_metatag ]
- blog.metatags[1].values[2].content.should == 'bacana'
- blog.metatags[1].values[2].articles.should == [ article_with_tag_metatag ]
+ blog.metatags[0].label.should == 'tags'
+ blog.metatags[0].should have(3).values
+ blog.metatags[0].values[0].content.should == 'manero'
+ blog.metatags[0].values[0].articles.should == [ article_with_single_tag_metatag, article_with_multiple_tag_metatag ]
+ blog.metatags[0].values[1].content.should == 'massa'
+ blog.metatags[0].values[1].articles.should == [ article_with_multiple_tag_metatag ]
+ blog.metatags[0].values[2].content.should == 'bacana'
+ blog.metatags[0].values[2].articles.should == [ article_with_multiple_tag_metatag ]
+
+ blog.metatags[1].label.should == 'title'
+ blog.metatags[1].should have(1).values
+ blog.metatags[1].values[0].content.should == 'This is a super post!'
+ blog.metatags[1].values[0].articles.should == [ article_with_title_metatag ]
blog.metatags[2].label.should == 'published'
blog.metatags[2].should have(1).values
Please sign in to comment.
Something went wrong with that request. Please try again.