Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friendly display of article having a damaged "front matter" #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.libs << 'lib' << 'test' << '.'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
Expand Down
8 changes: 7 additions & 1 deletion lib/toto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def load

# use the date from the filename, or else toto won't find the article
@obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/
($1 ? {:date => $1} : {}).merge(YAML.load(meta))
($1 ? {:date => $1} : {}).merge(load_front(meta))
elsif @obj.is_a? Hash
@obj
end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) }
Expand Down Expand Up @@ -280,6 +280,12 @@ def date() @config[:date].call(self[:date]) end
def author() self[:author] || @config[:author] end
def to_html() self.load; super(:article, @config) end
alias :to_s to_html

private
def load_front(meta)
matter = YAML.load(meta)
matter.is_a?(Hash) ? matter : {:title => @obj.to_s, :body => "Failed to parse front matter : #{meta}"}
end
end

class Config < Hash
Expand Down
3 changes: 3 additions & 0 deletions test/articles/2011-01-24-damage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
front matter is missing!!!

## this is a mere recipe in markdown
11 changes: 10 additions & 1 deletion test/toto_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
setup { @toto.get('/about') }
asserts("returns a 200") { topic.status }.equals 200
asserts("body is not empty") { not topic.body.empty? }
should("have access to @articles") { topic.body }.includes_html("#count" => /5/)
should("have access to @articles") { topic.body }.includes_html("#count" => /6/)
end

context "GET a single article" do
Expand Down Expand Up @@ -241,6 +241,15 @@ def readme() "#{self[:name]}'s README" end

should("be in the directory") { topic.path }.equals Date.today.strftime("/blog/%Y/%m/%d/toto-and-the-wizard-of-oz/")
end

context "damaged article" do
setup do
conf = Toto::Config.new({})
damaged = File.expand_path('../articles/2011-01-24-damage.txt', __FILE__)
Toto::Article.new(damaged, conf)
end
should("load with a damaged body") {topic.load[:body]}.matches(/^Failed to parse front matter/)
end
end
end

Expand Down
1 change: 1 addition & 0 deletions toto.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |s|
"test/articles/2009-04-01-tilt-factor.txt",
"test/articles/2009-12-04-some-random-article.txt",
"test/articles/2009-12-11-the-dichotomy-of-design.txt",
"test/articles/2011-01-24-damage.txt",
"test/autotest.rb",
"test/templates/about.rhtml",
"test/templates/archives.rhtml",
Expand Down