From 1bb2259a103edcfb16a0886afb26f2ee99a9fb62 Mon Sep 17 00:00:00 2001 From: fen Date: Wed, 2 Nov 2016 00:00:38 +0100 Subject: [PATCH 1/4] add failing test --- test/test_url.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/test_url.rb b/test/test_url.rb index f5a77622..5f983909 100644 --- a/test/test_url.rb +++ b/test/test_url.rb @@ -80,5 +80,26 @@ class TestURL < JekyllUnitTest url.to_s end end + + should "ignore NoMethodErrors when a placeholder is not found" do + site = fixture_site({ + "collections" => { + "methods" => { + "output" => true + } + } + }) + site.read + matching_doc = site.collections["methods"].docs.find do |doc| + doc.relative_path == "_methods/escape-+ #%20[].md" + end + out, err = capture_io do + URL.new( + :template => "/methods/:title/:headline", + :placeholders => matching_doc.url_placeholders + ).to_s + end + assert out.include? ":headline is not defined!" + end end end From a2ffde8f1423a250c921b6e23693a38b33235eaf Mon Sep 17 00:00:00 2001 From: fen Date: Wed, 2 Nov 2016 00:22:20 +0100 Subject: [PATCH 2/4] rewrite test to only not throw error --- test/test_url.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/test_url.rb b/test/test_url.rb index 5f983909..28fab43d 100644 --- a/test/test_url.rb +++ b/test/test_url.rb @@ -93,13 +93,11 @@ class TestURL < JekyllUnitTest matching_doc = site.collections["methods"].docs.find do |doc| doc.relative_path == "_methods/escape-+ #%20[].md" end - out, err = capture_io do - URL.new( - :template => "/methods/:title/:headline", - :placeholders => matching_doc.url_placeholders - ).to_s - end - assert out.include? ":headline is not defined!" + URL.new( + :template => "/methods/:title/:headline", + :placeholders => matching_doc.url_placeholders + ).to_s + pass end end end From 2d35364c02003ccbb9c5b06c291b65c2f6c3b278 Mon Sep 17 00:00:00 2001 From: fen Date: Wed, 2 Nov 2016 00:22:41 +0100 Subject: [PATCH 3/4] implement warning about missing keys in url drops --- lib/jekyll/url.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index c74e11c8..32bcfb8f 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -86,7 +86,11 @@ module Jekyll def generate_url_from_drop(template) template.gsub(%r!:([a-z_]+)!) do |match| - replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze)) + begin + replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze)) + rescue NoMethodError + Jekyll.logger.warn "", "#{match} is not defined!" + end if replacement.nil? "".freeze else From e92dd2055dd0e64dff66a6ec58ed3dce5b7af5bb Mon Sep 17 00:00:00 2001 From: fen Date: Wed, 2 Nov 2016 21:30:23 +0100 Subject: [PATCH 4/4] check for the urldrop key first --- lib/jekyll/drops/url_drop.rb | 5 +++++ lib/jekyll/url.rb | 11 +++++------ test/test_url.rb | 11 ++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/jekyll/drops/url_drop.rb b/lib/jekyll/drops/url_drop.rb index 04e48a20..5c97190d 100644 --- a/lib/jekyll/drops/url_drop.rb +++ b/lib/jekyll/drops/url_drop.rb @@ -78,6 +78,11 @@ module Jekyll def y_day @obj.date.strftime("%j") end + + private + def fallback_data + {} + end end end end diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 32bcfb8f..291f6e58 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -86,15 +86,14 @@ module Jekyll def generate_url_from_drop(template) template.gsub(%r!:([a-z_]+)!) do |match| - begin - replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze)) - rescue NoMethodError - Jekyll.logger.warn "", "#{match} is not defined!" + key = match.sub(":".freeze, "".freeze) + unless @placeholders.key?(key) + raise NoMethodError, "The URL template key #{key} doesn't exist!" end - if replacement.nil? + if @placeholders[key].nil? "".freeze else - self.class.escape_path(replacement) + self.class.escape_path(@placeholders[key]) end end.gsub(%r!//!, "/".freeze) end diff --git a/test/test_url.rb b/test/test_url.rb index 28fab43d..1a447116 100644 --- a/test/test_url.rb +++ b/test/test_url.rb @@ -93,11 +93,12 @@ class TestURL < JekyllUnitTest matching_doc = site.collections["methods"].docs.find do |doc| doc.relative_path == "_methods/escape-+ #%20[].md" end - URL.new( - :template => "/methods/:title/:headline", - :placeholders => matching_doc.url_placeholders - ).to_s - pass + assert_raises NoMethodError do + URL.new( + :template => "/methods/:headline", + :placeholders => matching_doc.url_placeholders + ).to_s + end end end end