From 347651e5716c768efbc4f613b242bd55ed55bfea Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Thu, 10 Nov 2016 15:34:03 -0800 Subject: [PATCH] URL#generate_url_from_drop: be smarter about replacing *just* the keys --- lib/jekyll/url.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index 291f6e58..e4a93fba 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -84,17 +84,29 @@ module Jekyll end end + def possible_keys(key) + if key.end_with?("_") + [key, key.chomp("_")] + else + [key] + end + end + def generate_url_from_drop(template) template.gsub(%r!:([a-z_]+)!) do |match| - key = match.sub(":".freeze, "".freeze) - unless @placeholders.key?(key) - raise NoMethodError, "The URL template key #{key} doesn't exist!" - end - if @placeholders[key].nil? - "".freeze - else - self.class.escape_path(@placeholders[key]) + pool = possible_keys(match.sub(":".freeze, "".freeze)) + + winner = pool.find { |key| @placeholders.key?(key) } + if winner.nil? + raise NoMethodError, + "The URL template doesn't have #{pool.join(" or ")} keys. Check your permalink template!" end + + value = @placeholders[winner] + value = "" if value.nil? + replacement = self.class.escape_path(value) + + match.sub(":#{winner}", replacement) end.gsub(%r!//!, "/".freeze) end