diff --git a/Rakefile b/Rakefile index 062e398..0a80650 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/toto.rb b/lib/toto.rb index d74b920..cbb4a9a 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -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) } @@ -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 diff --git a/test/articles/2011-01-24-damage.txt b/test/articles/2011-01-24-damage.txt new file mode 100644 index 0000000..be551eb --- /dev/null +++ b/test/articles/2011-01-24-damage.txt @@ -0,0 +1,3 @@ +front matter is missing!!! + +## this is a mere recipe in markdown \ No newline at end of file diff --git a/test/toto_test.rb b/test/toto_test.rb index aa090b6..0fb450c 100644 --- a/test/toto_test.rb +++ b/test/toto_test.rb @@ -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 @@ -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 diff --git a/toto.gemspec b/toto.gemspec index 03b032e..cd218a8 100644 --- a/toto.gemspec +++ b/toto.gemspec @@ -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",