From 3903d57d30160e7eae469402a0db24d0716e491c Mon Sep 17 00:00:00 2001 From: thierry Date: Mon, 24 Jan 2011 16:18:37 +0100 Subject: [PATCH 1/3] feature : do start when matter is damaged (not damaged means : YAML parses string and returns a Hash) --- Rakefile | 2 +- lib/toto.rb | 8 +++++++- test/articles/2011-01-24-damage.txt | 3 +++ test/toto_test.rb | 9 +++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 test/articles/2011-01-24-damage.txt 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..527f681 100644 --- a/test/toto_test.rb +++ b/test/toto_test.rb @@ -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 From d3d8616e58a7cbcf732629cd54e21f20d1c83b10 Mon Sep 17 00:00:00 2001 From: thierry Date: Mon, 24 Jan 2011 16:20:34 +0100 Subject: [PATCH 2/3] adding an article had side effect of making one red...fixed --- test/toto_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/toto_test.rb b/test/toto_test.rb index 527f681..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 From 1f0f2e2d424b01d4dd1091b2d93160e2cceddba0 Mon Sep 17 00:00:00 2001 From: thierry Date: Mon, 24 Jan 2011 16:33:48 +0100 Subject: [PATCH 3/3] add 'damaging' article to gem ? dunno ... --- toto.gemspec | 1 + 1 file changed, 1 insertion(+) 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",